comparing floating point numbers in SKILL

Discussion in 'Cadence' started by Jimka, Nov 18, 2006.

  1. Jimka

    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
    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)
    (digits value 64) == (digits w 64)
    (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)))))
     
    Jimka, Nov 18, 2006
    #1
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.