C++ Write a long float number to a file -


file.txt

123.56 89.78 8.89 468.98 567.90 5.78 178908.90 12.56 6789.90 12.56 16.780 0.00 

i've parsed numbers array called float a[150] = {0}. so, looks like:

a[0] = 123.56 a[1] = 89.78 .... .... a[10] = 16.780 a[11] = 0.00 a[12] = 0 a[13] = 0 ... ... 

then, have sorting algorithm

sort(a, i) // number of elements (12) 

now a[] looks like

a[0] = 0 a[1] = 5.78 ... ... a[10] = 6789.90 a[11] = 178908.90 

then, write file called "final.txt"

std::ofstream file (name); if (file.is_open())     {         (j = 0; j < i; j++)         {             file << a[j] << std::endl;         }     } 

the file output "final.txt" looks like:

0 5.78 8.89 12.56 12.56 16.78 89.78 123.56 468.98 567.9 6789.9 178909 // why not correct !!!!!!! 

the problem a[11] after sorting, why not correct in "final.txt", though correct when debug in a[11] ?

floating point values printed several constraints make values readable. instance not want output of 16.78 read

16.799999999999998 

which might more correct representation of 16.78. avoid operator << operation on ostreams uses precision field determine how many significant digits print. value set small application.

reference http://en.cppreference.com/w/cpp/io/manip/setprecision.

other formatting settings given link πάντα ῥεῖ provided.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -