Why do most C compilers poxily round towards zero ?
Peter Fletcher
peterf at arp.anu.oz.au
Wed Oct 10 09:09:28 AEST 1990
One of the most frustrating things about using floating point in C is the
poxy way rounds floating point numbers towards zero instead of -infinity.
This creates a big inconsistency around 0.0 and makes any sort of consistent
rounding a bigger hassle than you'd like:
To round a number, instead of
int a;
float b;
...
a = (int)(b+0.5);
you need to do
a = (b>0.0) ? (int)(b+0.5) : (int)(b-0.5);
Worse, to floor a number, instead of
a = (int)b;
you need to do either
a = floor(b); or
a = (b>0.0) ? (int)b : -(int)(-b);
Also, you have silly situations where
(int)(b-10.0)+10 doesn't always equal (int)b
I don't think this policy is in any of the C specifications, but it occurs
in all the C compilers I've used (sun, apollo, pyramid). Does anyone know
why ?
I think with Suns and Apollos you can tell the floating point hardware
to use a different rounding mode, but it seems that both methods are
completely different and probably not compatible with anybody else.
Is there a reasonably standard Unix way to do this ?
-peter fletcher
-------------------------------------------------------------------------------
Internet : peterf at csis.dit.csiro.au
Phone : +61-6-2750914
Fax : +61-6-2571052
Physical : CSIRO Division of Information Technology,
ANU, Acton, Canberra ACT AUSTRALIA
-------------------------------------------------------------------------------
More information about the Comp.lang.c
mailing list