Author Topic: What's wrong with "REM"?  (Read 6941 times)

0 Members and 1 Guest are viewing this topic.

cjw

  • Guest
What's wrong with "REM"?
« on: September 02, 2008, 04:36:34 AM »
1.
This is OK--------
(rem 1000 1000)
->0
(rem 1000.0 1000)
->0.0
2.My question is here:
Why???---------
(+ sum a)
->1000.0
(rem (+ sum a) 1000)
->1000.0

from the code:

Quote
(defun c:tt ()
  (setq lst '("193.8" "130" "150" "30" "110" "50" "80"))
  (setq   i      0
   sum    0
   return nil
   a      (rem 35256.2 1000)
   tmp    nil
  )
  (repeat (length lst)
    (setq b (read (nth i lst)))
    (setq sum (+ sum b))
    (setq tmp (cons (+ sum a) tmp))
    (setq return (cons (rem (+ sum a) 1000) return))
    (setq i (1+ i))
  )
  (princ (reverse tmp))
  (princ "\n")
  (princ (reverse return))
  (princ)
)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: What's wrong with "REM"?
« Reply #1 on: September 02, 2008, 06:43:38 AM »
RTFM

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: What's wrong with "REM"?
« Reply #2 on: September 02, 2008, 08:44:22 AM »
The problem exhibits itself only with the result of addition in the function.

In the example provided, the value of sum is 743.8 and the value of a is 256.2, thus the value of (+ sum a) is 1000.0 and the value of (rem (+ sum a) 1000) should be 0.0 ...

To resolve the issue use this
Code: [Select]
(setq return (cons (rem (atof (rtos (+ sum a) 2 8)) 1000) return))
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: What's wrong with "REM"?
« Reply #3 on: September 02, 2008, 09:26:19 AM »
The mixing of reals and integers can be a "Got Ya" in lisp. As you can see if you use
integers for calculations you get integers BUT if you use one real your result will be a real.
This has caused more that one question here and added hours of debugging frustration
to many a programmer.

Also see FLOAT and FIX functions. There are many rounding routines here at The Swamp
if you need one.


PS I too have been guilty of reading the manual last. 8-)
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.

New Shooz

  • Guest
Re: What's wrong with "REM"?
« Reply #4 on: September 02, 2008, 09:30:11 AM »
The trouble I've always found with REM is their bald, skinny, whiny voiced singer - can't stand him to be honest  :pissed:

Matt__W

  • Seagull
  • Posts: 12955
  • I like my water diluted.
Re: What's wrong with "REM"?
« Reply #5 on: September 02, 2008, 09:32:18 AM »
The trouble I've always found with REM is their bald, skinny, whiny voiced singer - can't stand him to be honest  :pissed:

"It's the end of the world as we know it......."
Autodesk Expert Elite
Revit Subject Matter Expert (SME)
Owner/FAA sUAS Pilot @ http://skyviz.io

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: What's wrong with "REM"?
« Reply #6 on: September 02, 2008, 10:07:11 AM »
The mixing of reals and integers can be a "Got Ya" in lisp. As you can see if you use
integers for calculations you get integers BUT if you use one real your result will be a real.
This has caused more that one question here and added hours of debugging frustration
to many a programmer.

Also see FLOAT and FIX functions. There are many rounding routines here at The Swamp
if you need one.


PS I too have been guilty of reading the manual last. 8-)

CAB, I don't think the problem is with real vs. integer calculations. The help clearly states that if you use a real and integer, a real is returned.

In the example provided, (rem (+ sum a) 1000) should return 0.0 as (+ sum a) = 1000.0 that would mean that (rem 1000.0 1000) = 0.0 and would be correct, except (rem (+ sum a) 1000) returns 1000.0 .. clearly an incorrect return value. It would appear as though there is a float error in the mathematics.

Try these examples
(rem 1000.0 999.99999999999999) = 0.0
(rem 999.9999999999999 1000) = 1000.0
(rem 999.99999999999999 1000) = 0.0

In the function provided, the value of a is 256.2 (the remainder of 35256.2 / 1000), the value of sum is incremented by the value of b and sum, initialized at 0

Even forcing an evaluation of the value would incorrectly show that the value (eval (+ sum a)) is less than 1000, which is clearly not true.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: What's wrong with "REM"?
« Reply #7 on: September 02, 2008, 10:50:05 AM »
Thanks for the light bulb there Keith. :)
Would this also be a solution?
Code: [Select]
(defun c:tt ()
  (setq lst '("193.8" "130" "150" "30" "110" "50" "80"))
  (setq   i      0
   sum    0
   return nil
   a      (atof (rtos (rem 35256.2 1000) 2 8))  ; <-----<<
   tmp    nil
  )
  (repeat (length lst)
    (setq b (read (nth i lst)))
    (setq sum (+ sum b))
    (setq tmp (cons (+ sum a) tmp))
    (setq return (cons (rem (+ sum a) 1000) return))
    (setq i (1+ i))
  )
  (princ (reverse tmp))
  (princ "\n")
  (princ (reverse return))
  (princ)
)
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.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: What's wrong with "REM"?
« Reply #8 on: September 02, 2008, 11:33:13 AM »
That does appear to be a resolution, as is the solution I put in my first post
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: What's wrong with "REM"?
« Reply #9 on: September 02, 2008, 11:38:11 AM »
Yes, i just took your solution & placed it outside the loop.
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.

cjw

  • Guest
Re: What's wrong with "REM"?
« Reply #10 on: September 02, 2008, 08:45:39 PM »
Thank you all! genius! 8-)
I just don't know how to express my gratitude!
Thanks.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: What's wrong with "REM"?
« Reply #11 on: September 02, 2008, 10:30:11 PM »
I just don't know how to express my gratitude!

Perhaps at some point in the future you might consider dropping a nickle in the offering plate .. although it is not required ...

incidently, glad to be of help
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

tjr

  • Guest
Re: What's wrong with "REM"?
« Reply #12 on: September 03, 2008, 12:48:49 AM »
RTFM
No matter how you slice it this is pretty much a "DH" response. Sometimes people need a foot to backside push in the right direction to find things. Happens to everyone. However the elitist "RTFM" stuff is a little unwarranted.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: What's wrong with "REM"?
« Reply #13 on: September 03, 2008, 04:51:00 AM »
..  elitist ...


I THINK NOT !



I don't expect other people to do my homework , or study for me .. ( nor the reciprocal)  If that makes me elitist by your definition, I can live with it.  :-P





kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: What's wrong with "REM"?
« Reply #14 on: September 03, 2008, 06:58:51 AM »
I THINK NOT !

It's curious why a man of your intelligence would be daft to the notion that this kind of response, in particular to a noob to the forum, would be somewhere between rude and off putting.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst