CONFUSED!!

  • Thread starter Thread starter Stefan
  • Start date Start date
S

Stefan

Have a look at the follwoing VB-code. The first MsgBox shows the number 2
while the second one 6. WHY? How come 2.5 is rounded off to 2 and 5.5 is
rounded off to 6??

Dim i As Integer = 5 / 2

MsgBox(i)

Dim j As Integer = 11 / 2

MsgBox(j)
 
Change to integer division if possible if not change from integer type and
they try rounding, i ran in to the same problem .. it baffled me till i
solved it using int division only and gave up.

Dim i As Integer = 5 \2

as to your answer of why the rounding difference .. i dunno?
 
Stefan said:
Have a look at the follwoing VB-code. The first MsgBox shows the
number 2 while the second one 6. WHY? How come 2.5 is rounded off to
2 and 5.5 is rounded off to 6??

Dim i As Integer = 5 / 2

MsgBox(i)

Dim j As Integer = 11 / 2

MsgBox(j)

Mathematical rounding (right words?) means round to the next _even_ number.
 
I forget the name of this type of rounding, but odd and even numbers are
rounded differently. Try 7/2 and 9/2, as well and see the similarities in
the way it is handled.

I will see if I can dig up the article on rounding and post an URL.

--
Gregory A. Beamer
MPV; MCP: +I, SE, SD, DBA

**********************************************************************
Think outside the box!
**********************************************************************
 
Stefan,
I was confused too, it is written at "type conversions functions"
When the fractional part is exactly 0.5, CInt and CLng always round it to
the nearest even number. For example, 0.5 rounds to 0 and 1.5 rounds to 2.
CInt and CLng differ from the Fix and Int functions, which truncate, rather
than round, the fractional part of a number. Also, Fix and Int always return
a value of the same type as is passed in.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vagrptypeconversion.asp

Cor
 
Hi Stefan,

You have a good point there. I did a loop:

Dim I As Integer
For I = 1 To 15
Dim Idiv2 as Integer = I / 2
Debug.WriteLine (I & " / 2 = " & Idiv2)
Next

This produced (extra spacing added):

1 / 2 = 0
2 / 2 = 1
3 / 2 = 2
4 / 2 = 2
5 / 2 = 2
6 / 2 = 3
7 / 2 = 4
8 / 2 = 4
9 / 2 = 4
10 / 2 = 5
11 / 2 = 6
12 / 2 = 6
13 / 2 = 6
14 / 2 = 7
15 / 2 = 8

Something is clearly fishy but I know not what.

Regards,
Fergus
 
Hi Stefan

It's called Accounting Rounding. The idea is that by rounding to the nearest
even number, rounding errors all but cancel out. As the name suggests, used
particularly in accountancy.

HTH

Charles
 
Hello,

Fergus Cooney said:
You have a good point there. I did a loop:

Dim I As Integer
For I = 1 To 15
Dim Idiv2 as Integer = I / 2
Debug.WriteLine (I & " / 2 = " & Idiv2)
Next

This produced (extra spacing added):

1 / 2 = 0
2 / 2 = 1
3 / 2 = 2
4 / 2 = 2
5 / 2 = 2
6 / 2 = 3
7 / 2 = 4
8 / 2 = 4
9 / 2 = 4
10 / 2 = 5
11 / 2 = 6
12 / 2 = 6
13 / 2 = 6
14 / 2 = 7
15 / 2 = 8

Something is clearly fishy but I know not what.

Seems to be the "round-to-even-rule".
 
Hi Stefan,

Herfried tells me it's called the "round-to-even" rule. And now I
understand Charles' post.

I found the following explanation on the Net.

===================================
The result of any arithmetic operation involving floating-point quantities
is rounded to the nearest representable floating-point number (or to ±? if out
of range). In case of ties, where the unrounded result is exactly halfway
between two floating-point numbers, one chooses the one that has a last binary
digit of 0 (the rule of round to even.) The only exception to this rule is
that conversions of floating-point to integer types, using a cast such as
(int) x, always truncate-that is, round to the number nearest to 0, throwing
the fractional part away.

The justifications for the round-to-even rule are subtle. In computations
involving many floating-point operations, it can help avoid biasing the
arithmetic error in any particular direction. It also has the very interesting
property of preventing drift in certain computations. Suppose, for example,
that a certain loop has the effect of computing

x = (x + y) - y;

on each of many iterations (you wouldn't do this explicitly, of course,
but it may happen to one of your variables for certain particular values of
the input data). The round-to-even rule guarantees that the value of x here
will change at most once, and then drift no further.
===================================

I don't like it. The idea that CInt (3 / 2) = CInt (5 / 2) ?? Yuck!!

Regards,
Fergus
 
Hello,

Fergus Cooney said:
I don't like it. The idea that CInt (3 / 2) = CInt (5 / 2) ?? Yuck!!

I don't like it too, because it doesn't work the way my brain is
thinking.
 
Herfried K. Wagner said:
Hello,



I don't like it too, because it doesn't work the way my brain is
thinking.


If it doesn't work like your brain, it probably works correctly.

SCNR ;-))))
 
Thanks for all your replies. I was really frustrated yesterday but
apparently many of you had experienced this problem...

Is there a function that rounds 2.5 off to 3? Or, is there a simple solution
to my problem?
 
Fergus Cooney said:
Hi Stefan,

D = 2.5
I = Int (D + 0.5) 'Rounds up if x.5 or above.

....and as i is declared as Integer and Option Strict is used:

I = CInt(Int (D + 0.5))

;-)
 
Hello,

Fergus Cooney said:
D = 2.5
I = Int (D + 0.5) 'Rounds up if x.5 or above.

\\\
Dim d As Double
Dim i As Integer
d = -2.5
i = CInt(Fix(d + Math.Sign(d) * 0.5))
MsgBox(i)
///

;-)
 
Hi Herfried,

I've never approved of armin' the policemen. Here in Britain most of them
carry truncheons. But guns are becoming much more prevalent, unfortunately.

Regards,
Fergus
 
This was discussed last month. Microsoft uses Bankers Rounding.
Bankers rounding is the number is rounded to the nearest even number.
Example. 11.5 would be rounded to 12, but 8.5 would be rounded to 8.
Microsoft has an article on it on their website.

James W. Hall Sr.
(e-mail address removed)
 
Forget that post, I didn't read the question properley. Sorry.

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
 
Back
Top