Lisp HUG Maillist Archive

Floating point error?

Hi.

I'm getting some really strange behavior on LWM:

(* 0.02 5)
==> 0.099999994

I can't imagine I'm the first person to notice this. Am I missing 
something fundamental about how to carry out floating point arithmetic 
in LispWorks?

Thanks,
-Chris


RE: Floating point error?

> I'm getting some really strange behavior on LWM:
> 
> (* 0.02 5)
> ==> 0.099999994

which LispWorks version do you use? I can't reproduce it in LispWorks 4.3.7,
Windows version.

But your problem is a general problem, because not every decimal number can
be expressed as a floating point number. If you are calculating with money,
try using cents.

Regards,

Frank


Re: Floating point error?

On Oct 30, 2004, at 2:41 PM, Frank Buss wrote:
> which LispWorks version do you use? I can't reproduce it in LispWorks 
> 4.3.7,
> Windows version.

LispWorks 4.3.7 running on OS X 10.3.5. Incidentally, MCL 5.0 gives the 
correct answer on the same system.

> But your problem is a general problem, because not every decimal 
> number can
> be expressed as a floating point number. If you are calculating with 
> money,
> try using cents.

In this case, I am able to represent the quantities as integers. 
However, while I certainly understand that there are limits to floating 
point representation, it seems that LispWorks should be able to 
adequately represent integer multiples of 0.02.

Comparing MCL to LispWorks, I find
LispWorks:
least-positive-single-float ==>1.4012985E-45
(float-precision 0.02) ==> 24

MCL 5.0:
least-positive-single-float ==> 5.0E-324
(float-precision 0.02) ==> 53

Are the LW settings different for PC and Mac? If so, is there a way to 
adjust these values?

Thanks,
-Chris S


RE: Floating point error?

> Comparing MCL to LispWorks, I find
> LispWorks:
> least-positive-single-float ==>1.4012985E-45
> (float-precision 0.02) ==> 24

on Windows it has other limits:

CL-USER> least-positive-single-float
4.9406564584124657E-324

CL-USER> (float-precision 0.02)
53

CL-USER> least-positive-double-float
4.9406564584124657E-324

Regards,

Frank


Re: Floating point error?

On Oct 30, 2004, at 2:17 PM, Chris R. Sims wrote:

>
> I'm getting some really strange behavior on LWM:
>
> (* 0.02 5)
> ==> 0.099999994

For what it's worth, I get exactly the same answer (i.e., 0.099999994) 
from all of the following on Mac OS X 10.3.5 on a dual G5:

sbcl
OpenMCL
LWM 4.3.7
acl trial edition
clisp
cmucl

In fact, MCL 5.0 is the *only* common lisp implementation of the 7 
running on this machine that returns 1.0.

Thought you'd want to know.

regards,

Ralph



Raffael Cavallaro, Ph.D.
raffaelcavallaro@mac.com

Updated at: 2020-12-10 08:54 UTC