in reply to Re: floating points and sprintf
in thread floating points and sprintf

Check my just posted note. The real value is 147.2. That is mulitplied by 100 (value should be close to 14720.0) and then converted. I understand rounding errors converting from floats to ints but this problem strikes me as a bug.

Replies are listed 'Best First'.
Re: Re: Re: floating points and sprintf
by John M. Dlugosz (Monsignor) on Nov 14, 2002 at 19:11 UTC
    No, the real value is 147.199999999999990, since .2 cannot be represented exactly in a floating-point variable. Multiplying by 100 will not make it magically go back to what you wanted it to be, since the roundoff already occured. 14719.999999... truncated to an int is 14719, exactly what it said it does. This is not a bug, this is a typical floating-point gotcha.

    You probably want to correctly round rather than truncating. E.g. $intval= int(0.5 + $floatval);.