Double precision

Hello:

I'm trying to assign the value 3.51 to a double, but it stores 3.5099999999...

So, i'm trying to round it, but I can't use the round function (because the teacher doesn't want it) and passed some time, those errors are critical...

Are some process to save exactly the 3.51??

Regards!
Store it as the integer 351 and divide by 100 when it matters (or never, if the only place it matters is in the output to user).
Last edited on
I tried it but it fail too
The integer 351 is stored as 351. It is not stored as 350.9999999, because it is an integer.

If it appears not to work, you are doing it wrong. Do all the calculations in integers, and when it's time to output the answer, still do not convert it to floating point - output it as the integer answer in the form of a string, with a decimal point inserted in the right place.
The problem is that the objects that programmed the teacher return doubles, and I'm allways using it. I cant be casting from doubles to integers because I wast precision and time... I need to do operations with dobles such divide...
A double can't store 3.51 exactly. If you just care about the output you can use std::setprecision.
1
2
double value = 3.51;
std::cout << std::setprecision(3) << value  << std::endl;
The problem is not the output, is that all the operations that I have, have some errors, and at the end, the error is too big...

The problem is not the output, is that all the operations that I have, have some errors, and at the end, the error is too big...


Doubles are inexact and rounding errors are their undeniable feature. :D
What problem are you solving?
The Selfish simulator, times that a user spend into the system...
have you tried typecasting? put the first variable type in front of the new variable so it interprets it as such.

C-style => (int)(float var)
Yep, but cast didn't solve my problems, I'm working with real numbers, a cast to integer and again to double didn't work
Whatever you're measuring, measure it in integers. If it's time, measure number of microseconds or milliseconds. Don't use doubles.
You can minimize a bit errors with doubles if you don't subtract them too often
Topic archived. No new replies allowed.