Re: Rounding of the double
Hi Alex,
I still think you can do what you want using DWORDs and just assume the
decimal position rather than using floats of doubles. FWIW, I think you're
going to have the same problem in any language as the floating point
technology is not specific to C++ or unmanaged C++. There are places where
users have worked around it.
For example, I wrote this function years ago and still use it for finance
applications where I want to show dollars and cents. It seems to still
work. It's not fancy and I certainly wouldn't use it for anything time
critical. Of course I also wanted to round off to the nearest significant
digit as well.
Tom
double round(double value, int sig) /* Round a double or float to 'sig'
places to the left or right of the decimal. */
{
double dhold;
long ihold;
if (sig > 0) { /* Round to the right of the decimal place */
dhold=(value+(5/pow(10.0,(double)sig+1)))*pow(10.0,(double)sig);
ihold=dhold;
return((double)ihold/pow(10.0,(double)sig));
}
else { /* Round to the left of the decimal place */
sig=abs(sig);
dhold=(value+(5*pow(10.0,(double)sig-1)))/pow(10.0,(double)sig);
ihold=dhold;
return((double)ihold*pow(10.0,(double)sig));
}
}
"Alex" <alsim123@hotmail.com> wrote in message
news:1180709515.897443.226890@p47g2000hsd.googlegroups.com...
Again, everybody, please try to understand what I need, I thought
that I'd expressed myself clear enough. But I will try to do it
again:
1. I don't need the representation of this double.
2. I need to divide 8 by 10 and later on to be able use the result
0.2, not 0.199999999999999999
But obviously C++ won't allow me to do so, because of the nature of
float data type.
Or may be I want too much :)
Thanks,
Alex
On Jun 1, 4:52 am, "Les" <l.neil...@nospam.acecad.co.uk> wrote:
"Alex" <alsim...@hotmail.com> wrote in message
news:1180638882.949184.176840@u30g2000hsc.googlegroups.com...
Thanks to everybody for the concern,
Joseph, I cannot take you suggestions, I mean using of expression
instead of variable. I must use variable, because this variable is
some parameter, which I have to use later on. And it's inconvenient
to carry all members of the expression instead of one value...
And this is not matter of a display.
The only thing I need to be able to store dValue = 0.80
Precision - 2 digits (which is 1%). That's all. I don't need
0.80000000.
So if I subtract 1.00 - 0.80 I'm going to get 0.20.
But because I have 0.800000000000004
if I subtract 1.00 - 0. 800000000000004, I'm getting 0.1999999 (I'm
not talking about rounding)
By the way SQL way is not working either, because it returns correct
value, but I have to assign it to some variable in may code, and this
variable must be double, so even though SQL Server returns
0.800000000000000, I'm getting 0.800000000000004
Sorry, but this shows that you have not understood the concept of the
internal *representation* of floating point numbers.
As Joe says "there is no accurate representation of 0.80 in IEEE floating
point"
Read that sentence again.
There are other numbers for which "there is no accurate representation in
IEEE floating point"
You must understand this or all your future floating point math
expectations
will suffer the same consequences as this one.
Les- Hide quoted text -
- Show quoted text -