Look at this code:
Dim i As Integer = 0
Dim j As Integer = 5
If (j / i = 7) Or i = 0 Then
MessageBox.Show("In If")
Else
MessageBox.Show("In Else")
End If
I would have expected to get an error thrown in the execution of the "If"
statement since the division operation will cause a "divide by zero"
exception. But this doesn't happen. The following statement, added before
the "If" WILL cause an exception...natually:
j=j/i
What rule states that an If statement is "exempt" from errors? Or, better
yet, what am I missing?
TIA,
Larry Woods 10 8696
Larry, VB.NET If statement is inconsistent with standard?
There is nothing wrong with the If statement, its the expressions that you
are trying to evaluate!
The regular division operator "/" does not cause a Divide by Zero Exception!
It returns a value based on the IEEE 754 arithmetic. In other words it
returns Nan, Positive Infinity or Negative Infinity.
You need to use the integer division operator "\" to get a division by zero
exception. http://msdn.microsoft.com/library/de...Spec11_5_4.asp
If your example, the regular division operator returns Infinite. Infinity
does not equal 7, so it checks if i = 0, which it does.
the "If" WILL cause an exception...natually: j=j/i
This causes an Overflow Exception, as Infinity will not fit in a 32 bit
integer value. It does not cause a Divide By Zero exception!
Check out the Nan, PositiveInfinity, and NegativeInfinity fields of the
Single & Double classes. Also check out the IsNan, IsInfinity,
IsPositiveInfinity, and IsNegativeInfinity methods of the Single & Double
classes.
Hope this helps
Jay
"Larry Woods" <la***@lwoods.com> wrote in message
news:u6**************@tk2msftngp13.phx.gbl... Look at this code:
Dim i As Integer = 0 Dim j As Integer = 5 If (j / i = 7) Or i = 0 Then MessageBox.Show("In If") Else MessageBox.Show("In Else") End If
I would have expected to get an error thrown in the execution of the "If" statement since the division operation will cause a "divide by zero" exception. But this doesn't happen. The following statement, added before the "If" WILL cause an exception...natually:
j=j/i
What rule states that an If statement is "exempt" from errors? Or, better yet, what am I missing?
TIA,
Larry Woods
It is because the result of 1/0 is infinity and that does not equal 7.
MessageBox.Show(j / i) displays Infinity
--
Regards - One Handed Man
Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
"Larry Woods" <la***@lwoods.com> wrote in message
news:u6**************@tk2msftngp13.phx.gbl... Look at this code:
Dim i As Integer = 0 Dim j As Integer = 5 If (j / i = 7) Or i = 0 Then MessageBox.Show("In If") Else MessageBox.Show("In Else") End If
I would have expected to get an error thrown in the execution of the "If" statement since the division operation will cause a "divide by zero" exception. But this doesn't happen. The following statement, added before the "If" WILL cause an exception...natually:
j=j/i
What rule states that an If statement is "exempt" from errors? Or, better yet, what am I missing?
TIA,
Larry Woods
Darn! I would have sworn that I tested that.....
Thanks, guys!
Larry
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:uK**************@tk2msftngp13.phx.gbl... Larry, VB.NET If statement is inconsistent with standard? There is nothing wrong with the If statement, its the expressions that you are trying to evaluate!
The regular division operator "/" does not cause a Divide by Zero
Exception! It returns a value based on the IEEE 754 arithmetic. In other words it returns Nan, Positive Infinity or Negative Infinity.
You need to use the integer division operator "\" to get a division by
zero exception.
http://msdn.microsoft.com/library/de...Spec11_5_4.asp If your example, the regular division operator returns Infinite. Infinity does not equal 7, so it checks if i = 0, which it does.
the "If" WILL cause an exception...natually: j=j/i
This causes an Overflow Exception, as Infinity will not fit in a 32 bit integer value. It does not cause a Divide By Zero exception!
Check out the Nan, PositiveInfinity, and NegativeInfinity fields of the Single & Double classes. Also check out the IsNan, IsInfinity, IsPositiveInfinity, and IsNegativeInfinity methods of the Single & Double classes.
Hope this helps Jay
"Larry Woods" <la***@lwoods.com> wrote in message news:u6**************@tk2msftngp13.phx.gbl... Look at this code:
Dim i As Integer = 0 Dim j As Integer = 5 If (j / i = 7) Or i = 0 Then MessageBox.Show("In If") Else MessageBox.Show("In Else") End If
I would have expected to get an error thrown in the execution of the
"If" statement since the division operation will cause a "divide by zero" exception. But this doesn't happen. The following statement, added
before the "If" WILL cause an exception...natually:
j=j/i
What rule states that an If statement is "exempt" from errors? Or,
better yet, what am I missing?
TIA,
Larry Woods
In article <u6**************@tk2msftngp13.phx.gbl>, la***@lwoods.com
says... Look at this code:
Dim i As Integer = 0 Dim j As Integer = 5 If (j / i = 7) Or i = 0 Then MessageBox.Show("In If") Else MessageBox.Show("In Else") End If
I would have expected to get an error thrown in the execution of the "If" statement since the division operation will cause a "divide by zero" exception. But this doesn't happen. The following statement, added before the "If" WILL cause an exception...natually:
j=j/i
What rule states that an If statement is "exempt" from errors? Or, better yet, what am I missing?
First, you should always have:
Option Strict On
Option Explicit On
This will help you catch small type conversions that can happen in the
background without you knowing it. In the second case (j=j/i), without
Option Strict On, VB is doing a conversion behind-the-scenes and causing
an overflow exception.
Check the VB.NET docs for the division operator:
ms-help://MS.NETFrameworkSDK/vbls7/html/vblrfVBSpec11_5_4.htm
There's two different ones. The normal one ("/") and the integer
division operator ("\"). You're using integers with the normal one, so
VB.NET converts j and i to doubles and does the division. According to
the specs: "The quotient is computed according to the rules of IEEE 754
arithmetic".
After defining i and j try adding:
Dim k as double = i / j
And check out the value of k in the watch window.
--
Patrick Steele
Microsoft .NET MVP http://weblogs.asp.net/psteele
"Larry Woods" <la***@lwoods.com> schrieb Look at this code:
Dim i As Integer = 0 Dim j As Integer = 5 If (j / i = 7) Or i = 0 Then MessageBox.Show("In If") Else MessageBox.Show("In Else") End If
I would have expected to get an error thrown in the execution of the "If" statement since the division operation will cause a "divide by zero" exception. But this doesn't happen. The following statement, added before the "If" WILL cause an exception...natually:
j=j/i
What rule states that an If statement is "exempt" from errors? Or, better yet, what am I missing?
Floating point divisions by zero don't result in an exception. The Result is
"infinity" and can be stored in a floating point variable.
Integer divisions do throw an exception.
j / i is a floating point division, because "/" is used (instead of "\") for
integer divisions. Therefore, j and i are converted to floats before
division and afterwards the result (infinity) is assigned to j. The
assignment leads to an exception because infinty can not be assigned to an
Integer variable.
The expression used with the If statement does not result in an exception,
becasue it doesn't have to be assigned to an Integer variable.
--
Armin
Larry,
Interesting, I have not your answer I did some test, they seem to explain
something to me but like you I am curious about the deeper rules behind
this.
\\\\\
Dim i As Integer = 0
Dim j As Integer = 5
If (j / i) = System.Double.PositiveInfinity Then
MessageBox.Show("In If")
Else
MessageBox.Show("In Else")
End If
////
Results in "In if", it looks if it is placed in a double before the
evaluating.
I am curious too where to find this in the documentation.
I hope we get a lot of answers, maybe we are only dummies.
Cor
Cor,
You can find the info spread across, the Operators Section & the Conversions
section of the Visual Basic .NET Language specifications. http://msdn.microsoft.com/library/de...rfVBSpec11.asp http://msdn.microsoft.com/library/de...VBSpec11_5.asp http://msdn.microsoft.com/library/de...rfVBSpec10.asp
Not sure if there is a 'easier to read' web page available or not.
Hope this helps
Jay
"Cor" <no*@non.com> wrote in message
news:3f***********************@reader20.wxs.nl... Larry, Interesting, I have not your answer I did some test, they seem to explain something to me but like you I am curious about the deeper rules behind this. \\\\\ Dim i As Integer = 0 Dim j As Integer = 5 If (j / i) = System.Double.PositiveInfinity Then MessageBox.Show("In If") Else MessageBox.Show("In Else") End If //// Results in "In if", it looks if it is placed in a double before the evaluating. I am curious too where to find this in the documentation. I hope we get a lot of answers, maybe we are only dummies. Cor
Jay,
I am making a message to Patrick.
I write in that that I am a dummy.
I don't know if in English is to that proverb about salt and wounds
:-)
Cor
because in if statement you are checking a value for true
or false.the statement is wrong so it evaluates the 'OR'
Condition.Division operation will not take place.
when you add j=j/i you are assigining the resulting value
of j/i to j. in this case exception is thrown.
hope my explanation is right.any comments? -----Original Message----- Look at this code:
Dim i As Integer = 0 Dim j As Integer = 5 If (j / i = 7) Or i = 0 Then MessageBox.Show("In If") Else MessageBox.Show("In Else") End If
I would have expected to get an error thrown in the
execution of the "If"statement since the division operation will cause
a "divide by zero"exception. But this doesn't happen. The following
statement, added beforethe "If" WILL cause an exception...natually:
j=j/i
What rule states that an If statement is "exempt" from
errors? Or, betteryet, what am I missing?
TIA,
Larry Woods
.
Hi, you're slightly wrong.
The If statement is evaluated, but it's not wrong. A floating point division
is performed and anything divided by Zero is undefined. Undefined values are
allowed in floating point variables, and the / operator is a floating point
division. The result is not being assigned, so no exception is thrown.
The resulting value of the floating point division is Undefined, but
integers can not be undefined. So when trying to assign Undefined to an
integer, the exception is thrown.
--
HTH,
-- Tom Spink, Über Geek
Please respond to the newsgroup,
so all can benefit
"Maybe it's a game called 'Punish the User'"
"Nageswara" <ph********@hotmail.com> wrote in message
news:0a****************************@phx.gbl... because in if statement you are checking a value for true or false.the statement is wrong so it evaluates the 'OR' Condition.Division operation will not take place.
when you add j=j/i you are assigining the resulting value of j/i to j. in this case exception is thrown. hope my explanation is right.any comments?
-----Original Message----- Look at this code:
Dim i As Integer = 0 Dim j As Integer = 5 If (j / i = 7) Or i = 0 Then MessageBox.Show("In If") Else MessageBox.Show("In Else") End If
I would have expected to get an error thrown in the execution of the "If"statement since the division operation will cause a "divide by zero"exception. But this doesn't happen. The following statement, added beforethe "If" WILL cause an exception...natually:
j=j/i
What rule states that an If statement is "exempt" from errors? Or, betteryet, what am I missing?
TIA,
Larry Woods
. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: The Pistoleer |
last post by:
I have an inconsistent problem with some Internet pages when using Netscape
(7.1) that I cannot reproduce with IE nor locally. Below is an example
link.
There are several tables on the pages. ...
|
by: jimbo |
last post by:
Could someone comment on this code please.
1. Are the comments in it correct?
2. Why does sizeof(arr) not work consistently in C? In someFunction()
sizeof(arr) means sizeof(&arr) in main. ...
|
by: Peter Knörrich |
last post by:
Hello,
I've found another inconsistency, and looking through the
list archives I can find mentions of funky stuff like
print float('inf')
giving
Infanity
|
by: lovecreatesbea... |
last post by:
c-faq, 4.3:
The postfix ++ and -- operators essentially have higher precedence
than the prefix unary operators.
BSD Manual, OPERATOR(7):
Operator Associativity...
|
by: reycri |
last post by:
While the following is allowed:
if (a == b)
SomeFunction();
else
OtherFunction();
The following is not:
try
|
by: Francine.Neary |
last post by:
I am learning C, having fun with strings & pointers at the moment!
The following program is my solution to an exercise to take an input,
strip the first word, and output the rest. It works fine...
|
by: Rahul Babbar |
last post by:
Hi All,
When could be the possible reasons that could make a database
inconsistent?
I was told by somebody that it could become inconsistent if you "
force all the applications to a close on...
|
by: johnthawk |
last post by:
In the below code setting cell to inconsistent sets entire column inconsistent (renderer).However, I need a third state off | on | inconsistent . How can I change one path->cell?
Any help...
|
by: Kai-Uwe Bux |
last post by:
Juha Nieminen wrote:
I think your code violates the One-Definition-Rule. The template function
foo() is defined in two significantly different ways in the two files. As
far as I know, no...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |