954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Rounding up problem

I'm attempting to read lines of a file and store the contents of the line in an object.

This is an example line (x,y,z co-ordinates) :

-12.852222 32.548111 -934.306681

My problem is that my program is rounding these numbers up and I need absolute precision. Having read one line, storing the numbers in the object and printing out the values to make sure they are correct they now look like this:

X: -12.8522 Y: 32.5481 Z: -934.307

This is probably something to do with the types I'm using. The variables are declared as doubles in the object. I've also tried floats, which still gives the same problems.

If you want to see any of my code, please just ask.

Thanks

Quakes
Newbie Poster
7 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

> printing out the values to make sure they are correct they now look like this ...
the initial default precision for a stream is 6. to print out more decimal digits, do something like

std::cout << std::fixed << std::setprecision(8) 
          // 8 digits *after* decimal point
          << value ;


> I need absolute precision.
you will not get absolute precision with floating point values.
if the precision of a long double is not sufficient, you would either use integral types (numerator,denominator) or use a high precision arithmetic library.

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

Thanks very much, knew it would be something simple like that.

Quakes
Newbie Poster
7 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You