Problem With comparing Float\Double values..

8 views
Skip to first unread message

Kullai reddy

unread,
Nov 17, 2008, 11:51:48 PM11/17/08
to Customize your ACAD
This is the problem in all Languages including .net


Command: (equal (* 3.0 0.05) 0.15)
nil

Command: (* 3 0.05)
0.15

Command: (rtos (* 3.0 0.05) 2 20)
"0.1500000000000000"


Command: (equal (* 3.0 0.05) 0.15)
nil

Command: (equal (* 4.0 0.05) 0.20)
T

Command: (equal (* 4 0.05) 0.20)
T

Command: (equal (* 4 0.05) 0.20)
T

Command: (equal (* 4 0.05) 0.2)
T

Command: (equal (* 5 0.05) 0.25)
T

Command: (equal (* 1 0.05) 0.05)
T

Command: (equal (* 7 0.05) 0.35)
nil

Command: (equal (* 9 0.05) 0.45)
T

Command: (equal (* 8 0.05) 0.40)
T

Command: (equal (* 11 0.05) 0.55)
T

Command: (equal (* 13 0.05) 0.65)
T

Command: (equal (* 15 0.05) 0.75)
T

Command: (equal (* 17 0.05) 0.85)
nil


Solution for This

There is only one Solution When Comparing Float/Decimal numbers in any
Programming languages compare the Difference with

A= 0.15
B= (* 3 0.15)
(If (< (- A B) Tolerance) t nil) or use (Equal A B Tolerance) function
in Lisp


Example:
Command: (equal (* 3.0 0.05) 0.15 0.000000000000001)
T

Masani

unread,
Nov 18, 2008, 7:36:10 AM11/18/08
to Customize your ACAD

Kullai,

Have you ever tried to find out why this is happening?

The reason behind this is very interesting one.

One of the reasons floating point numbers can be tricky is due to non-
obvious differences between binary and decimal (base 10) numbers. In
normal decimal numbers, the fraction 1/3rd is the infinite decimal
sequence: 0.333333333… Similarly, consider the fraction 1/10. In
decimal, this is easy represented as 0.1, and we are used to thinking
of 0.1 as an easily representable number. However, in binary, 0.1 is
represented by the infinite sequence: 0.00011001100110011…

Any way, thanks for the post. This is really help full for a
programmer.

Thank You,
Sreekar Masani
Reply all
Reply to author
Forward
0 new messages