14.7.2 Division operator

本文详细介绍了C#中整数除法、浮点数除法及十进制数除法的操作符重载实现方式,并解释了不同情况下的运算规则、异常处理及结果取舍方法。
For an operation of the form x / y, binary operator overload resolution (?4.
2.4) is applied to select a
specific operator implementation. The operands are converted to the
parameter types of the selected
operator, and the type of the result is the return type of the operator.
The predefined division operators are listed below. The operators all
compute the quotient of x and y.
?Integer division:
int operator /(int x, int y);
uint operator /(uint x, uint y);
long operator /(long x, long y);
ulong operator /(ulong x, ulong y);
If the value of the right operand is zero, a System.DivideByZeroException is
thrown.
The division rounds the result towards zero, and the absolute value of the
result is the largest possible
integer that is less than the absolute value of the quotient of the two
operands. The result is zero or
positive when the two operands have the same sign and zero or negative when
the two operands have
opposite signs.
If the left operand is the maximum negative int or long value and the right
operand is ?1, an overflow
occurs. In a checked context, this causes a System.ArithmeticException (or
a subclass thereof)
to be thrown. In an unchecked context, it is implementation-defined as to
whether a
System.ArithmeticException (or a subclass thereof) is thrown or the
overflow goes unreported
with the resulting value being that of the left operand.
?Floating-point division:
float operator /(float x, float y);
double operator /(double x, double y);
The quotient is computed according to the rules of IEC 60559 arithmetic.
The following table lists the
results of all possible combinations of nonzero finite values, zeros,
infinities, and NaN?s. In the table, x
C# LANGUAGE SPECIFICATION
158
and y are positive finite values. z is the result of x / y. If the result
is too large for the destination type,
z is infinity. If the result is too small for the destination type, z is
zero.

     +y   -y   +0   -0   +8   -8   NaN
+x   +z   -z   +8   -8   +0   -0   NaN
-x   -z   +z   -8   +8   -0   +0   NaN
+0   +0   -0   NaN  NaN  +0   -0   NaN
-0   -0   +0   NaN  NaN  -0   +0   NaN
+8   +8   -8   +8   -8   NaN  NaN  NaN
-8   -8   +8   -8   +8   NaN  NaN  NaN
NaN  NaN  NaN  NaN  NaN  NaN  NaN  NaN

?Decimal division:
decimal operator /(decimal x, decimal y);
If the value of the right operand is zero, a System.DivideByZeroException
is thrown. If the
resulting value is too large to represent in the decimal format, a
System.OverflowException is
thrown. If the result value is too small to represent in the decimal
format, the result is zero. The scale
of the result, before any rounding, is the smallest scale that will
preserve a result equal to the exact
result.
Decimal division is equivalent to using the division operator of type
System.Decimal.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值