VB.NET中的除法運算符有兩個:/(浮點除法)、\(整數(shù)除法)
C#中的除法運算符只有一個:/(除法)
VB.NET中的除法運算符與C#中的除法運算符存在很大的差異,使用時注意區(qū)分。
關于C#中的除法運算符的介紹(摘自MSDN):
/(除法): 將兩個數(shù)相除并返回除數(shù)和被除數(shù)的數(shù)據(jù)類型中精度高的數(shù)據(jù)類型。
執(zhí)行除法前,編譯器會將除數(shù)和被除數(shù)的數(shù)據(jù)類型統(tǒng)一成兩者類型中精度高的數(shù)據(jù)類型。返回的運算結(jié)果的類型也是兩者類型中精度高的數(shù)據(jù)類型。例如:兩個整數(shù)相除的結(jié)果始終為一個整數(shù)。 一個整數(shù)和一個Double型相除,返回結(jié)果為Double型。但是需要注意一點,Double和Decimal不能直接進行算術運算,必須先顯式的將兩者類型進行統(tǒng)一,之后才能進行運算,Double和Decimal不能直接進行運算的原因是兩者之間不能進行隱式的類型轉(zhuǎn)換。
除法運算符 (/) 用第二個操作數(shù)除第一個操作數(shù)。所有數(shù)值類型都具有預定義的除法運算符。
using System;
class MainClass
{
static void Main()
{
Console.WriteLine(-5/2);
Console.WriteLine(-5.0/2);
}}
關于VB.NET中的除法運算符的介紹(摘自MSDN):
/(浮點除法):將兩個數(shù)相除并返回以浮點數(shù)表示的結(jié)果。
所得結(jié)果的數(shù)據(jù)類型取決于操作數(shù)的類型。 下表顯示如何確定結(jié)果的數(shù)據(jù)類型。
操作數(shù)數(shù)據(jù)類型 | 結(jié)果數(shù)據(jù)類型 |
兩個表達式都是整數(shù)數(shù)據(jù)類型(SByte、Byte、Short、UShort、Integer、UInteger、Long、ULong) | Double |
一個表達式為 Single 數(shù)據(jù)類型,而另一個表達式不為 Double | Single |
一個表達式為 Decimal 數(shù)據(jù)類型,而另一個表達式不為 Single 或 Double | Decimal |
任一表達式為 Double 數(shù)據(jù)類型 | Double |
執(zhí)行除法之前,任何整數(shù)數(shù)值表達式都會被擴展為 Double。 如果將結(jié)果賦給整數(shù)數(shù)據(jù)類型,Visual Basic 會嘗試將結(jié)果從 Double 轉(zhuǎn)換成這種類型。 如果結(jié)果不適合該類型,會引發(fā)異常。如果除數(shù)或被除數(shù)計算結(jié)果等于 Nothing,則將其視為零。
\(整數(shù)除法):將兩個數(shù)相除并返回以整數(shù)形式表示的結(jié)果。
下表如何確定結(jié)果的數(shù)據(jù)類型。 請注意,此表是對稱的;對于給定的操作數(shù)數(shù)據(jù)類型組合,無論操作數(shù)的順序如何,結(jié)果數(shù)據(jù)類型都是相同的。
Boolean | SByte | Byte | Short | UShort | Integer | UInteger | Long | ULong | |
Boolean | Boolean | SByte | Short | Short | Integer | Integer | Long | Long | Long |
SByte | SByte | SByte | Short | Short | Integer | Integer | Long | Long | Long |
Byte | Short | Short | Byte | Short | UShort | Integer | UInteger | Long | ULong |
Short | Short | Short | Short | Short | Integer | Integer | Long | Long | Long |
UShort | Integer | Integer | UShort | Integer | UShort | Integer | UInteger | Long | ULong |
Integer | Integer | Integer | Integer | Integer | Integer | Integer | Long | Long | Long |
UInteger | Long | Long | UInteger | Long | UInteger | Long | UInteger | Long | ULong |
Long | Long | Long | Long | Long | Long | Long | Long | Long | Long |
ULong | Long | Long | ULong | Long | ULong | Long | ULong | Long | ULong |
如果 \ 運算符兩個操作數(shù)中的任何一個為 Decimal、Single 或 Double,則 Visual Basic 在運算前會嘗試將其轉(zhuǎn)換為 Long,并且運算的結(jié)果數(shù)據(jù)類型為 Long。如果 Option Strict 為 On,將產(chǎn)生編譯器錯誤。 如果 Option Strict 為 Off,若值超出 Long 數(shù)據(jù)類型 (Visual Basic) 的范圍,則可能會產(chǎn)生 OverflowException。 轉(zhuǎn)換為 Long 也服從“四舍六入五成雙”。如果除數(shù)或被除數(shù)計算結(jié)果等于 Nothing,則將其視為零。
一、“/”運算符:
C#中默認的“/”是取整除法(不四舍五入)。譬如C#中1/2默認返回一個整數(shù)0.
但是VB.NET中“/”返回一個Double類型的數(shù)值(相當于C#中1.0/2的效果)。
在做這類運算時候,VB.NET會把被除數(shù)和除數(shù)提升到Double類型,因此1/0在VB.NET不是錯誤(因為這個表達式被解析成:1/CDbl(0))。結(jié)果CDbl(0)是一個接近于0的,但不是等于0的浮點數(shù)(可以理解為“正無窮小”),那么結(jié)果是“正無窮大”。但C#在做"/"時候,由于兩個都是整數(shù),因此直接做整數(shù)相除,拋出異常(如果嘗試在C#中1.0/0,可以得到和VB.NET中一樣的結(jié)論)。
二、“\”運算符:
這是VB.NET獨有的,和C#的“/”一致。值得注意的是如果雙方有一個浮點數(shù),取整結(jié)果默認轉(zhuǎn)化為Long(Int64)。此時1\0會發(fā)生錯誤。
三、取余運算:
C#中是“%”,VB.NET是Mod,類型按照實際結(jié)果決定。