[PLUG] Unexpected math result

Russell Senior russell at personaltelco.net
Wed Dec 21 22:03:17 UTC 2011


>>>>> "Tim" == Tim  <tim-pdxlug at sentinelchicken.org> writes:

>> What languages use that, or something giving non-floating point
>> results these days?

Tim> I haven't had to use such things for a long time, but Scheme has
Tim> a data type for floating point and another data type for exact
Tim> numbers. [...]

Yeah, and Common Lisp, and rational numbers (i.e exact fractions), e.g.

[4]> (- (+ (* 2 0.00425) 0.0025) (* 2 0.00425) 0.0025)
-4.656613E-10
[5]> (- (+ (* 2 425/100000) 25/10000) (* 2 425/100000) 25/10000)
0

Or, one of the fun things with GNU CLISP (a particular implementation
of Common Lisp) 

[8]> (- (+ (* 2 0.00425L0) 0.0025L0) (* 2 0.00425L0) 0.0025L0)
4.2351647362715016953L-22
is modifying at runtime the precision of floating
point:

[11]> (SETF (EXT:LONG-FLOAT-DIGITS) 128)
128
[12]> (- (+ (* 2 0.00425L0) 0.0025L0) (* 2 0.00425L0) 0.0025L0)
0.0L0

Also, Common Lisp has something called decode-float:

[13]> (decode-float (- (+ (* 2 0.00425) 0.0025) (* 2 0.00425) 0.0025))
0.5 ;
-30 ;
-1.0
[14]> (decode-float (+ (* 2 0.00425) 0.0025))
0.704 ;
-6 ;
1.0
[15]> (decode-float (* 2 0.00425))
0.544 ;
-6 ;
1.0
[16]> (decode-float 0.0025)
0.64 ;
-8 ;
1.0

Decode-float is breaking a float down into a mantissa, an exponent and
a sign.


-- 
Russell Senior, President
russell at personaltelco.net



More information about the PLUG mailing list