Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!know!zaphod.mps.ohio-state.edu!samsung!munnari.oz.au!csc.anu.oz.au!csc3.anu.oz.au!arp!peterf From: peterf@arp.anu.oz.au (Peter Fletcher) Newsgroups: comp.lang.c Subject: Why do most C compilers poxily round towards zero ? Message-ID: <1990Oct9.230928.27552@arp.anu.oz.au> Date: 9 Oct 90 23:09:28 GMT Organization: Automated Reasoning Project, ANU. Lines: 47 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@csis.dit.csiro.au Phone : +61-6-2750914 Fax : +61-6-2571052 Physical : CSIRO Division of Information Technology, ANU, Acton, Canberra ACT AUSTRALIA -------------------------------------------------------------------------------