Author Topic: problem with rem function  (Read 13403 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
problem with rem function
« on: May 18, 2017, 12:11:30 PM »
Hello everyone.

I have a strange result of the rem function other than I expected.

For example:
Code - Auto/Visual Lisp: [Select]
  1.  (rem 1.8 0.6)
Doesn't return 0.0 but it returns 1.11022e-016

Thank you.

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: problem with rem function
« Reply #1 on: May 18, 2017, 12:42:46 PM »
it's a well know autocad's behavior
problem is not only with rem function
see
Code: [Select]
(= (- 1.1 1.0) 0.1) -> nil

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: problem with rem function
« Reply #2 on: May 18, 2017, 12:45:09 PM »
Refer to Floating-point Arithmetic Accuracy Problems.

Suggestion:
Code - Auto/Visual Lisp: [Select]
  1. (equal 0.0 (rem 1.8 0.6) 1e-8)

Coder

  • Swamp Rat
  • Posts: 827
Re: problem with rem function
« Reply #3 on: May 18, 2017, 12:56:39 PM »
Thank you guys for your kind replies.

I thought my thread would look stupid to ask but I think it wasn't.

mac0312

  • Mosquito
  • Posts: 13
Re: problem with rem function
« Reply #4 on: December 07, 2019, 02:46:48 AM »
(rem 13.3 0.1) → 0.1
autocad 2014 test

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: problem with rem function
« Reply #5 on: December 07, 2019, 04:36:04 AM »
(rem 13.3 0.1) → 0.1
autocad 2014 test

Confiremed the same, AutoCAD 2018...
 :oops:

Code: [Select]
Command: (rem 0.1 0.1)
0.0
Command: (rem 0.2 0.1)
0.0
Command: (rem 0.3 0.1)
0.1
Command: (rem 0.4 0.1)
0.0
Command: (rem 0.5 0.1)
0.1
Command: (rem 0.6 0.1)
0.1
Command: (rem 0.7 0.1)
0.1
Command: (rem 0.8 0.1)
0.0
Command: (rem 0.9 0.1)
0.1
Command: (rem 1.0 0.1)
0.1
Command: (rem 1.1 0.1)
2.77556e-17
Command: (rem 1.2 0.1)
0.1
Command: (rem 1.3 0.1)
0.1
Command: (rem 1.4 0.1)
0.1
Command: (rem 1.5 0.1)
0.1
« Last Edit: December 07, 2019, 05:06:44 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: problem with rem function
« Reply #6 on: December 07, 2019, 04:55:32 AM »
BricsCAD v20.1.06

Code: [Select]
: (rem 13.3 0.1)
0.1
: (rem 13.2 0.1)
0.0999999999999986

Code: [Select]
: (rem 0.1 0.1)
0.0
: (rem 0.2 0.1)
0.0
: (rem 0.3 0.1)
0.1
: (rem 0.4 0.1)
0.0
: (rem 0.5 0.1)
0.1
: (rem 0.6 0.1)
0.1
: (rem 0.7 0.1)
0.0999999999999999
: (rem 0.8 0.1)
0.0
: (rem 0.9 0.1)
0.1
: (rem 1.0 0.1)
0.1
: (rem 1.1 0.1)
0.0
: (rem 1.2 0.1)
0.0999999999999999
: (rem 1.3 0.1)
0.1
: (rem 1.4 0.1)
0.0999999999999998
: (rem 1.5 0.1)
0.0999999999999999
« Last Edit: December 07, 2019, 05:00:56 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: problem with rem function
« Reply #7 on: December 07, 2019, 05:32:30 AM »
Solution is to perhaps use custom (rem n1 n2) function :

Code: [Select]
Command: (fix (/ 13.3 0.1))
133
Command: (rtos (/ 13.3 0.1) 2 50)
"133.0000000000000"
Command: (rem 13.3 0.1)
0.1
Command: (defun rem ( n1 n2 )
(_> (- (/ (float n1) (float n2)) (fix (/ (float n1) (float n2))))
(_> )
REM
Command: (rem 13.3 0.1)
0.0
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: problem with rem function
« Reply #8 on: December 07, 2019, 05:36:38 AM »
Since I don't know how to change built-in function, I'll put this in my acaddoc.lsp :

Code: [Select]
(defun rem ( n1 n2 )
  (- (/ (float n1) (float n2)) (fix (/ (float n1) (float n2))))
)

HTH., M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: problem with rem function
« Reply #9 on: December 07, 2019, 05:49:46 AM »
Code: [Select]
(defun rem ( n1 n2 )
  (- (/ (float n1) (float n2)) (fix (/ (float n1) (float n2))))
)
(rem 4.1 0.1)

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: problem with rem function
« Reply #10 on: December 07, 2019, 05:57:46 AM »
I saw it VovKa... I've just fixed it :

Code: [Select]
(defun rem ( n1 n2 )
  (if (< (abs n1) (abs n2))
    (if (or (= (type n1) 'real) (= (type n2) 'real))
      0.0
      0
    )
    (if (or (= (type n1) 'real) (= (type n2) 'real))
      (if (minusp n1)
        (- (* (- (/ (float (abs n1)) (float (abs n2))) (fix (/ (float (abs n1)) (float (- (abs n2) 1e-16))))) (abs n2)))
        (* (- (/ (float n1) (float (abs n2))) (fix (/ (float n1) (float (- (abs n2) 1e-16))))) (abs n2))
      )
      (if (minusp n1)
        (fix (- (* (- (/ (float (abs n1)) (float (abs n2))) (fix (/ (float (abs n1)) (float (- (abs n2) 1e-16))))) (abs n2))))
        (fix (* (- (/ (float n1) (float (abs n2))) (fix (/ (float n1) (float (- (abs n2) 1e-16))))) (abs n2)))
      )
    )
  )
)
« Last Edit: December 07, 2019, 07:25:25 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: problem with rem function
« Reply #11 on: December 07, 2019, 07:43:31 AM »
Here are the results after implementation of my subfunction :

Code: [Select]
Command: (rem 0.1 0.1)
0.0
Command: (rem 0.2 0.1)
0.0
Command: (rem 0.3 0.1)
-4.44089e-17
Command: (rem 0.4 0.1)
0.0
Command: (rem 0.5 0.1)
0.0
Command: (rem 0.6 0.1)
-8.88178e-17
Command: (rem 0.7 0.1)
-8.88178e-17
Command: (rem 0.8 0.1)
0.0
Command: (rem 0.9 0.1)
0.0
Command: (rem 1.0 0.1)
0.0
Command: (rem 1.1 0.1)
0.0
Command: (rem 1.2 0.1)
-1.77636e-16
Command: (rem 1.3 0.1)
0.0
Command: (rem 1.4 0.1)
-1.77636e-16
Command: (rem 1.5 0.1)
0.0
Command: (rem 2.05 0.1)
0.05
Command: (rem -2.05 0.1)
-0.05
Command: (rem -2 0.1)
0.0
Command: (rem -2 1)
0
Command: (rem 1 2)
0
Command: (rem 1.0 2)
0.0
Command: (rem 1.0 2.0)
0.0
Command: (rem 1.0 3.0)
0.0
Command: (rem pi pi)
0.0
Command: (rem (* 2 pi) pi)
0.0
Command: (rem (* 3 pi) (* 0.5 pi))
0.0
Command: (rem (* 3.05 pi) (* 0.5 pi))
0.15708
Command: (rtos (rem (* 3.05 pi) (* 0.5 pi)) 2 50)
"0.1570796326794891"
Command: (rtos (* 0.05 pi) 2 50)
"0.1570796326794896"

HTH., M.R.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: problem with rem function
« Reply #12 on: December 07, 2019, 08:05:56 AM »
Since rem is typically used to test the even divisibility of a number by a given divisor, you can easily account for the rounding errors introduced by the limited precision of the double-precision floating-point format by simply testing the result with some tolerance, e.g.:
Code - Auto/Visual Lisp: [Select]
  1. (defun divp ( x d )
  2.     ((lambda ( r ) (or (equal 0.0 r 1e-8) (equal d r 1e-8))) (rem x d))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (divp 13.1 0.1)
  2. T


ribarm

  • Gator
  • Posts: 3225
  • Marko Ribar, architect
Re: problem with rem function
« Reply #13 on: December 07, 2019, 08:27:20 AM »
Code - Auto/Visual Lisp: [Select]
  1. ... (equal d r 1e-8) ...
  2.  

??
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

VovKa

  • Water Moccasin
  • Posts: 1626
  • Ukraine
Re: problem with rem function
« Reply #14 on: December 07, 2019, 08:36:07 AM »
Code: [Select]
    (if (or (= (type n1) 'real) (= (type n2) 'real))
      0.0
      0
    )
this should be changed to n1