Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: float values and operators

by Plankton (Vicar)
on Aug 11, 2004 at 21:28 UTC ( [id://382100]=note: print w/replies, xml ) Need Help??


in reply to Re: float values and operators
in thread float values and operators

Hi Zaxo,

I noticed that this doesn't happen in C ...
bash-2.05b$ cat float.c #include <stdio.h> int main () { float a = 36.8; float b = 36.6; if( a >= ( b + 0.2 ) ) { printf ("true\n"); } } bash-2.05b$ gcc -o float float.c bash-2.05b$ ./float true
... and does happen in Python ...
bash-2.05b$ cat float.py #!/usr/bin/python A = 36.8 B = 36.6 if( A >= B + 0.2 ): print "true" else: print "false" bash-2.05b$ ./float.py false
I would of included a java example but I aint got all day to download the SDK. Maybe I'll update with one later. Can you explain why C deals with float point number while Perl and Python appear to have this problem? Is it a compiled verses and interpreted language issue?

Thanks

Plankton: 1% Evil, 99% Hot Gas.

Replies are listed 'Best First'.
Re^3: float values and operators
by Aristotle (Chancellor) on Aug 11, 2004 at 21:43 UTC

    Are you sure C "deals"?

    $ cat imprec.c #include <stdio.h> int main () { float fa = 36.8; float fb = 36.6; double da = 36.8; double db = 36.6; printf( ( fa >= ( fb + 0.2 ) ) ? "true\n" : "false\n" ); printf( ( da >= ( db + 0.2 ) ) ? "true\n" : "false\n" ); } $ gcc -o imprec imprec.c $ ./imprec true false

    You're seeing the result of cut-off caused by float's lower precision.

    Perl and Python use doubles in all floating point operations.

    Makeshifts last the longest.

Re^3: float values and operators
by ikegami (Patriarch) on Aug 11, 2004 at 21:49 UTC

    C doesn't deal, actually:

    $ cat a.c #include <stdio.h> int main() { printf("%s\n", 36.6 == 36.8-0.2 ? "true" : "false"); return 0; } $ gcc a.c $ a.out false

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-28 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found