深入学习java源码之Math.addExact()与 Math.multiplyExact()

^运算符

或的运算符,其运算规则是:

两个操作数的位中,相同则结果为0,不同则结果为1。

int i = 15, j = 2

运行结果是:i ^ j = 13.

分析上面程序,i=15转成二进制是1111,j=2转成二进制是0010,根据异或的运算规则得到的是1101,转成十进制就是13.

K+1个数,其中有2k个相同,需要找出不相同的那个数,比如:2、3、4、4、3、5、6、6、5。

int[] array = {2,3,4,4,3,5,6,6,5};int v = 0;for (int i = 0;i < array.length;i++) {v ^= array[i];System.out.println("只出现一次的数是:" + v);}只出现一次的数是2.

基本数据类型的加减乘除运算

加法

 int a = 10;long b = 35;float c = 36.95f;double d = 18.04;

减法

long a = 300;
int b = 18;
float c = 128.7f;
double d = 53.31;float jian = (float)(a - b - c - (double)d);

乘法

int a = 1;
long b = 1000000;
float c = 0.000001f;
double d = 99.99;

除法

int a = 1000;
long b = 1000000;
float c = 0.001f;
double d = 99.99;float chu = (float)((double)d/(a/b/c));
Modifier and Type Method and Description
static double abs(double a)

返回值为 double绝对值。

static float abs(float a)

返回 float值的绝对值。

static int abs(int a)

返回值为 int绝对值。

static long abs(long a)

返回值为 long绝对值。

static int addExact(int x, int y)

返回其参数的总和,如果结果溢出int,则抛出 int

static long addExact(long x, long y)

返回其参数的总和,如果结果溢出long,则抛出 long

static int decrementExact(int a)

返回一个递减1的参数,如果结果溢出int,则 int

static long decrementExact(long a)

将返回的参数递减1,如果结果溢出long,则 long

static double floor(double a)

返回小于或等于参数的最大(最接近正无穷大) double值,等于一个数学整数。

static int floorDiv(int x, int y)

返回小于或等于代数商的最大(最接近正无穷大) int值。

static long floorDiv(long x, long y)

返回小于或等于代数商的最大(最接近正无穷大) long值。

static int floorMod(int x, int y)

返回 int参数的底部模数。

static long floorMod(long x, long y)

返回 long参数的底模数。

static int incrementExact(int a)

返回自变量1,如果结果溢出int,则 int

static long incrementExact(long a)

返回一个增加1的参数,如果结果溢出long,则 long

static int multiplyExact(int x, int y)

返回参数的乘积,如果结果溢出int,则抛出 int

static long multiplyExact(long x, long y)

返回参数的乘积,如果结果溢出long,则抛出 long

static int negateExact(int a)

返回参数的否定,如果结果溢出int,则 int

static long negateExact(long a)

返回参数的否定,如果结果溢出long,则 long

static long round(double a)

返回参数中最接近的 long ,其中 long四舍五入为正无穷大。

static int round(float a)

返回参数中最接近的 int ,其中 int四舍五入为正无穷大。

static int subtractExact(int x, int y)

返回参数的差异,如果结果溢出int,则抛出 int

static long subtractExact(long x, long y)

返回参数的差异,如果结果溢出long,则抛出 long

static int toIntExact(long value)

返回long参数的值; 如果值溢出int,则int

java源码

