Re: ESL - math.e - rounding
- Posted by "Christian Cuvier" <christian.cuvier at agriculture.gouv.fr> Aug 03, 2005
- 769 views
> Date: Wed, 03 Aug 2005 14:53:22 +0200 > From: "Juergen Luethje" <j.lue at gmx.de> > Subject: ESL - math.e - rounding > > > Hi all, > > here are some thoughts concerning ESL rounding functions. > > Any function for rounding a number should take a second argument that > denotes the number of digital places that we want the result to have, > e.g. > round(77.123456, 3) = 77.123 > > There is an IEEE standard for rounding. A *different* way of rounding > is what we call in German "kaufmaennisches Runden". > Translated word by word to English, this would be "commercial rounding". > Does this (or a similar) term exist in English? > round_half_even(x,n) -- according to IEEE > round_half_up(x,n) -- "kaufmaennisches Runden" > > > Both functions sometimes return different results, when the (n+1)th > decimal place of x equals 5: > round_half_even() -- rounds to nearest even value > round_half_up() -- always rounds up ( surprise!) > > -- Example > x = 7.125 > round_half_even(x,2) --> 7.12 > round_half_up(x,2) --> 7.13 > > x = 7.135 > round_half_even(x,2) --> 7.14 > round_half_up(x,2) --> 7.14 > > > What do you all think? > > Regards, > Juergen > In theory, we need the 4 rounding modes below. And the Intel FP architecture supports them all. [Quoting from Intel FPU documentation] Table 4-8. Rounding Modes and Encoding of Rounding Control (RC) Field -------------------------------------------------------------------- Rounding ! RC Field ! Setting Description Mode ! ! ---------------+----------+---------------------------------------- ! ! Rounded result is the closest to the Round to ! 00B ! infinitely precise result. If two values nearest (even) ! ! are equally close, the result is the even ! ! value (that is, the one with the ! ! least-significant bit of zero). Default ---------------+----------+------------------------------------------ Round down ! ! Rounded result is closest to but no greater (toward =E2=88=92=E2=88=9E) ! 01B !than the infinitely precise resu= lt. ---------------+----------+------------------------------------------ Round up ! ! Rounded result is closest to but no less (toward +=E2=88=9E) ! 10B !than the infinitely precise result. ---------------+----------+------------------------------------------ Round toward ! ! Rounded result is closest to but no greater zero (Truncate)! 11B !in absolute value than the infinitely precise ! !result. --------------------------------------------------------------------- Commercial rounding is just rounding up, while scientific calculations use round to nearst more often. CChris