Group: General Forum Members Last Login: Monday, December 8, 2008 8:42 AM Points: 775,Visits: 214

Since the other thread was trainwrecked, but since there is indeed a problem with the function as currently written, I thought we should start a new thread to fix the problem. This is a valuable tool for many toolboxes, and agreeing on one that meets the specification would be a boon to those who need it. If you think banker's rounding is stupid, we have a thread for you to voice your opinion here. Please keep it out of this one.

To start, I'd like to ask David Jackson to post the latest iteration of the function, so we can go from there. My initial impression is that we might want to end up with two versions, one handling decimal/numeric data as input, and another with float as input, to help reduce the issues that arise from floating point numbers on computers.

This method is also known as unbiased rounding or as statistician's rounding or as bankers' rounding. It is identical to the common method of rounding except when the digit(s) following the rounding digit start with a five and have no non-zero digits after it. The new algorithm is:

* Decide which is the last digit to keep. * Increase it by 1 if the next digit is 6 or more, or a 5 followed by one or more non-zero digits. * Leave it the same if the next digit is 4 or less * Otherwise, all that follows the last digit is a 5 and possibly trailing zeroes; then change the last digit to the nearest even digit. That is, increase the rounded digit if it is currently odd; leave it if it is already even.

With all rounding schemes there are two possible outcomes: increasing the rounding digit by one or leaving it alone. With traditional rounding, if the number has a value less than the half-way mark between the possible outcomes, it is rounded down; if the number has a value exactly half-way or greater than half-way between the possible outcomes, it is rounded up. The round-to-even method is the same except that numbers exactly half-way between the possible outcomes are sometimes rounded up—sometimes down.

Although it is customary to round the number 4.5 up to 5, in fact 4.5 is no nearer to 5 than it is to 4 (it is 0.5 away from either). When dealing with large sets of scientific or statistical data, where trends are important, traditional rounding on average biases the data upwards slightly. Over a large set of data, or when many subsequent rounding operations are performed as in digital signal processing, the round-to-even rule tends to reduce the total rounding error, with (on average) an equal portion of numbers rounding up as rounding down. This generally reduces the upwards skewing of the result.

Round-to-even is used rather than round-to-odd as the latter rule would prevent rounding to a result of zero.

Examples:

* 3.016 rounded to hundredths is 3.02 (because the next digit (6) is 6 or more) * 3.013 rounded to hundredths is 3.01 (because the next digit (3) is 4 or less) * 3.015 rounded to hundredths is 3.02 (because the next digit is 5, and the hundredths digit (1) is odd) * 3.045 rounded to hundredths is 3.04 (because the next digit is 5, and the hundredths digit (4) is even) * 3.04501 rounded to hundredths is 3.05 (because the next digit is 5, but it is followed by non-zero digits)

-- ============================================= -- Example to execute function -- ============================================= SELECT dbo.fn_BRound (282.26545, 100) -- 100 to nearest penny, 20 to nearest 5 pence, 10 to nearest 10 pence, -- 5 to nearest 20 pence, 2 to nearest 50 pence, 1 to nearest pound/dollar/euro

*/ AS BEGIN DECLARE@TempDOUBLEPRECISION, @FixTempDOUBLEPRECISION

SELECT@Temp=@p1*@p2

--This is the closest I can get, but it is still wrong for certain values SELECT@FixTemp=SIGN(@Temp+0.5*SIGN(@p1))*FLOOR(ABS(@Temp+0.5*SIGN(@p1)))

-- Handle rounding of .5 in a special manner IF@Temp-FLOOR(@Temp)=0.5 BEGIN IF@FixTemp/2<>FLOOR(@FixTemp/2)-- Is Temp odd -- Reduce Magnitude by 1 to make even SELECT@FixTemp=@FixTemp-SIGN(@p1) END

Group: General Forum Members Last Login: Monday, December 8, 2008 8:42 AM Points: 775,Visits: 214

I'll try and summarize the Wiki entry that is in the code comments, so that everyone understands what it is supposed to do.

Banker's rounding is identical to traditional rounding with a single exception. When the digit to the immediate right of the desired final display digit is a 5, and when no non-zero characters follow it, you round to the nearest even digit. That's it.

Examples:

When rounding to 2 decimal places...

0.1249999999999999999999999999999999999999999 returns 0.12 using both methods

0.1250000000000000000000000000000000000000001 returns 0.13 using both methods

0.1250000000000000000000000000000000000000000 rounds down to .12 using Banker's rounding, since the second digit, 2, is an even number, and up to .13 using traditional rounding, since traditional rounding always rounds up at 5 or greater.

On the other hand....

0.1349999999999999999999999999999999999999999 returns 0.13 using both methods

0.1350000000000000000000000000000000000000001 returns 0.14 using both methods

0.1350000000000000000000000000000000000000000 rounds up to 0.14 using Banker's rounding, since the second digit, 3, is odd, and the nearest even number is 4 (0.14 is much closer than 0.12 to 0.135), while traditional rounding also returns 0.14, since it always rounds up at 5 or greater.

As you can see, they behave differently very rarely, but when they do, it's intentional, so that's what we need to provide for.

Group: General Forum Members Last Login: Monday, December 8, 2008 8:42 AM Points: 775,Visits: 214

While this is a trick on his part, I actually agree with him, even though he'll disagree with what this function should do with it (he can take that to the other thread). What he's not telling you is that SQL Server will do an implicit round on this number before it ever gets to the function, so the function will actually only receive .125 as a parameter, instead of its actual value which is something like 1.2503125781445361340335083770943e-6. We should indeed be able to handle that, and based on the rules above (which is what this thread is about), should return .12.

On the other hand, if the function actually receives the value 1.2503125781445361340335083770943e-6 from SQL Server, then we should return .13, based on the rules above.

So yes, he's correct, we should be able to handle that. It's just that we'll handle it differently depending on whether we get the implicitly rounded version or the more accurate version of the number, as that's how Banker's rounding is designed, again, as noted above.

Group: General Forum Members Last Login: 2 days ago @ 6:32 AM Points: 441,Visits: 1,827

Sergiy

you was politely asked to stay out of this thread. The reason you got an error was because you are using Lynns function, which does not take the same parameters as mine. Try:

Note: I'm at home now, and can't test this, (Not got SQL at Home), but simple proof reading, even after a beer, tells me you are not bothering to even read the thread properly.

Group: General Forum Members Last Login: Monday, December 8, 2008 8:42 AM Points: 775,Visits: 214

David, while I agree that it was his intention, he didn't actually crap in this thread (I realize that the day isn't over yet), as his post was useful to demonstrate what the function should be expected to do. He might not like the answer as to how it should behave in each of the two given situations, but that is what Banker's rounding is supposed to do.

You cannot post new topics. You cannot post topic replies. You cannot post new polls. You cannot post replies to polls. You cannot edit your own topics. You cannot delete your own topics. You cannot edit other topics. You cannot delete other topics. You cannot edit your own posts. You cannot edit other posts. You cannot delete your own posts. You cannot delete other posts. You cannot post events. You cannot edit your own events. You cannot edit other events.

You cannot delete your own events. You cannot delete other events. You cannot send private messages. You cannot send emails. You may read topics. You cannot rate topics. You cannot vote within polls. You cannot upload attachments. You may download attachments. You cannot post HTML code. You cannot edit HTML code. You cannot post IFCode. You cannot post JavaScript. You cannot post EmotIcons. You cannot post or upload images.