rounding to a specific place?
when I have cin >> dprice; get a double value and I type in 8.88, it gets immediately stored as 8.8800000000000008. Now the real questions about this would be like why does it do that, why don't they fix that, and why do people still use C++ when it does stuff like that?
But my question for the time being is, how can I round that value off to one less place to get rid of the extra value that gets tacked on? Math.h's ceil rounds it up to 9 and floor drops it to 8 and it doesn't seem to take any parameters for the amount of decimal places I want so is there another common library that has a round function or any other way to fix it other than rounding?
oh and btw, MSND says the C++ version of math.h has a round function that does just that but the compiler says it doesn't.
Re: rounding to a specific place?
The reason 8.88, it gets is stored as 8.8800000000000008 is because of the way floating point numbers are represented in computers (the IEEE floating point standard). I won't go into details about the format, but the result is that not all decimal numbers can be represented exactly. When you decide "8.88", the number is stored as the closest possible approximation to that value. This will happen in every single programing language , because they all (to my knowledge) use the IEEE FP standard.
You should just ignore it, because it will be negligible in your calculations. If you want to display the value to a certain number of decimal points, you can do something like this:
Code:
#include <iomanip>
#include <iostream>
// ....
float f = 8.88f;
std::cout << std::setprecision(3) << f << std::endl;
Re: rounding to a specific place?
actually it becomes a problem because I'm off by a few pennies. I still can't find a damn rounding function so....
inputting 9.99 as dprice...
dchange = 10.00 - dprice;
results in 0.0099999999999997868 so in the next step
itotalpennies = dchange * 100;
it calculates to 0.99999999999997868 then of course assigns 0 to pennies and it says you don't owe them any change.
Re: rounding to a specific place?
aha! I finally found a link where what someone says makes sense cuz they explain it :P I add 0.5 that way it rounds the value up a penny if it's ###.50000000001 or down if it's ###.4999999999 when converted to an integer :D btw if I'm not mistaken, VB.net's decimal data type doesn't have that problem.
Re: rounding to a specific place?
Quote:
Originally Posted by Desolator144
aha! I finally found a link where what someone says makes sense cuz they explain it :P I add 0.5 that way it rounds the value up a penny if it's ###.50000000001 or down if it's ###.4999999999 when converted to an integer :D btw if I'm not mistaken, VB.net's decimal data type doesn't have that problem.
The .NET System.Double will have the exact same problem. MSDN says:
Quote:
Double complies with the IEC 60559:1989 (IEEE 754) standard for binary floating-point arithmetic.
which means that it represents floating point numbers internally in exactly the same way as C/C++.
Re: rounding to a specific place?
VB.NET's Decimal type doesn't exhibit the precision issue because it's not stored as an approximation.
It's also slower, because decimal operations can't take advantage of the FPU.
Re: rounding to a specific place?
You can do the same thing in C++. Search for "arbitrary precision math libraries". The tricky part is actually separating the wheat from the chaff. One of the best is the GNU multiple precision library, GMPlib:
http://gmplib.org/