Java_文法基礎_取餘操作符 %,java操作符

來源:互聯網
上載者:User

Java_文法基礎_取餘操作符 %,java操作符

首先我們來看下 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.