Java _ basic syntax _ remainder operator %, java Operator

Source: Internet
Author: User

Java _ basic syntax _ remainder operator %, java Operator

First, let's take a look at The official definition of 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.

This identity holds even in the special case that the dividend is the negative integer of largest possible magn=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 magnpointer of the result is always less than the magnpointer of the divisor.

If the value of the divisor for an integer remainder operator is 0, then an ArithmeticException is thrown.

In general, % in Java represents the remainder (Note: not the modulo). In C and C ++, % only supports integer operations, while in Java, this operator not only accepts integer operations, but also floating-point numbers. The calculated result is the same as the divisor. If the divisor is 0, ArithmeticException is thrown.

Example:

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 + ")");    }}

Running result:
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)

Let's take a look at the official reference to floating point numbers:

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
    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
    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 magnative is
    As large as possible without exceeding the magnqueue 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.

Example:

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);    }}

Running result:
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.