public final class Math {private Math() {}public static int round(float a) {int intBits = Float.floatToRawIntBits(a);int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)>> (FloatConsts.SIGNIFICAND_WIDTH - 1);int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2+ FloatConsts.EXP_BIAS) - biasedExp;if ((shift & -32) == 0) { // shift >= 0 && shift < 32// a is a finite number such that pow(2,-32) <= ulp(a) < 1int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)| (FloatConsts.SIGNIF_BIT_MASK + 1));if (intBits < 0) {r = -r;}// In the comments below each Java expression evaluates to the value// the corresponding mathematical expression:// (r) evaluates to a / ulp(a)// (r >> shift) evaluates to floor(a * 2)// ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)// (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)return ((r >> shift) + 1) >> 1;} else {// a is either// - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2// - a finite number with ulp(a) >= 1 and hence a is a mathematical integer// - an infinity or NaNreturn (int) a;}}   public static long round(double a) {long longBits = Double.doubleToRawLongBits(a);long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)>> (DoubleConsts.SIGNIFICAND_WIDTH - 1);long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2+ DoubleConsts.EXP_BIAS) - biasedExp;if ((shift & -64) == 0) { // shift >= 0 && shift < 64// a is a finite number such that pow(2,-64) <= ulp(a) < 1long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)| (DoubleConsts.SIGNIF_BIT_MASK + 1));if (longBits < 0) {r = -r;}// In the comments below each Java expression evaluates to the value// the corresponding mathematical expression:// (r) evaluates to a / ulp(a)// (r >> shift) evaluates to floor(a * 2)// ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)// (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)return ((r >> shift) + 1) >> 1;} else {// a is either// - a finite number with abs(a) < exp(2,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2// - a finite number with ulp(a) >= 1 and hence a is a mathematical integer// - an infinity or NaNreturn (long) a;}}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 resultif (((x ^ r) & (y ^ r)) < 0) {throw new ArithmeticException("integer overflow");}return r;}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 resultif (((x ^ r) & (y ^ r)) < 0) {throw new ArithmeticException("long overflow");}return r;}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 xif (((x ^ y) & (x ^ r)) < 0) {throw new ArithmeticException("integer overflow");}return r;}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 xif (((x ^ y) & (x ^ r)) < 0) {throw new ArithmeticException("long overflow");}return r;}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;}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 * -1if (((y != 0) && (r / y != x)) ||(x == Long.MIN_VALUE && y == -1)) {throw new ArithmeticException("long overflow");}}return r;}public static int incrementExact(int a) {if (a == Integer.MAX_VALUE) {throw new ArithmeticException("integer overflow");}return a + 1;}public static long incrementExact(long a) {if (a == Long.MAX_VALUE) {throw new ArithmeticException("long overflow");}return a + 1L;}public static int decrementExact(int a) {if (a == Integer.MIN_VALUE) {throw new ArithmeticException("integer overflow");}return a - 1;}public static long decrementExact(long a) {if (a == Long.MIN_VALUE) {throw new ArithmeticException("long overflow");}return a - 1L;}public static int negateExact(int a) {if (a == Integer.MIN_VALUE) {throw new ArithmeticException("integer overflow");}return -a;}public static long negateExact(long a) {if (a == Long.MIN_VALUE) {throw new ArithmeticException("long overflow");}return -a;}public static int toIntExact(long value) {if ((int)value != value) {throw new ArithmeticException("integer overflow");}return (int)value;}    public static int abs(int a) {return (a < 0) ? -a : a;}public static long abs(long a) {return (a < 0) ? -a : a;}public static float abs(float a) {return (a <= 0.0F) ? 0.0F - a : a;}public static double abs(double a) {return (a <= 0.0D) ? 0.0D - a : a;}  public static long floorDiv(long x, long y) {long r = x / y;// if the signs are different and modulo not zero, round downif ((x ^ y) < 0 && (r * y != x)) {r--;}return r;}public static int floorDiv(int x, int y) {int r = x / y;// if the signs are different and modulo not zero, round downif ((x ^ y) < 0 && (r * y != x)) {r--;}return r;}public static int floorMod(int x, int y) {int r = x - floorDiv(x, y) * y;return r;}public static long floorMod(long x, long y) {return x - floorDiv(x, y) * y;}} 
public final class StrictMath {private StrictMath() {}public static int round(float a) {return Math.round(a);}public static long round(double a) {return Math.round(a);}public static int addExact(int x, int y) {return Math.addExact(x, y);}public static long addExact(long x, long y) {return Math.addExact(x, y);}public static int subtractExact(int x, int y) {return Math.subtractExact(x, y);}public static long subtractExact(long x, long y) {return Math.subtractExact(x, y);}public static int multiplyExact(int x, int y) {return Math.multiplyExact(x, y);}public static long multiplyExact(long x, long y) {return Math.multiplyExact(x, y);}public static int toIntExact(long value) {return Math.toIntExact(value);}public static int floorDiv(int x, int y) {return Math.floorDiv(x, y);}public static long floorDiv(long x, long y) {return Math.floorDiv(x, y);}public static int floorMod(int x, int y) {return Math.floorMod(x , y);}public static long floorMod(long x, long y) {return Math.floorMod(x, y);}public static int abs(int a) {return Math.abs(a);}public static long abs(long a) {return Math.abs(a);}public static float abs(float a) {return Math.abs(a);}public static double abs(double a) {return Math.abs(a);}}

深入学习java源码之Math.addExact()与 Math.multiplyExact()相关推荐

