r/visualbasic • VB.Net Advanced • Apr 12 '13

I found a bug in VB: math class

http://imgur.com/AUwuPmJ
6 Upvotes

7 comments sorted by

10

u/smokeajay Apr 12 '13

Not a bug, but rather a feature. I think it is called a banker's round. Even whole numbers are rounded down and odd whole numbers are rounded up (from .5). Wiki link

1

u/no1warlord VB.Net Advanced Apr 12 '13

Hmm, never seen that before, thanks! Is there a pre-built function to do it the other way?

6

u/CharlieMay VB.Net Intermediate Apr 12 '13
Sub Main()
    Console.WriteLine(Math.Round(4.5))
    Console.WriteLine(Math.Round(5.5))
    Console.WriteLine(Math.Round(4.5, MidpointRounding.AwayFromZero))
    Console.WriteLine(Math.Round(5.5, MidpointRounding.AwayFromZero))
    Console.ReadLine()
End Sub

Results:

4

6

5

6

Midpoint values and rounding conventions

Rounding involves converting a numeric value with a specified precision to the nearest value with less precision. For example, you can use the Round(Double) method to round a value of 3.4 to 3.0, and the Round(Double, Int32) method to round a value of 3.579 to 3.58.

In a midpoint value, the value after the least significant digit in the result is precisely half way between two numbers. For example, 3.47500 is a midpoint value if it is to be rounded two decimal places, and 7.500 is a midpoint value if it is to be rounded to an integer. In these cases, the nearest value can't be easily identified without a rounding convention.

The Round method supports two rounding conventions for handling midpoint values:

Rounding away from zero

Midpoint values are rounded to the next number away from zero. For example, 3.75 rounds to 3.8, 3.85 rounds to 3.9, -3.75 rounds to -3.8, and -3.85 rounds to -3.9. This form of rounding is represented by the MidpointRounding.AwayFromZero enumeration member.

Rounding away from zero is the most widely known form of rounding. Rounding to nearest, or banker's rounding

Midpoint values are rounded to the nearest even number. For example, both 3.75 and 3.85 round to 3.8, and both -3.75 and -3.85 round to -3.8. This form of rounding is represented by the MidpointRounding.ToEven enumeration member.

Rounding to nearest is the standard form of rounding used in financial and statistical operations. It conforms to IEEE Standard 754, section 4. When used in multiple rounding operations, it reduces the rounding error that is caused by consistently rounding midpoint values in a single direction. In some cases, this rounding error can be significant.

1

u/no1warlord VB.Net Advanced Apr 12 '13

Thanks that's helped a lot, much appreciated :)

1

u/RCubed111 Apr 12 '13

Also, Math.Floor will give you the next smallest integer, and Math.Ceiling will give you the next largest integer.

1

u/no1warlord VB.Net Advanced Apr 12 '13

Yeah I knew that, I make a youtube tutorial series about VB, and I recorded an episode about that amongst other things today. If you're interested in the series then here it is: http://www.youtube.com/playlist?list=PLqJXDdbv9bT8x46jIzXyJrQ-AhJtrNLAQ&feature=view_all Although the math.Floor/Ceiling wont be in it until tomorrow for me (GMT).

The series goes from very basic first program to ..... well, I haven't finished it yet. Although each video has a long description as of today :)

1

u/[deleted] Apr 12 '13

I feel like such a nerd that i already knew that. Good post.