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

0 Members and 1 Guest are viewing this topic.

ribarm

  • Gator
  • Posts: 3263
  • Marko Ribar, architect
Re: problem with rem function
« Reply #15 on: December 07, 2019, 09:00:26 AM »
Code: [Select]
    (if (or (= (type n1) 'real) (= (type n2) 'real))
      0.0
      0
    )
this should be changed to n1

This is not so important - original (rem) function returns real here - although it's wrong :

Quote
Command: (rem 1 2.0)
1.0
Command: (rem 1 0.5)
0.0
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3263
  • Marko Ribar, architect
Re: problem with rem function
« Reply #16 on: December 07, 2019, 10:15:00 AM »
Actually I was wrong... This was correct :

Code: [Select]
Command: (rem 1 2)
1
Command: (rem 1 2.0)
1.0
Command: (rem 1 0.5)
0.0

I just overprogrammed it... So it's simply :

Code - Auto/Visual Lisp: [Select]
  1. (defun rem ( n1 n2 )
  2.   (if (or (= (type n1) 'real) (= (type n2) 'real))
  3.     (if (minusp n1)
  4.       (- (* (- (/ (float (abs n1)) (float (abs n2))) (fix (/ (float (abs n1)) (float (- (abs n2) 1e-15))))) (+ (abs n2) 1e-15)))
  5.       (* (- (/ (float n1) (float (abs n2))) (fix (/ (float n1) (float (- (abs n2) 1e-15))))) (+ (abs n2) 1e-15))
  6.     )
  7.     (if (minusp n1)
  8.       (fix (- (+ (* (- (/ (float n1) (float (abs n2))) (fix (/ (float n1) (float (- (abs n2) 1e-15))))) (+ (abs n2) 1e-15)) 1e-8)))
  9.       (fix (+ (* (- (/ (float n1) (float (abs n2))) (fix (/ (float n1) (float (- (abs n2) 1e-15))))) (+ (abs n2) 1e-15)) 1e-8))
  10.     )
  11.   )
  12. )
  13.  

Sorry, we all make mistakes sometimes, but built-in function was also wrong... At least I think I fixed it now for sure...
Regards, M.R.
« Last Edit: December 11, 2019, 05:27:11 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

efernal

  • Bull Frog
  • Posts: 206
Re: problem with rem function
« Reply #17 on: December 07, 2019, 10:22:11 AM »
since problem is known, fixed it:

(rem (* 10 0.1)(* 10 0.1))
(rem (* 10 0.2)(* 10 0.1))
(rem (* 10 0.3)(* 10 0.1))
(rem (* 10 0.4)(* 10 0.1))
(rem (* 10 0.5)(* 10 0.1))
(rem (* 10 0.6)(* 10 0.1))
(rem (* 10 0.7)(* 10 0.1))
(rem (* 10 0.8)(* 10 0.1))
(rem (* 10 0.9)(* 10 0.1))
(rem (* 10 1.0)(* 10 0.1))
(rem (* 10 1.1)(* 10 0.1))
(rem (* 10 1.2)(* 10 0.1))
(rem (* 10 1.3)(* 10 0.1))
(rem (* 10 1.4)(* 10 0.1))
(rem (* 10 1.5)(* 10 0.1))

e.fernal

efernal

  • Bull Frog
  • Posts: 206
Re: problem with rem function
« Reply #18 on: December 07, 2019, 10:24:20 AM »
Command:
Command: (rem (* 10 0.1)(* 10 0.1))
0.0

Command: (rem (* 10 0.2)(* 10 0.1))
0.0

Command: (rem (* 10 0.3)(* 10 0.1))
0.0

Command: (rem (* 10 0.4)(* 10 0.1))
0.0

Command: (rem (* 10 0.5)(* 10 0.1))
0.0

Command: (rem (* 10 0.6)(* 10 0.1))
0.0

Command: (rem (* 10 0.7)(* 10 0.1))
0.0

Command: (rem (* 10 0.8)(* 10 0.1))
0.0

Command: (rem (* 10 0.9)(* 10 0.1))
0.0

Command: (rem (* 10 1.0)(* 10 0.1))
0.0

Command: (rem (* 10 1.1)(* 10 0.1))
0.0

Command: (rem (* 10 1.2)(* 10 0.1))
0.0

Command: (rem (* 10 1.3)(* 10 0.1))
0.0

Command: (rem (* 10 1.4)(* 10 0.1))
0.0

Command: (rem (* 10 1.5)(* 10 0.1))
0.0

e.fernal

mac0312

  • Mosquito
  • Posts: 13
Re: problem with rem function
« Reply #19 on: December 07, 2019, 11:13:15 PM »
Thanks all!  :smitten:

mac0312

  • Mosquito
  • Posts: 13
