in reply to Re: Okay! What!?!?!?
in thread Okay! What!?!?!?

It's been about 15 years since I've written any Pascal, but here you go...

program floating; var A, B :real; begin A := 14.4e0; B := 10.0e0 + 14.4e0 - 10.0e0; writeln( A ); writeln( B ); if A = B then writeln('they are equal') else writeln('they differ'); end.

Compiled using Free Pascal Compiler 2.6.2-5 (i386), I get the following output:

1.44000000000000E+001 1.44000000000000E+001 they are equal

I don't know if FPC is especially smart at optimizing the mathematical expressions, or especially smart at DWIM output and comparisons on floating point numbers. But it's being smart somehow.

use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

Replies are listed 'Best First'.
Re^3: Okay! What!?!?!? (order)
by tye (Sage) on Apr 23, 2014 at 14:32 UTC

    This could be explained by the compiler knowing to perform the constant folding in an order that reduces the risk of loss of precision.

    So do the addition and subtraction as separate steps.

    - tye        

Re^3: Okay! What!?!?!?
by davido (Cardinal) on Apr 23, 2014 at 14:11 UTC

    There must be some behind the scenes DWIMery, because according to the documentation, FreePascal's "real" type maps directly to a standard IEEE 754 "double" on most architectures, and as such, should suffer from the same shortcomings of representing floating point numbers in base 2.


    Dave