  1. Math源码java_深入学习java源码之Math.sin()与 Math.sqrt()

    深入学习java源码之Math.sin()与 Math.sqrt() native关键字 凡是一种语言,都希望是纯.比如解决某一个方案都喜欢就单单这个语言来写即可.Java平台有个用户和本地C代码进行 ...

  2. 深入学习java源码之Math.max()与 Math.min()

    深入学习java源码之Math.max()与 Math.min() java基本数据类型及自动转型 8种基本数据类型及其所占空间大小: 一.byte,占用一个字节,取值范围为 -128-127,默认是 ...

  3. 转载:深入学习java源码之Callable.call()与Future.get()

    原始链接:https://blog.csdn.net/qq_35029061/article/details/86750369 深入学习java源码之Callable.call()与Future.ge ...

  4. 深入学习java源码之 Arrays.sort()与Arrays.parallelPrefix()

    深入学习java源码之 Arrays.sort()与Arrays.parallelPrefix() Comparator接口 能对不同类型的对象进行排序(当然排序依据还是基本类型),也不用自己实现排序 ...

  5. 如何学习java源码

    刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心. 说到技术基础,我打个比方吧 ...

  6. java 源码学习,Java源码剖析34讲学习笔记~4

    详解 ThreadPoolExecutor 的参数含义及源码执行流程 前言 在阿里巴巴的开发者手册中针对线程池有如下说明: [强制]线程池不允许使用 Executors 去创建,而是通过 Thread ...

  7. Java源码学习笔记之lang包——包装类Integer.class

    前言:仅为学习所发现而记录. JDK 版本:1.8 同样的,在去除所有方法和静态变量之后,以下为核心内容. public final class Integer extends Number impl ...

  8. Java源码阅读学习后的浅析和感悟(JDK篇)(持续更新)

    目录 Java源码阅读学习后的浅析和感悟(JKD篇) - 为什么阅读源码 集合框架类 - 为什么会要引入集合 - 集合结构图(部分) ArrayList集合源码分析 - 扩容机制 - 关键方法解释(D ...

  9. 面试官系统精讲Java源码及大厂真题 - 01 开篇词:为什么学习本专栏

    01 开篇词:为什么学习本专栏 更新时间:2019-10-30 10:08:31 才能一旦让懒惰支配,它就一无可为. --克雷洛夫 不为了源码而读源码,只为了更好的实践 你好,我是文贺,Java 技术 ...

最新文章

  1. WPF中制作带中国农历的万年历
  2. Maximum Product Subarray
  3. Codeforces Round #757 (Div. 2)
  4. byteman_使用Byteman和JUnit进行故障注入
  5. CV方向介绍 | 基于自然语言的跨模态行人re-id的SOTA方法简述(上)
  6. Windows10 热点(WIFI)配置教程
  7. [转载] java构造函数初始化与执行顺序问题
  8. 华为设备MSTP配置命令
  9. 什么是开源网络情报?有什么特点?
  10. 商城项目02_环境搭建、安装VAGRANT、DOCKER、MYSQL、REDIS、从0搭建各个微服务项目、数据库初始化、安装NGINX
  11. 重装上阵两个人合体机器人_重装上阵多人机甲合体攻略 机甲怎么合体
  12. Android MediaRecorder录制视频详细步骤
  13. QQ音乐 vs 网易云音乐,用户体验哪家强?
  14. CRC16 校验函数
  15. .xin 域名信用验证问题集锦
  16. Ubuntu上软件安装
  17. 神奇的“TexturePacker”
  18. 超轻型飞机 - 蟋蟀,世界上最小的双引擎载人飞机!
  19. python程序设计基础与应用董付国电子版_Python 程序设计基础(董付国 著)完整版PDF_IT教程网...
  20. 俄被踢出SWIFT,地缘冲突推动区块链金融清算行业发展

热门文章

  1. Cookie跨域的问题
  2. H3C telnet配置要点
  3. 基于51单片机的矩阵计算器设计
  4. linux查看文件的特殊权限,linux文件的特殊权限及隐藏权限
  5. 设计模式-创建型模式(单例、简工、工方)
  6. 基于TCP的安卓与服务器交互开发
  7. 广州的11个辖区_人才吸引力排名,广州11个区哪家强?
  8. ZigBee的电子标签系统设计
  9. 怎么用HTML5制作万花筒,简易万花筒制作详细步骤 手工万花筒的做法图解
  10. [导入]2008张国立倾情打造《想爱都难》全30集