Re: problem with rem function
« Reply #20 on: December 07, 2019, 11:21:21 PM »
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 [color=red]1e-8[/color]) (equal d r 1e-8))) (rem x d))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (divp 13.1 0.1)
  2. T

(divp 12.4 0.1)→(rem x d)
Sometimes 1.0e-006, sometimes 1.0e-008
« Last Edit: December 07, 2019, 11:24:57 PM by mac0312 »

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: problem with rem function
« Reply #21 on: December 08, 2019, 06:37:02 AM »
I think 1e-8 is more than sufficient -
Code - Auto/Visual Lisp: [Select]
  1. _$ (rtos (rem 12.4 0.1) 2 17)
  2. "0.09999999999999966"

ribarm

  • Gator
  • Posts: 3263
  • Marko Ribar, architect
Re: problem with rem function
« Reply #22 on: December 08, 2019, 06:42:12 AM »
I think 1e-8 is more than sufficient -
Code - Auto/Visual Lisp: [Select]
  1. _$ (rtos (rem 12.4 0.1) 2 17)
  2. "0.09999999999999966"

Lee are you saying (rem) built-in function is working well... I think this was bug... (rem 12.4 0.1) should be 0.0 and not 0.1... (/ 12.4 0.1) = 124 without reminder - reminder is 0.0
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: problem with rem function
« Reply #23 on: December 08, 2019, 06:58:02 AM »
I think 1e-8 is more than sufficient -
Code - Auto/Visual Lisp: [Select]
  1. _$ (rtos (rem 12.4 0.1) 2 17)
  2. "0.09999999999999966"

Lee are you saying (rem) built-in function is working well... I think this was bug... (rem 12.4 0.1) should be 0.0 and not 0.1... (/ 12.4 0.1) = 124 without reminder - reminder is 0.0

I'm saying that issues such as this are inevitable when working with doubles due to the inability to represent certain Real numbers with finite memory, and one has to remember that the error introduced at the nth decimal place can be both positive & negative; in this case, since we are working with modular arithmetic, the error will be either 0+1e-16 or m-1e-16 where m is the modulus.

ribarm

  • Gator
  • Posts: 3263
  • Marko Ribar, architect
Re: problem with rem function
« Reply #24 on: December 08, 2019, 07:05:26 AM »
But with my function, there is no such issue... I've reprogrammed (rem) function to be correct... And I placed it at first place in my acaddoc.lsp before all other things and I've tested it with my acaddoc.lsp, this returned 0.0 which is correct :

Code: [Select]
(defun c:remtest nil
  (princ (rem 13.3 0.1))
  (princ)
)

My function, if you didn't noticed is here :
http://www.theswamp.org/index.php?topic=53061.msg597469#msg597469

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

:)

M.R. on Youtube

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: problem with rem function
« Reply #25 on: December 08, 2019, 02:17:16 PM »
I'm probably missing something but why would it need to be more ambitious than:

Code: [Select]
(defun _rem ( n d / r )
    (cond
        ((equal 0 (setq r (rem n d)) 1e-6) 0)
        ((apply 'equal (mapcar 'abs (list d r 1e-6))) 0)
        (r)
    )
)

?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ribarm

  • Gator
  • Posts: 3263
  • Marko Ribar, architect
Re: problem with rem function
« Reply #26 on: December 08, 2019, 05:08:14 PM »
Yes, you are missing something :
My (rem n1 n2) is designed to overwrite buit-in (rem) function, and your if you change name (_rem) to (rem) it will act recursively calling function you just defined and not built-in (rem)... I am afraid it has to be my way if you want to overwrite original (rem) definition...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: problem with rem function
« Reply #27 on: December 08, 2019, 05:32:08 PM »
I didn’t intend to overwrite the built in function thus my naming was deliberate. In over 30 years of LISP programming I don’t recall ever having such need and remain of the opinion it should be avoided [unless replicating Joshua Overkamp’s efforts of late - highly unlikely for me]. Mileages vary of course.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ribarm

  • Gator
  • Posts: 3263
  • Marko Ribar, architect
Re: problem with rem function
« Reply #28 on: December 10, 2019, 05:48:56 AM »
...remain of the opinion it should be avoided [unless replicating Joshua Overkamp’s efforts of late - highly unlikely for me]...

Why would you keep original (rem) if you know it's buggy? I would say if you can mention where is this errornous odd behaviour of original (rem) used in correct and succesful manner, then I'll appreciate your opinion, otherwise I and perhaps everyone else would suggest (strongly) that you should overwrite buggy (rem)... As for efficency behaviour it's I suppose just slightly slower than original as its not built-in optimized Alisp Function like others are...
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

VovKa

  • Water Moccasin
  • Posts: 1629
  • Ukraine
Re: problem with rem function
« Reply #29 on: December 10, 2019, 12:35:47 PM »
buggy
(rem_ribarm 1 0.0000000000001)