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@xxxxxxxxxxx> wrote in message news:1180709515.897443.226890@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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...@xxxxxxxxxxxxxxxxxxx> wrote:
"Alex" <alsim...@xxxxxxxxxxx> wrote in message

news:1180638882.949184.176840@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx





> 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 -



.



Relevant Pages

  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... And now a question about something else: why do you use floating ... use then to copy a float into a char *1. ... binary representation doesn't resemble a string like "123.46343" ...
    (comp.lang.c)
  • Re: Safer of 2 methods for very long doc
    ... I use wrapped objects so seldom that I have to think about how I've handled that when I have used them, and I guess, as you say, I've moved the anchor. ... paragraph and the picture move to the next page. ... in Word the text does not flow from after a 'floating' ... >I'd appreciate knowing how you "float" a table, ...
    (microsoft.public.word.docmanagement)
  • Re: float vs. double?
    ... There have been repeated myths that float is faster than double. ... In antique machines, the ... Note that the floating-point ALU of Intel chips supports an 80-bit floating point number, ... is different precision being stored? ...
    (microsoft.public.vc.mfc)
  • Re: converting float to double
    ... necessarily the one preferred for financial transactions. ... What he has is a float that approximates the desired value. ... I'd say eschew floating point except where necessary ... cents = lrint ...
    (comp.lang.c)
  • Re: Rounding of the double
    ... But at least part of your problem arises because you aren't formatting the floating point ... And this is not matter of a display. ... As Joe says "there is no accurate representation of 0.80 in IEEE floating ... MVP Tips:http://www.flounder.com/mvp_tips.htm- Hide quoted text - ...
    (microsoft.public.vc.mfc)