in reply to Strange rounding Error in Perl

Any programming language that doesn't expose you to this artifact of decimal to binary conversion for floating point numbers isn't telling the truth. The artifact is there, whether you're using BASIC, COBOL, Pascal, Perl, C/C++, etc. Whether or not you see it will depend on if the language hides it by performing behind the scenes rounding and consequential loss of precision.

See my answer here for a little more info. Trust me, it's not unique to Perl. We learned about it in Computer Science I, in high school in 1984, on the old Franklin Ace 1000 (Apple II similar) computers with FP-Basic.


Dave

Replies are listed 'Best First'.
Re^2: Strange rounding Error in Perl
by pg (Canon) on Nov 22, 2004 at 17:30 UTC

    I don't think you were right about COBOL, so better remove it from your assertion ;-) COBOL supports fixed decimal, you can define picture like 99.99 etc.

      No, floating point math operations in COBOL are suceptible to the binary to decimal conversion problem too. Defining a picture, or whatever you want to call it is essentially forcing rounding to hide the problem. COBOL allows for fixed decimal numbers, and they're handled differently internally. They're not floating point. For floating point math, the decimal to binary conversion error is still there. You're just not seeing it easily because the language is masking it by rounding, and by not showing all of the available digits, and that is exactly what I meant by saying that languages that don't show this behavior are telling a lie.

      Calculators hide the error too, again, by not showing all digits, and by rounding.


      Dave