Java_ Syntax Basics _ take the remainder operator%

Source: Internet
Author: User

Tag: Take-up operator specification

First we look at the Java? Language specification in the official definition of it:

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 are 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 is integers after binary numeric promotion (§5.6.2) produces a result value suc h That (A/b) *b+ (a%b) is equal to a.

This identity holds even in the special case, the dividend is the negative integers of largest possible magnitude for I TS type and the divisor is-1 (the remainder is 0).

It follows from this rule, the result of the remainder operation can being 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 are 0, then an arithmeticexception is thrown.

The general meaning is that:% in Java for redundancy (note: not modulo), in C and C + +,% only supports integer operations, whereas in Java, not only the integer operation is accepted, but also the floating-point number is accepted, the result of this operator evaluates to the same symbol as the divisor, if the divisor is 0, Will throw arithmeticexception.

Cases:

Package deep; Public classTest { Public Static void Main(string[] args) {intA =5%3;//2        intb =5/3;//1System. out. println ("5%3 produces"+ A +"(Note that 5/3 produces"+ B +")");intc =5% (-3);//2        intD =5/ (-3);//-1System. out. println ("5% ( -3) produces"+ C +"(Note that 5/( -3) produces"+ D +")");intE = (-5) %3;//-2        intF = (-5) /3;//-1System. out. println ("( -5)%3 produces"+ E +"(Note that ( -5)/3 produces")+ F +")");intg = (-5) % (-3);//-2        intH = (-5) / (-3);//1System. out. println ("( -5)% ( -3) produces"+ G +"(Note that ( -5)/( -3) produces"+ H +")"); }}

Operation 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)

And look at the official mention of floating-point numbers:

The result of a floating-point remainder operation as computed by the% operator are not the same as, the produced by the R Emainder operation defined by IEEE 754. The IEEE 754 remainder operation computes the remainder from a rounding division, not a truncating division Havior 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 T He integer remainder operator; This is compared with the C library function Fmod. The IEEE 754 remainder operation is computed by the library routine Math.ieeeremainder.

The result of a floating-point remainder operation is determined by the rules of the IEEE 754 arithmetic:

    • If either operand is Nan, and the result is Nan.
    • If The result is a NaN, the sign of the result equals the sign of
      the dividend.
    • If The dividend is a infinity, or the divisor is a zero, or both, and
      the result is NaN.
    • The
    • If the dividend is finite and the divisor are 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 was defined by the mathematical relation R
      = n-(d q) where q is a integer that was negative only if n/d are negative and positive only if n/d are 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 Z Ero. Overflow, underflow, or loss of precision cannot occur.

Cases:

Package deep; Public classTest { Public Static void Main(string[] args) {DoubleA =5.0%3.0;//2.0System. out. println ("5.0%3.0 produces"+ a);Doubleb =5.0% (-3.0);//2.0System. out. println ("5.0% ( -3.0) produces"+ b);Doublec = (-5.0) %3.0;// -2.0System. out. println ("( -5.0)%3.0 produces"+ c);DoubleD = (-5.0) % (-3.0);// -2.0System. out. println ("( -5.0)% ( -3.0) produces"+ D); }}

Operation 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

Java_ Syntax Basics _ take the remainder operator%

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.