comparing floating point numbers in SKILL

J

Jimka

Guest
A very common question to this board is why aren't my two floating
point numbers which look equal actually equal?

w = .3 - .2
0.1
value = 0.1
0.1
w == value
nil
How can this occur? Well, it is usually caused by the fact that w and
value
were derived by different formulas and the numbers actually have two
different binary
representations; nevertheless they print the same because the SKILL
printer ignores
everyting beyond a certain decimal place in printing.

If you want to find out the binary representation of a number between 0
and 1
you can use the following useful function.

(digits w 64)
".00011001100110011001100110011001100110011001100110011000000000000"
(digits value 64)
".00011001100110011001100110011001100110011001100110011010000000000"
(digits value 64) == (digits w 64)
nil
(inScheme
(defun digits (f_number percision)
(letrec ((fraction (lambda (num percision)
(cond ((minusp percision) "")
((num > 1)
(error))
(t (let ((scaled (times 2 num)))
(strcat (sprintf nil "%d" (floor scaled))
(fraction (difference scaled (floor scaled)) (sub1
percision)))))))))
(unless (lessp f_number 1)
(error "specify a number less than 1"))
(unless (plusp f_number)
(error "specify a positive"))
(strcat "." (fraction f_number percision)))))
 

Welcome to EDABoard.com

Sponsor

Back
Top