Author Topic: return values of REM ???  (Read 1686 times)

0 Members and 1 Guest are viewing this topic.

Peter2

  • Swamp Rat
  • Posts: 650
return values of REM ???
« on: July 27, 2014, 10:42:37 AM »
I tried the "rem"-function, 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.  
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: return values of REM ???
« Reply #2 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

Peter2

  • Swamp Rat
  • Posts: 650
Re: return values of REM ???
« Reply #3 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.
Peter

AutoCAD Map 3D 2023 German (so some technical terms will be badly retranslated to English)
BricsCAD V23

Lee Mac

  • Seagull
  • Posts: 12906
  • London, England
Re: return values of REM ???
« Reply #4 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...