a=b(mod c)

Machine-specific discussion
Unix, Linux, OS X, OS/2, Windows, ..?
Locked
alex
Posts: 100
Joined: Thu Mar 10, 2005 2:27 pm
Location: Russia

a=b(mod c)

Post by alex »

I knew from my scool, that
1=1(mod 5)
6=1(mod 5)
-1=4(mod 5)
-3=2(mod 5)
and result MUST BE POSITIV !

Why in newlisp
(% -3 5) => -3
and
(mod -2 5) => -2
??

alex
Posts: 100
Joined: Thu Mar 10, 2005 2:27 pm
Location: Russia

Post by alex »

I am sorry - NOT NEGATIVE !

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

There are various ways to look at this, see http://en.wikipedia.org/wiki/Remainder

newLISP behaves like 'C' or Pascal in this case. '%' and 'mod' in newLISP work like the underlying 'C' library routines: '%=' and 'fmod()'

Lutz

William James
Posts: 58
Joined: Sat Jun 10, 2006 5:34 am

Post by William James »

This will always give a non-negative result.

Code: Select all

(define (%% m n , result)
  (set 'result (% m n))
  (+ result (if (< result 0) n 0)))

alex
Posts: 100
Joined: Thu Mar 10, 2005 2:27 pm
Location: Russia

Post by alex »

Or

Code: Select all

(define (%% a b) (% (+ (% a b) b) b))
:-)

William James
Posts: 58
Joined: Sat Jun 10, 2006 5:34 am

Post by William James »

Very nice, but when running on a 6502 processor clocked at 1.79MHz that extra % will cost you.

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

running on a 6502 processor clocked at 1.7
Do you have newLISP running on such a system?
Hans-Peter

William James
Posts: 58
Joined: Sat Jun 10, 2006 5:34 am

Post by William James »

I haven't used my Atari 800XL in ages, but I use my Commodore 64 monitor to watch television; its chroma and luminance inputs accept S-video via a customized cable.

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

I have also an old C64 standing around in my workplace.

But your post gave the impression, that you run newLISP on a aged hardware. Was this impression wrong?
Hans-Peter

William James
Posts: 58
Joined: Sat Jun 10, 2006 5:34 am

Post by William James »

I was only joking. (If you have a working C64 monitor, try running S-video from a DVD player to its chroma and luminance inputs; the picture is very clear.)

Locked