TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: cjw on September 02, 2008, 04:36:34 AM

Title: What's wrong with "REM"?
Post by: cjw 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)
)
Title: Re: What's wrong with "REM"?
Post by: Kerry on September 02, 2008, 06:43:38 AM
RTFM

Title: Re: What's wrong with "REM"?
Post by: Keith™ 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))
Title: Re: What's wrong with "REM"?
Post by: CAB 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-)
Title: Re: What's wrong with "REM"?
Post by: New Shooz 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:
Title: Re: What's wrong with "REM"?
Post by: Matt__W 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......."
Title: Re: What's wrong with "REM"?
Post by: Keith™ 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.
Title: Re: What's wrong with "REM"?
Post by: CAB 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)
)
Title: Re: What's wrong with "REM"?
Post by: Keith™ 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
Title: Re: What's wrong with "REM"?
Post by: CAB on September 02, 2008, 11:38:11 AM
Yes, i just took your solution & placed it outside the loop.
Title: Re: What's wrong with "REM"?
Post by: cjw on September 02, 2008, 08:45:39 PM
Thank you all! genius! 8-)
I just don't know how to express my gratitude!
Thanks.
Title: Re: What's wrong with "REM"?
Post by: Keith™ 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 (http://www.theswamp.org/donate.html) .. although it is not required ...

incidently, glad to be of help
Title: Re: What's wrong with "REM"?
Post by: tjr 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.
Title: Re: What's wrong with "REM"?
Post by: Kerry 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





Title: Re: What's wrong with "REM"?
Post by: MP 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.
Title: Re: What's wrong with "REM"?
Post by: Kerry on September 03, 2008, 07:32:24 AM


bye all.
Title: Re: What's wrong with "REM"?
Post by: Keith™ on September 03, 2008, 08:21:04 AM
I don't expect other people to do my homework , or study for me

When the information in the "manual" is incomplete, or worse, inaccurate, then what ...

Considering in lisp, that expressions are evaluated from inside out, it would make perfect sense to expect that (+ sum a) would produce a value that can be used with rem. Clearly there is an error somewhere, either with the use of addition in a rem statement, or with the values returned by addition of real numbers. Without looking into the lisp engine itself, it would be impossible to know where the problem lies, but suffice to say, this is a problem, and very likely a bug. So to that point, to insist that someone RTFM to find a resolution to a potential bug, is both unhelpful and unwarranted. I guess I expect too much ...
Title: Re: What's wrong with "REM"?
Post by: Glenn R on September 05, 2008, 12:17:53 PM
Good call Kerry.
Title: Re: What's wrong with "REM"?
Post by: Spike Wilbury on September 05, 2008, 12:23:22 PM
And for those no English speaking/culture swampers, what this RTFM means?
Title: Re: What's wrong with "REM"?
Post by: It's Alive! on September 05, 2008, 12:55:59 PM
And for those no English speaking/culture swampers, what this RTFM means?

It means, Please Kindly read the documentation… or something like that
Title: Re: What's wrong with "REM"?
Post by: CAB on September 05, 2008, 12:56:32 PM
RTFM is not an appropriate response in this case.
http://www.netlingo.com/lookup.cfm?term=RTFM
Title: Re: What's wrong with "REM"?
Post by: Mark on September 05, 2008, 03:56:53 PM
And for those no English speaking/culture swampers, what this RTFM means?

Read The Fine Manual
Title: Re: What's wrong with "REM"?
Post by: Glenn R on September 05, 2008, 04:00:19 PM
 :lmao:
Title: Re: What's wrong with "REM"?
Post by: Spike Wilbury on September 05, 2008, 04:13:57 PM
OK... it is make sense now, I just sent a letter to my boss using that, waiting for his response...   (instead of regards, i used the rtfm, thanks to you guys!)
Title: Re: What's wrong with "REM"?
Post by: Dinosaur on September 05, 2008, 05:26:36 PM
OK... it is make sense now, I just sent a letter to my boss using that, waiting for his response...   (instead of regards, i used the rtfm, thanks to you guys!)

Was that your current / old boss or the possible future / new boss?
Title: Re: What's wrong with "REM"?
Post by: Spike Wilbury on September 05, 2008, 05:52:25 PM
OK... it is make sense now, I just sent a letter to my boss using that, waiting for his response...   (instead of regards, i used the rtfm, thanks to you guys!)

Was that your current / old boss or the possible future / new boss?

was joking.... :evil:

I had some fun, at least I laugh today....
 :lmao: