🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.mac
Article #23152

Re: Lightspeed C bug?

#23152
From: Jim Damoulakis
Date: Thu, 24 Dec 1987 14:34
47 lines
1917 bytes
In article <870081@hpcilzb.HP.COM> cnc@hpcilzb.HP.COM (Chris Christensen) writes:
>I am trying to generate random numbers from 0 to 7.
>
>I use
>
>#define ABS(x) (((x) < 0) ? -(x) : (x))
>x = ABS(Random()) % 8;
>
>I get random numbers from -7 to 7
>
>I use
>
>x = Random();
>x = ABS(x);
>x %= 8;
>
>I get the desired range.
>
>This is the define for Absolut that I have always used. Why doesn't this work?
>Am I being bitten by a "one pass" compiler bug?
>

It looks like you are confusing a preprocessor operation with a function.
Remember that all preprocessing occurs before compilation.   Therefore your

       ABS(Random()) % 8      is expanded to

       (((Random()) < 0) ? -(Random()) : (Random())) % 8;

which should produce integers between -7 and 7.  

You need to isolate the call to Random() before applying the ABS macro, as
you have done in your second example above.

This looks like normal C behavior to me.  I hope this helps.


Jim Damoulakis               Tel. (617) 263-3833  UUCP: jimd@necis.nec.com
NEC Information Systems, Inc.              289 Great Road, Acton, MA 01720
"It's like deja vu all over again"  -- Yogi Berra
-----------------------------------------------------------------------------
-- 
Jim Damoulakis               Tel. (617) 263-3833  UUCP: jimd@necis.nec.com
NEC Information Systems, Inc.              289 Great Road, Acton, MA 01720
"It's like deja vu all over again"  -- Yogi Berra
-----------------------------------------------------------------------------
Technical Details
Message-ID: <583@necis.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!hao!ames!necntc!necis!jimd
References: <870081@hpcilzb.HP.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!hao!ames!necntc!necis!jimd
From: jimd@necis.UUCP (Jim Damoulakis)
Newsgroups: comp.sys.mac
Subject: Re: Lightspeed C bug?
Message-ID: <583@necis.UUCP>
Date: 24 Dec 87 14:34:05 GMT
References: <870081@hpcilzb.HP.COM>
Reply-To: jimd@necis.UUCP (Jim Damoulakis)
Organization: NEC Information Systems, Acton, MA
Lines: 47