Java_基础_取余操作符 %
来源:互联网 发布:淘宝海外直营店真的吗 编辑:程序博客网 时间:2024/06/05 14:16
首先我们来看下 The Java® Language Specification 中官方对它的定义:
The binary % operator is said to yield the remainder of its operands from an implied division; the left-hand operand is the dividend and the right-hand operand is the divisor.
In C and C++, the remainder operator accepts only integral operands, but in the Java programming language, it also accepts floating-point operands.
The remainder operation for operands that are integers after binary numeric promotion (§5.6.2) produces a result value such that (a/b)*b+(a%b) is equal to a.
This identity holds even in the special case that the dividend is the negative integer of largest possible magnitude for its type and the divisor is -1 (the remainder is 0).
It follows from this rule that the result of the remainder operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive. Moreover, the magnitude of the result is always less than the magnitude of the divisor.
If the value of the divisor for an integer remainder operator is 0, then an ArithmeticException is thrown.
大体的意思就是说:Java中的%代表求余(注意:不是求模),在C和C++中,%仅支持整数操作,而在Java中,不仅接受整数操作,而且接受浮点数,此运算符计算的结果的符号与被除数的符号相同,如果除数为0,将抛出ArithmeticException。
例:
package deep;public class Test { public static void main(String[] args) { int a = 5 % 3; // 2 int b = 5 / 3; // 1 System.out.println("5%3 produces " + a + " (note that 5/3 produces " + b + ")"); int c = 5 % (-3); // 2 int d = 5 / (-3); // -1 System.out.println("5%(-3) produces " + c + " (note that 5/(-3) produces " + d + ")"); int e = (-5) % 3; // -2 int f = (-5) / 3; // -1 System.out.println("(-5)%3 produces " + e + " (note that (-5)/3 produces " + f + ")"); int g = (-5) % (-3); // -2 int h = (-5) / (-3); // 1 System.out.println("(-5)%(-3) produces " + g + " (note that (-5)/(-3) produces " + h + ")"); }}
运行结果:
5%3 produces 2 (note that 5/3 produces 1)
5%(-3) produces 2 (note that 5/(-3) produces -1)
(-5)%3 produces -2 (note that (-5)/3 produces -1)
(-5)%(-3) produces -2 (note that (-5)/(-3) produces 1)
再看看官方对浮点数的提及:
The result of a floating-point remainder operation as computed by the % operator is not the same as that produced by the remainder operation defined by IEEE 754. The IEEE 754 remainder operation computes the remainder from a rounding division, not a truncating division, and so its behavior is not analogous to that of the usual integer remainder operator. Instead, the Java programming language defines % on floating-point operations to behave in a manner analogous to that of the integer remainder operator; this may be compared with the C library function fmod. The IEEE 754 remainder operation may be computed by the library routine Math.IEEEremainder.
The result of a floating-point remainder operation is determined by the rules of IEEE 754 arithmetic:
- If either operand is NaN, the result is NaN.
- If the result is not NaN, the sign of the result equals the sign of
the dividend. - If the dividend is an infinity, or the divisor is a zero, or both,
the result is NaN. - If the dividend is finite and the divisor is an infinity, the result
equals the dividend. - If the dividend is a zero and the divisor is finite, the result
equals the dividend. - In the remaining cases, where neither an infinity, nor a zero, nor
NaN is involved, the floating-point remainder r from the division of
a dividend n by a divisor d is defined by the mathematical relation r
= n - (d · q) where q is an integer that is negative only if n/d is negative and positive only if n/d is positive, and whose magnitude is
as large as possible without exceeding the magnitude of the true
mathematical quotient of n and d.
Evaluation of a floating-point remainder operator % never throws a run-time exception, even if the right-hand operand is zero. Overflow, underflow, or loss of precision cannot occur.
例:
package deep;public class Test { public static void main(String[] args) { double a = 5.0 % 3.0; // 2.0 System.out.println("5.0%3.0 produces " + a); double b = 5.0 % (-3.0); // 2.0 System.out.println("5.0%(-3.0) produces " + b); double c = (-5.0) % 3.0; // -2.0 System.out.println("(-5.0)%3.0 produces " + c); double d = (-5.0) % (-3.0); // -2.0 System.out.println("(-5.0)%(-3.0) produces " + d); }}
运行结果:
5.0%3.0 produces 2.0
5.0%(-3.0) produces 2.0
(-5.0)%3.0 produces -2.0
(-5.0)%(-3.0) produces -2.0
- Java_基础_取余操作符 %
- Java_语法基础_移位运算符
- java_基础_包
- java_基础_杂
- java_基础_集合
- java_基础_泛型
- java_基础_异常
- java_基础_线程
- java_基础_类入门
- java_基础_类详解
- java_基础_绘图技术
- Java_基础_交换变量
- Java_基础_初始化顺序
- Java_基础_参数传递
- Java_数组操作_排序
- Java_数组操作_反转
- java_基础_二进制、位运算、移位运算符
- javascript取整 取余等操作
- Clusterware Startup Sequence--11gR2 Clusterware and Grid Home - What You Need to Know [ID 1053147.1]
- 【OpenCV】SIFT原理与源码分析:5.特征检测器 FeatureDetector
- JSON拼接
- [LeetCode] Permutations II
- 量子力学第八弹——散射问题
- Java_基础_取余操作符 %
- [Django]bulk_create 探究
- 强连通分量——kosaraju算法
- IP子网划分
- View的生命周期方法和Activity生命周期方法关系
- 【计算机视觉】SIFT中LoG和DoG比较
- ListView滑动过程中禁止加载图片
- UITextField
- MFC中关于m_pMainWnd = &dlg;处报错的解决方案,针对XX处未处理的异常:0xC0000005:写入位置0x00000020时发生访问冲突