In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to achieve modular operation in Java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Java% (Modulus Operation) Modulus Operation of Java
1. Realization algorithm
Public static double ramainder (double dividend, double dividor) {return dividend-dividend / dividor * dividor;}
2.java 's modular operation support types: character type (naturally excluding negative numbers), byte type, short integer type, integer type, long integer type, single-precision floating-point type, double-precision floating-point type.
The Modulus Operation of C++
Algorithm is the same as above
The supported types of modular operations are character type (excluding negative numbers), byte type, short integer type, integer type, long integer type, Boolean type (divisor cannot be false, because in C++, true represents 1, false represents 0, and natural divisor cannot be 0;).
Note: parity judgment
Take a look at a piece of code first:
Public static void main (String [] args) {int arrs [] = {2,1,0,-1,-2}; for (int I = 0; I
< arrs.length; i++) { System.out.println(arrs[i] + (arrs[i] % 2 == 1 ? "是奇数" : "是偶数")); } } 很显然是一段判断奇偶性的代码,但是结果可能大家都能看得出来. 2是偶数1是奇数0是偶数-1是偶数-2是偶数 为什么-1会是偶数呢? 原因就是取模运算的实现算法导致的. System.out.println(arrs[i] + (arrs[i] % 2 == 1 ? "是奇数" : "是偶数"));//修改为:System.out.println(arrs[i] + (arrs[i] % 2 == 0 ? "是偶数" : "是奇数")); 给我一个警示:在以后的奇偶判断使用中,使用偶数进行判断代替使用奇数. (同样的现象C++中也是一样) 编程语言中的%取模运算的本质探究公式:a % b = a - a / b * b(对于a为正数) 如: -10 % 3 =>-10-(- 10) / 3 * 3 =-10 + 9 =-1
The essence is that how many 3s can be added or subtracted by-10? how many 3s can be closest to zero?
10%-3 = > 10-10 / (- 3) * (- 3) = 10-9 = 1
The essence is 10 plus or minus how much-3 can be closest to 0, 10 must add three-3, which is equal to 1 close to 0.
-10%-3 = >-10-(- 10) / (- 3) * (- 3) =-10 + 9 =-1
The essence is how many-3 can be added or subtracted by-10-3 can be closest to 0-10-3 must be minus three-3, which equals-1 close to 0.
When an is negative:
Formula: a-(int) a / b * b
Such as:
-10.5% 3 =-10.5-(- 10) / 3 * 3 =-10.5 + 9 =-1.5 above is all the content of this article "how to implement Modular Operation in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.