Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Okay! What!?!?!?

by tobyink (Canon)
on Apr 23, 2014 at 11:27 UTC ( [id://1083333]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1083333]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-03-29 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found