TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Peter2 on July 27, 2014, 10:42:37 AM

Title: return values of REM ???
Post by: Peter2 on July 27, 2014, 10:42:37 AM
I tried the "rem"-function (http://www.theswamp.org/~john/avlisp/#rem), and many results are OK.

But why - look at the second part of the code - "(rem 1.8 0.3)" and others give a remainder? Normally, 1.8 : 0.3 = 6; 0 remainder ..
Code - Auto/Visual Lisp: [Select]
  1. Befehl: (rem 12 3)
  2. 0
  3. Befehl: (rem 13 3)
  4. 1
  5. Befehl: (rem 13 -3)
  6. 1
  7. Befehl: (rem -13 3)
  8. -1
  9. Befehl: (rem -12 3)
  10. 0
  11. Befehl: (rem 1.1 1)
  12. 0.1
  13. Befehl: (rem 1.9 1)
  14. 0.9
  15. Befehl: (rem -1.9 1)
  16. -0.9
  17. Befehl: (rem -1.9 0.1)
  18. -0.1
  19. Befehl: (rem -1.9 -0.1)
  20. -0.1
  21. Befehl: (rem -19 -1)
  22. 0
  23. Befehl: (rem -19 -1)
  24. 0
  25.  
  26. Befehl: (rem -1.9 0.1)
  27. -0.1
  28. Befehl: (rem 1.9 0.1)
  29. 0.1
  30. Befehl: (rem 1.8 0.1)
  31. 0.1
  32. Befehl: (rem 1.8 0.9)
  33. 0.0
  34. Befehl: (rem 1.8 1)
  35. 0.8
  36. Befehl: (rem 1.8 1.8)
  37. 0.0
  38. Befehl: (rem 1.8 0.2)
  39. 0.2
  40. Befehl: (rem 1.8 0.3)
  41. 1.11022e-016
  42. Befehl: (rem 1.8 0.6)
  43. 1.11022e-016
  44. Befehl: (rem 1.8 0.9)
  45. 0.0
  46. Befehl: (/ 1.8 0.6)
  47. 3.0
  48.  
Title: Re: return values of REM ???
Post by: CAB on July 27, 2014, 11:13:42 AM
Maybe this tread will help:
http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Rem-function-question/td-p/822420 (http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Rem-function-question/td-p/822420)

and this one
http://www.theswamp.org/index.php?topic=27412.5
Title: Re: return values of REM ???
Post by: Lee Mac on July 27, 2014, 11:56:52 AM
In short, when supplying the rem function with doubles (reals) that are ostensibly divisible, the absolute value returned by rem will either be very close to zero or very close to the divisor, depending on whether the rounding of the doubles is above or below the value displayed.

Hence, some tolerance is required for comparisons, e.g.:

Code - Auto/Visual Lisp: [Select]
  1. (defun div-p ( n m )
  2.     (setq n (abs (rem n m)))
  3.     (or (equal 0.0 n 1e-8) (equal m n 1e-8))
  4. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (div-p 1.8 0.3)
  2. T
  3. _$ (div-p 1.9 0.1)
  4. T
  5. _$ (div-p -1.9 0.1)
  6. T
Title: Re: return values of REM ???
Post by: Peter2 on July 29, 2014, 04:23:58 AM
I'm always scared how this floating-point / double-stuff affects on really simple things like "1.8 : 0.2" ... :x

Thanks to  CAB and Lee.
Title: Re: return values of REM ???
Post by: Lee Mac on July 29, 2014, 07:05:10 PM
I'm always scared how this floating-point / double-stuff affects on really simple things like "1.8 : 0.2" ... :x

Its all manageable  :-)

I think the pros of the floating-point format outweigh the cons -
That said, this is obvious, as we would have long abandoned its use by now if they didn't...