www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/20/07:13:59

From: gfoot AT mc31 DOT merton DOT ox DOT ac DOT uk (George Foot)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: fixed point math: SOLUTION!!!
Date: 20 Feb 1997 09:40:59 GMT
Organization: Oxford University
Lines: 27
Message-ID: <5eh67b$m3i@news.ox.ac.uk>
References: <5egfq4$3pk AT nr1 DOT toronto DOT istar DOT net>
NNTP-Posting-Host: mc31.merton.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

: solution... "WHENEVER using fixed point math make profuse use of
: brackets!!!"  Like the following:

: #define putpixel(x,y,c) \
: _farpokeb(_dos_ds, 0xA0000+(y)*320+(x), (c));

YM "Whenever using #define, make profuse use of brackets" :)

It's a big problem; I think it's generally pretty safe if you enclose
all parameters in a #define in at least one set of brackets. If you're
writing an expression, you should also enclose the entire #define in
brackets. For example,

#define subtract1(a,b) a-b           /* very bad */
#define subtract2(a,b) (a)-(b)       /* better   */
#define subtract3(a,b) ((a)-(b))     /* best     */

The point about subtract2 still not being perfect is that, for example,

3*subtract2(4,2) => 3*(4)-(2)    /* this is not likely to be what was meant */
3*subtract3(4,2) => 3*((4)-(2))  /* what was probably intended              */

Somewhere in the info docs they address this common confusion, I think.

-- 
George Foot <gfoot AT mc31 DOT merton DOT ox DOT ac DOT uk>
Merton College, Oxford.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019