Re: strange casting problem

From: <nguler_at_jlab.org>
Date: Fri, 25 Apr 2008 20:55:35 -0400


Thanks for your input.
I verified that the source of this problem is as described by Kevin. And interestingly, the problem goes away if I change:

       double aa = W/W_width ;
 to:

       float aa = W/W_width ;

So, this looks like some kind of imperfection of "double" type.

Best Regards,

> Hi Nevzat,
>
> Nevzat Guler wrote:
>> Hello,
>> I apologize for bringing here non-root problem but I discovered it in my
>> root
>> program and also tested with a stand alone C Program.
>> I attached the simple program to the email and printed at the end ... The
>> problem is, when I convert a float to int, I have value offset.
>
> The problem is that doubles are stored on computers in base 2, so the
> computer cannot internally represent the exact value of decimal 0.01
> which you are dividing by (nor of the numbers 1.19, etc. which you
> divide). So even though you think you are dividing 1.19/0.01, instead
> of 119 exactly you end up with 118.99999999999997 or some such. Note
> that "cout << aa;" prints the value of aa rounded to (I believe) six
> decimal places by default.
>
> If you know that your results really should be integers, you could round
> them to the nearest integer with int(floor(aa + 0.5)) -- some math
> libraries also include functions like round() or rint() to do this for you.
>
>> Below you can see some examples. The problem appears between 113 and 119.
>> I used g++ -o tt tt.cc to complie the attached program and run it.
>> I am very puzzled and thought maybe you can help on this.
>
> [...]
>> aa=119 int(aa)=118 ceil(aa)=119 floor(aa)=118
> [...]
>
> Here's where you divide 1.19 (I assume) passed in as W, by 0.01 passed
> in as W_width:
>
>> int GetWIndex(double W, double W_start, double W_width)
>> {
>> double aa = W/W_width ;
>
> best regards,
>
> --
> Kevin B. McCarty <kmccarty_at_gmail.com>
> WWW: http://www.starplot.org/
> WWW: http://people.debian.org/~kmccarty/
> GPG: public key ID 4F83C751
>
Received on Sat Apr 26 2008 - 02:53:34 CEST

This archive was generated by hypermail 2.2.0 : Sat Apr 26 2008 - 11:50:02 CEST