255/1000 is a periodic number in binary just like 1/3 is a periodic number in decimal. It would take infinite storage to store it as a floating point number, so it is stored approximately.
$ perl -e'printf "%.20e\n", 1.255'
1.25499999999999989342e+00
$ perl -e'printf "%.20e\n", (1.255 * 100 + 0.5) * 10000000'
1.25999999999999976158e+09
Is there a way to make perl behave as a humble user of arithmetics would have expected
No. If you're using floats, there's no way to make both of these true since the input is the same:
int(1259999999.999999) = 1259999999
int((1.255 * 100 + 0.5) * 10000000) = 1256000000
Said humble user will be disappointed in one of the two cases. But maybe you're ok with the user being disappointed in the former case. Just apply a bit of rounding at the desired tolerance.
( $j = sprintf("%.6f", $j) ) =~ s/\..*//s;
|