Floating Point Precision Error
Asked Answered
C

2

7

I am having problem with the LISP expression below. There is floating precision error while doing sum for floating point numbers.

CL-USER> (+ -380 -158.27 -35.52)

Actual:   -573.79004
Expected: -573.79000

Please suggest me how can I achieve the expected result in LISP (I am using Lispworks).

Capet answered 10/12, 2013 at 9:5 Comment(3)
why would you expect anything else if you compute with floats of a certain precision?Tamikatamiko
Actually, I am porting an application from ORACLE to lisp and the ORACLE application gives -573.79000 but the LISP returns -573.79004. This has caused many mismatches while testing the end result of the applications.Capet
@Capet Sounds like your tests need a wider threshold. In particular, you want any values within, say, 0.00005 to be accepted.Sergei
V
5

Floats are not exact

Floats represent a subset of mathematical reals with certain precision, they are not exact numbers. Round off errors are inevitable.

The ANSI Common Lisp standard provides for 4(!) levels of float precision: short, single, double, long (all implementations provide at least 2). Neither is exact, but ORACLE is probably using double, so if you stick with doubles, you should be fine.

Theory

Please read What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Use integers and ratios if you want exact numbers

If you want to do exact computations, you should be using integers (e.g., representing currency as a number of cents, not dollars) or ratios.

Veradi answered 10/12, 2013 at 14:21 Comment(0)
T
9

Floating point numbers are not necessarily exact. Typically an implementation has single and double floats. There might be also short and long floats.

Single float

CL-USER 7 > (+ -380 -158.27 -35.52)
-573.79004

Now with double floats:

CL-USER 8 > (+ -380 -158.27d0 -35.52d0)
-573.79D0

LispWorks thus has two different float types (possibly short-float is a third, depending of 32bit or 64bit architecture):

CL-USER 9 > (describe 35.52)

35.52 is a SINGLE-FLOAT

CL-USER 10 > (describe 35.52d0)

35.52D0 is a DOUBLE-FLOAT

See also: *read-default-float-format*.

Tamikatamiko answered 10/12, 2013 at 9:17 Comment(0)
V
5

Floats are not exact

Floats represent a subset of mathematical reals with certain precision, they are not exact numbers. Round off errors are inevitable.

The ANSI Common Lisp standard provides for 4(!) levels of float precision: short, single, double, long (all implementations provide at least 2). Neither is exact, but ORACLE is probably using double, so if you stick with doubles, you should be fine.

Theory

Please read What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Use integers and ratios if you want exact numbers

If you want to do exact computations, you should be using integers (e.g., representing currency as a number of cents, not dollars) or ratios.

Veradi answered 10/12, 2013 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.