Re: ESL - math.e - rounding
- Posted by "Juergen Luethje" <j.lue at gmx.de> Aug 03, 2005
- 734 views
Jason Gade wrote: > Juergen Luethje wrote: >> >> 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? > > > Not being a memember of the IEEE standards committee, round_half_up() > would be the behavior I expect from a rounding function. That was the > way I was always taught to round. Same for me. That was the only way of rounding that I learned at school. But there actually is the IEEE standard that I mentioned. And for instance PowerBASIC's round() function works according to it. > Except, how does it work for negative numbers? Does it round towards + or -? Sorry, I forgot about that. As far as I know (math experts might want to correct me) round_half_even() works the same as for positive numbers, and round_half_up() always rounds up the absolute value: -- 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 ------------------------------------------------------------------------ > Alexander Toresson wrote: >> round_half_up() would be roof() in qb. >> There's also a function called floor() in qb, which always rounds down. Alexander, what do you mean with "qb"? QBasic? Quick Basic? In all languages that I know which have a floor() function, floor() does the same as Euphoria's floor() does. The opposite of floor() normally is called ceil(). It sounds to me that we are talking about different things here. > It would be nice to have different words for this. For one Euphoria > already has a floor() function, for two it sounds like I'm building a > house instead of a program. > > Just like peek() and poke() make me feel like a pervert. > > How about > round_up(num, places) > round_down(num, places) > round_even(num, places) For almost all cases, there are rules when to round up, and when to round down, which a round function should know and apply automatically. The only problem arises when the (n+1)th digit is '5', and no more digits are following. I never heard of a function round_half_down(). And this should not be confused with floor() or ceil(). > Also, how do these functions work on numbers with very large exponents? > Such as 7.135e15? Are they only for numbers within 15 significant > figures of 0? Both proposed rounding functions normally work the way probably everyone would expect a rounding function to work. The only interesting point is, when the (n+1)th digit is '5', and no more digits are following. *If* Euphoria works with 15 decimal places internally (this is just a guess), then of course it wouldn't make sense to write e.g. ? round_half_up(x, 20) Regards, Juergen