Re: ESL - math.e - rounding
- Posted by Alexander Toresson <alexander.toresson at gmail.com> Aug 03, 2005
- 763 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? > > > > Regards, > > Juergen > > > > -- > > Have you read a good program lately? > > > > > 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. > > Except, how does it work for negative numbers? Does it round towards + or -? > > > Alexander Toresson wrote: > > round_half_up() would be roof() in qb. > > There's also a function called floor() in qb, which always rounds down. > > 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. Oh, yes, it's round() (which rounds to the nearest integer) and roof() that I always have to reimplement. > Just like peek() and poke() make me feel like a pervert. Lol, yes, and peek4u() is even worse :) > How about > round_up(num, places) > round_down(num, places) > round_even(num, places) That's a good propotion. > 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? > Well, I don't think it would work very well to round 7.135e15 to the nearest integer, because 64-bit floats are starting to become inaccurate with that many decimals. I recommend checking out the following great article about the bottlenecks of floats: http://www.cfxweb.net/modules.php?name=News&file=article&sid=1312&mode=&order=0&thold=0 Regards, Alexander Toresson