Search code examples
lispautocadautolisp

Vlisp autocad: condition (> a b) is true even when a is equal b. Why?


i got a problem with my lisp. I'll put here where you can find the problem.

;values for debug
(setq l_max 2
      delta_sup 60
      phi_superiore 10
      delta_inf 40
      phi_inferiore 10
      lunghezza 10.0)

;code starts here
(setq l_ferri_sup l_max
      l_ferri_inf l_max
      n 1
      distanza l_max)
(while (> lunghezza distanza)
      (setq distanza (- (+ distanza l_max) (/ (* delta_sup phi_superiore) 1000.0))
            n (1+ n))
)

(setq l_ferri_sup (- (* n l_max) (- distanza lunghezza))
      n 1
      distanza l_max)


(while (> lunghezza distanza) ;WHEN "distanza" is 10.0, condition still true
      (setq distanza (- (+ distanza l_max) (/ (* delta_inf phi_inferiore) 1000.0))
            n (1+ n))
)

(setq l_ferri_inf (- (* n l_max) (- distanza lunghezza)))

If you try to run this few lines you will find the problem at the second while condition. It's very strange.. Any idea about that?

Thanks, Dennis

EDIT: i've correct an error in the question


Solution

  • l_maxis an integer (32-bit), distanza is a real (64-bit double-precision floating-point). This can lead to some rounding errors:

    (- 3.6 2.4) ; Returns 1.2
    (= 1.2 (- 3.6 2.4)) ; Returns nil
    (equal 1.2 (- 3.6 2.4) 1e-6) ; Returns T
    

    Try to initialize l_max with a real:

    (setq l_max 2.0)
    

    Or use an epsilon:

     (> lunghezza (+ distanza 1e-10))
    

    1e-10 is what AutoCAD uses to compare 2 reals by default.