473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

CONFUSED!!

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)
Nov 20 '05 #1
18 1855
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" <st******@student.uu.se> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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)

Nov 20 '05 #2
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" <st******@student.uu.se> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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)

Nov 20 '05 #3
"Stefan" <st******@student.uu.se> schrieb
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.
--
Armin

Nov 20 '05 #4
Cor
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/de...conversion.asp

Cor
Nov 20 '05 #5
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
Nov 20 '05 #6
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
"Stefan" <st******@student.uu.se> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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)

Nov 20 '05 #7
Hello,

"Fergus Cooney" <fi******@tesco.net> schrieb:
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".

--
Herfried K. Wagner
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet
Nov 20 '05 #8
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
Nov 20 '05 #9
Hello,

"Fergus Cooney" <fi******@tesco.net> schrieb:
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
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet
Nov 20 '05 #10
"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> schrieb
Hello,

"Fergus Cooney" <fi******@tesco.net> schrieb:
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.

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

SCNR ;-))))
--
Armin

Nov 20 '05 #11
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?

"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> schrieb
Hello,

"Fergus Cooney" <fi******@tesco.net> schrieb:
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.

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

SCNR ;-))))
--
Armin

Nov 20 '05 #12
Hi Stefan,

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

Regards,
Fergus
Nov 20 '05 #13
"Fergus Cooney" <fi******@tesco.net> schrieb
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))

;-)

--
Armin

Nov 20 '05 #14
Hello,

"Fergus Cooney" <fi******@tesco.net> schrieb:
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)
///

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet
Nov 20 '05 #15
Hello,

"Armin Zingler" <az*******@freenet.de> schrieb:
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))


Armin the policeman.

--
Herfried K. Wagner
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet
Nov 20 '05 #16
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
Nov 20 '05 #17
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.
jh***@alpha-transportation.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #18
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'"
"Tom Spink" <th**********@ntlworld.com> wrote in message
news:uW*************@TK2MSFTNGP09.phx.gbl...
Because they are Integers... An Integer is a whole number.

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

Please respond to the newsgroup,
so all can benefit

"Maybe it's a game called 'Punish the User'"
"Stefan" <st******@student.uu.se> wrote in message
news:#F**************@TK2MSFTNGP09.phx.gbl...
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)


Nov 20 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
2
by: Brian Roberts | last post by:
I'm confused about the use of hasattr/getattr, or possibly namespaces. I know how to do this: class UnderstandThis(object): def do_foo(self): pass def do_bar(self): pass def doit(self, cmd):...
11
by: Ohaya | last post by:
Hi, I'm trying to understand a situation where ASP seems to be "blocking" of "queuing" requests. This is on a Win2K Advanced Server, with IIS5. I've seen some posts (e.g.,...
6
by: ree32 | last post by:
I am a bit confused with capabilities of XML. I have an XML document with information on images(photos). Is there way to use XSL/XSLT to create a page that will display the images as gallery. ...
5
by: Jeff Amiel | last post by:
Yes, I've read the FAQ's... I'm still confused. I'm trying to help out a buddy to extract data from an .mdb file that has special 'permissions' on it. If I try to open it with the standard...
10
by: Lauren Wilson | last post by:
Ok I have searched the MS website for info on this. I am totally confused. If I want to deploy an Access 2003 app and allow my users to run it using Access 2003 Runtime, where do I get the...
1
by: Benny Ng | last post by:
Hi,All, Export Method: ------------------------------------------------------------------------- strFileNameExport = "Results" Response.Clear() Response.Buffer = True...
2
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP...
11
by: timmu | last post by:
Someone asked me a question about integer division and printf yesterday, I tell him he should do a casting to float/double before you do any interger division. But he doesn't think so, so I try...
2
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.