[PLUG] Unexpected math result

Michael R michael at jamhome.us
Wed Dec 21 20:24:09 UTC 2011



I was reminded of something this morning.   Perhaps you will enjoy
the puzzle.

 mikeraz at hive:~> perl -e 'my $x =
(.0425 *
2) + .0025; print $x - (.0425 * 2) - .0025, $/;'

 2.16840434497101e-18
  mikeraz at hive:~>
 


Silly me would expect something like 0 to be printed.  Why the
2.16840434497101e-18?  (OK, round it off and you have 0, but ....) 


Before you chuckle about Perl, consider:

mikeraz at hive:~>
cat fp.c
#include <stdio.h>
#include <math.h>

main() {
    int x;
    x
=     ( .00425 * 2 ) + .0025;
   
printf ( "%24.24f\n", x - ( .00425 * 2) - .0025);

}

mikeraz at hive:~> gcc fp.c -o fp
mikeraz at hive:~> ./fp
-0.011000000000000001096345
mikeraz at hive:~>

http://bit.ly/uSOdNh

-- 
     Michael Rasmussen
  
http://www.jamhome.us/
 Be Appropriate && Follow Your
Curiosity
_______________________________________________
Pdx-pm-list mailing list
Pdx-pm-list at pm.org
http://mail.pm.org/mailman/listinfo/pdx-pm-list

-- 
    
Michael Rasmussen
   http://www.jamhome.us/
 Be Appropriate
&& Follow Your Curiosity


More information about the PLUG mailing list