[PLUG] Unexpected math result

Tim tim-pdxlug at sentinelchicken.org
Wed Dec 21 20:29:11 UTC 2011



On Wed, Dec 21, 2011 at 12:24:09PM -0800, Michael R wrote:
> 
> 
> 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 ....) 


That's floating point for you, and most traditional languages use that
by default.  If you want exact calculations, you have to use a library
specifically designed for them, or use a language which does exact
calculations by default.

tim



More information about the PLUG mailing list