千家信息网

java求余的操作介绍

发表于:2025-11-13 作者:千家信息网编辑
千家信息网最后更新 2025年11月13日,本篇内容主要讲解"java求余的操作介绍",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"java求余的操作介绍"吧!java 求余操作初阶java中也有余的
千家信息网最后更新 2025年11月13日java求余的操作介绍

本篇内容主要讲解"java求余的操作介绍",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"java求余的操作介绍"吧!

java 求余操作初阶

java中也有余的规范【jls-15.17.3】,废话不说,直接上代码,从中我们可以学到很多技巧:

例1:

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

相信大多数人都知道结果了:

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

java 求余操作中阶

我们知道,正数不仅仅有正整数还有负整数,那么负数的情况下,会出现什么变化呢?

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

能完全正确得到结果的就很少了吧?

          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)

为什么求余的结果是这样的呢?jls-15.17.3规范告诉我们:

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.

注意:求余的正负数给pidend(左边操作数)的符号位一致!

java 求余操作高阶

java求余操作不但支持整数还支持浮点数

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

相信很多人可以根据整型的规则,得出正确的结果

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

补充一下,浮点型的求余有一些特殊的规则:

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 求余操作骨灰级

学到这里,或许有人沾沾自喜,我都掌握了求余的所有规则,看来需要给你泼泼冷水:

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

这个程序会打印什么?有人经过繁琐复杂的算出一个结果:

1431655765 1431655766 1431655765

但其实,上述程序运行报错:

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

为什么数组会出现索引 -2?奇怪吧?要回答这个问题,我们必须要去看看Math.abs 的文档

/** * 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; }

特意说明,如果是Integer#MIN_VALUE,返回负数

到此,相信大家对"java求余的操作介绍"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

结果 整数 规则 负数 内容 程序 学习 支持 复杂 实用 更深 特殊 一致 繁琐 沾沾自喜 不仅仅 从中 代码 兴趣 冷水 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 海南数据库安全箱厂家现货 怎样设置服务器无法上网 中国球员技术档案数据库 科密a1数据库连接 阿富康网络技术有限公司 管理工具里服务器管理器 上海宜员网络技术有限公司 云南学软件开发的五年大专学费 tcp服务器断了会怎样 本地与服务器连接管理工具 添加服务器 深圳市最近有哪些网络技术公司 触摸屏程序组态软件开发 服务器的网络安全问题 网络安全答题活动方案 一列不重复的数据库 常用的数据库定义语句 靠谱的财务管理软件开发公司 服务器关机后灯还在闪 徐水软件开发有限公司 雷神网络安全模式 信息安全包括物理安全网络安全与 软件开发打版是什么意思 设计一个新的网络安全协议框架 贵州黔巨人网络技术有限公司招聘 石家庄智能软件开发服务技术规范 国家网络安全专家红山镇 数据库怎么提取照片 应用层的网络安全需求 软件开发 知识产权归费用
0