http://qs1969.pair.com?node_id=11151092

bliako has asked for the wisdom of the Perl Monks concerning the following question:

Mise Wonks,

The problem has been encountered many times: print int(2.26*100) = 225 (although print 2.26*100 = 226).

LanX's Humans have too many fingers warns about Floats are not accurate with decimal fractions because the computer "has only two fingers" and offers this advice: calculate with integers in the desired accuracy and shift the decimal point afterwards.

And this is what I was attempting to do: I was converting the float 2.26 into an integer 226 in order to increment it by 1. (Background: 2.26 is a version number and I wanted to bump it up to version 2.27. Since then I discovered Version::Next which can be a solution ...)

In C I get the correct result it seems to me:

# gcc a.c #include <stdio.h> #define ABS(A) ((A)>0?(A):(-(A))) int main(void){ float version = 2.26; float newversion = version + 0.01; printf("version=%f, newversion=%f\n", version, newversion); printf("2.26*100=%.8f\n", 2.26*100); float x = 226.0; if( ABS(x-226.0)>0.00000001 ){ printf("ajaja\n"); } }

And in bc: echo "2.26*100" | bc -l gives 226.00

I have perl v5.36.0 and a modern linux OS on a modern desktop, 64bit, and :

perl -V:nvtype -V:nvsize nvtype='double'; nvsize='8';

So, the advice to use decimal cents to do calculations in cents instead of using floating dollars is sound but how do I get to the decimal cents in the first place?

Oh, wonk: a person preoccupied with arcane details or procedures in a specialized field; broadly : nerd. hehe (Edit: I think "nerd" is superfluous and plain wrong).

bw, bliako