in reply to Re^3: Strange int() result
in thread Strange int() result

Very interesting test! I could not understand at first why multiplying with an even integer factor would introduce rounding errors. So it seemed, but when I went one step back and ignored the multiplication I saw: the value itself is not expressible as a fixedpoint binary number.

So there are two ways to look at it (numerically and as a string):

$ perl -wE '$x=35784.45; printf "%.15f\n%s", $x, $x;' 35784.449999999997090 35784.45

Since now I always assumed the string representation were made from the numerical representation thus giving the same output. That was obviously wrong.

Update:

A second counter test

$ perl -wE '$x=35784.449999999997090; printf "%.15f\n%s", $x, $x;' 35784.449999999997090 35784.45
reveals: the string representation always uses some rounding.

Replies are listed 'Best First'.
Re^5: Strange int() result
by Laurent_R (Canon) on Jun 18, 2014 at 22:15 UTC
    Another test under the debugger to give you food for thought:
    DB<13> printf "%d", 35784.45 * 100; 3578444 DB<14> printf "%s", 35784.45 * 100 3578445
    But the real question was addressed earlier by davido: where is the 35784.45 piece of data coming from? If it is read in a text file or from a text-format network, then using regexes or printf "%s", ... is not only right, but is probably the best solution. If it comes from a previous calculation or from binary data, then it may be slightly more complicated, details would be needed about its original format.