Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the operation of java congruence

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "java remainder operation introduction", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "java remainder operation introduction"!

Java residual operation initial order

Java also has more specifications [jls-15.17.3], nonsense, directly on the code, from which we can learn a lot of skills:

Example 1:

int a = 5%3; // 2int b = 5/3; // 1System.out.println("5%3 produces " + a +" (note that 5/3 produces " + b + ")");

Most of you know the result:

5%3 produces 2 (note that 5/3 produces 1)

Java remainder operation middle order

We know that positive numbers have negative integers as well as positive integers, so what happens when negative integers are present?

Example 2:

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

Very few people get the results exactly right, right?

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)

Why does the remainder result in this way? The jls-15.17.3 specification tells us:

The binary % operator is said to yield the remainder of its operands from an implied pision; the left-hand operand is the pidend and the right-hand operand is the pisor.It follows from this rule that the result of the remainder operation can be negative only if the pidend is negative, and can be positive only if the pidend is positive. Moreover, the magnitude of the result is always less than the magnitude of the pisor.

Note: The sign bits of the positive and negative numbers for the remainder to pidend(left operand) are consistent!

Java Complementary Operations Higher Order

Java complement operations support not only integers but also floating point numbers

class Test2 { 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); }}

I believe that many people can get the correct result according to the rules of integer

5.0%3.0 produces 2.05.0%(-3.0) produces 2.0(-5.0)%3.0 produces -2.0(-5.0)%(-3.0) produces -2.0

In addition, floating-point residuals have some special rules:

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 pision, not a truncating pision, 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 pidend.If the pidend is an infinity, or the pisor is a zero, or both, the result is NaN.If the pidend is finite and the pisor is an infinity, the result equals the pidend.If the pidend is a zero and the pisor is finite, the result equals the pidend.In the remaining cases, where neither an infinity, nor a zero, nor NaN is involved, the floating-point remainder r from the pision of a pidend n by a pisor 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.

Java remainder operation ashes level

Learn here, perhaps some people complacent, I have mastered all the rules of remainder, it seems that you need to pour cold water:

public static void main(String[] args) { final int MODULUS = 3; int[] histogram = new int[MODULUS]; // Iterate over all ints (Idiom from Puzzle 26) int i = Integer.MIN_VALUE; do { histogram[Math.abs(i) % MODULUS]++; } while (i++ != Integer.MAX_VALUE); for (int j = 0; j < MODULUS; j++) System.out.println(histogram[j] + " "); }

What does this program print? Someone calculated a result after complicated and complicated calculation:

1431655765 1431655766 1431655765

But in fact, the above program runs incorrectly:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2at com.java.puzzlers.ModTest.main(ModTest.java:11)

Why does the array have index-2? Strange, isn't it? To answer this question, we have to look at the Math.abs documentation.

/** * Returns the absolute value of an {@code int} value. * If the argument is not negative, the argument is returned. * If the argument is negative, the negation of the argument is returned. * *

Note that if the argument is equal to the value of * {@link Integer#MIN_VALUE}, the most negative representable * {@code int} value, the result is that same value, which is * negative. * * @param a the argument whose absolute value is to be determined * @return the absolute value of the argument. */ public static int abs(int a) { return (a < 0) ? -a : a; }

Specifically, if Integer#MIN_VALUE, return negative

At this point, I believe everyone has a deeper understanding of "java remainder operation introduction", may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report