前端突然报了integer overflow错误,int类型溢出也就是数字超过了int类型,一看很懵逼,查看后台日期发现是在Math.toIntExact()方法报错

那么我们看下方法内部代码:

 /**
* Returns the value of the {@code long} argument;
* throwing an exception if the value overflows an {@code int}.
*
* @param value the long value
* @return the argument as an int
* @throws ArithmeticException if the {@code argument} overflows an int
* @since 1.8
*/
public static int toIntExact(long value) {
if ((int)value != value) {
throw new ArithmeticException("integer overflow");
}
return (int)value;
}

从代码中看出,它对参数value值进行了验证,如果强转为int仍然与原值相等说明没有超过int的值范围,否则抛出异常:integer overflow

我们在看看Math的其他方法,既然我们需要加减乘除,Math类肯定有现成的也会支持long类型的

这里只截出一部分,我们要用到的,这些都是1.8才有的方法

long类型的加减乘除运算:

加法:支持 int 和long的参数

   */
public static int addExact(int x, int y) {
int r = x + y;
// HD 2-12 Overflow iff both arguments have the opposite sign of the result
if (((x ^ r) & (y ^ r)) < 0) {
throw new ArithmeticException("integer overflow");
}
return r;
} /**
* Returns the sum of its arguments,
* throwing an exception if the result overflows a {@code long}.
*
* @param x the first value
* @param y the second value
* @return the result
* @throws ArithmeticException if the result overflows a long
* @since 1.8
*/
public static long addExact(long x, long y) {
long r = x + y;
// HD 2-12 Overflow iff both arguments have the opposite sign of the result
if (((x ^ r) & (y ^ r)) < 0) {
throw new ArithmeticException("long overflow");
}

减法运算:

  public static int subtractExact(int x, int y) {
int r = x - y;
// HD 2-12 Overflow iff the arguments have different signs and
// the sign of the result is different than the sign of x
if (((x ^ y) & (x ^ r)) < 0) {
throw new ArithmeticException("integer overflow");
}
return r;
} /**
* Returns the difference of the arguments,
* throwing an exception if the result overflows a {@code long}.
*
* @param x the first value
* @param y the second value to subtract from the first
* @return the result
* @throws ArithmeticException if the result overflows a long
* @since 1.8
*/
public static long subtractExact(long x, long y) {
long r = x - y;
// HD 2-12 Overflow iff the arguments have different signs and
// the sign of the result is different than the sign of x
if (((x ^ y) & (x ^ r)) < 0) {
throw new ArithmeticException("long overflow");
}

乘法:

 public static int multiplyExact(int x, int y) {
long r = (long)x * (long)y;
if ((int)r != r) {
throw new ArithmeticException("integer overflow");
}
return (int)r;
} /**
* Returns the product of the arguments,
* throwing an exception if the result overflows a {@code long}.
*
* @param x the first value
* @param y the second value
* @return the result
* @throws ArithmeticException if the result overflows a long
* @since 1.8
*/
public static long multiplyExact(long x, long y) {
long r = x * y;
long ax = Math.abs(x);
long ay = Math.abs(y);
if (((ax | ay) >>> 31 != 0)) {
// Some bits greater than 2^31 that might cause overflow
// Check the result using the divide operator
// and check for the special case of Long.MIN_VALUE * -1
if (((y != 0) && (r / y != x)) ||
(x == Long.MIN_VALUE && y == -1)) {
throw new ArithmeticException("long overflow");
}
}
return r;
}

除法:除法时乡下取整的
 public static int floorDiv(int x, int y) {
int r = x / y;
// if the signs are different and modulo not zero, round down
if ((x ^ y) < 0 && (r * y != x)) {
r--;
}
return r;
} public static long floorDiv(long x, long y) {
long r = x / y;
// if the signs are different and modulo not zero, round down
if ((x ^ y) < 0 && (r * y != x)) {
r--;
}
return r;
}

最新文章

  1. [.NET] 利用 async &amp; await 进行异步 IO 操作
  2. 1-MySQL命令行
  3. pinpoint 安装部署
  4. 让你脱离google不能访问的烦恼
  5. js 自动下载函数
  6. Python脚本控制的WebDriver 常用操作 &lt;十六&gt; 处理对话框
  7. Codeforces Round #322 (Div. 2) C. Developing Skills 优先队列
  8. 《UNIX环境高级编程》笔记--文件共享
  9. 安装 zabbix 时遇到的一个问题
  10. linux学习(十一)用户和用户组管理
  11. 使用生成器把Kafka写入速度提高1000倍
  12. 18 Loader代码案例
  13. log.go 源码阅读
  14. 关于RedHat Linux无法使用yum命令安装gcc-c++问题
  15. WIN SERVER 2012 自启动tomcat
  16. HTML基础总结
  17. SQL _ Create Procedure
  18. Space Ant
  19. Centos7网络正常,但使用yum提示安装源无法连接
  20. OI回忆录第一章 逐梦之始

热门文章

  1. replace|同时替换
  2. 信息检索盛会 微软“领衔主演”——记ACM SIGIR 2013信息检索国际会议
  3. android采用MVP完整漫画APP、钉钉地图效果、功能完善的音乐播放器、仿QQ动态登录效果、触手app主页等源码
  4. webservice入门程序学习中经验总结
  5. Query对象与DOM对象之间的转换
  6. python读取配置文件报keyerror-文件路径不正确导致的错误
  7. a, b = a, a+b 和a=b b=a+b的区别
  8. POJ 2226 Muddy Fields 二分图(难点在于建图)
  9. 2020 倒计时 1 天,Python 工程师找工作更难了?
  10. 安装NSQ