java 數(shù)學(xué)計(jì)算的具體使用
java.lang.Math 庫(kù)提供了常用的數(shù)學(xué)計(jì)算工具
常量
final double E = 2.7182818284590452354; // 自然對(duì)數(shù)底數(shù) final double PI = 3.14159265358979323846; // 圓周率 final double DEGREES_TO_RADIANS = 0.017453292519943295; // 角度轉(zhuǎn)弧度 final double RADIANS_TO_DEGREES = 57.29577951308232; // 弧度轉(zhuǎn)角度
取整
- abs(x): 絕對(duì)值
- floor(x): 向下取整
- ceil(x): 向上取整
- round(x): 四舍五入,如果有兩個(gè)(x.5),返回較大的那個(gè)數(shù)
- rint(x): 最接近的整數(shù),如果有兩個(gè)(x.5),返回偶數(shù)
- floorDiv(x, y): 向下取整除法
- floorMod(x, y): java 默認(rèn)的取摸 % 得到的結(jié)果和 x 的符號(hào)相同,floorMod 和 y 的符號(hào)相同
double delta = 0.0000001; assertEquals(Math.abs(-6), 6); assertEquals(Math.floor(-6.2), -7, delta); // 向下取整 assertEquals(Math.floor(6.2), 6, delta); assertEquals(Math.floor(6.8), 6, delta); assertEquals(Math.ceil(-6.2), -6, delta); // 向上取整 assertEquals(Math.ceil(6.2), 7, delta); assertEquals(Math.ceil(6.8), 7, delta); assertEquals(Math.round(-6.2), -6, delta); // 四舍五入 assertEquals(Math.round(6.2), 6, delta); assertEquals(Math.round(6.8), 7, delta); assertEquals(Math.round(-6.5), -6, delta); assertEquals(Math.round(6.5), 7, delta); assertEquals(Math.rint(-6.2), -6, delta); // 最接近整數(shù),如果存在兩個(gè),返回偶數(shù) assertEquals(Math.rint(6.2), 6, delta); assertEquals(Math.rint(6.8), 7, delta); assertEquals(Math.rint(-6.5), -6, delta); assertEquals(Math.rint(6.5), 6, delta); assertEquals(Math.floorDiv(7, 3), 2); assertEquals(Math.floorDiv(-7, 3), -3); assertEquals(Math.floorMod(7, 3), 1); assertEquals(Math.floorMod(-7, -3), -1); assertEquals(Math.floorMod(-7, 3), 2); assertEquals(-7 % -3, -1); assertEquals(-7 % 3, -1);
三角函數(shù)
assertEquals(Math.sin(Math.PI / 2), 1.0, delta); assertEquals(Math.cos(Math.PI), -1, delta); assertEquals(Math.tan(Math.PI / 4), 1.0, delta); assertEquals(Math.asin(1), Math.PI / 2, delta); assertEquals(Math.acos(-1), Math.PI, delta); assertEquals(Math.atan(1), Math.PI / 4, delta);
指數(shù)對(duì)數(shù)
- pow(x, y): x^y,x 的 y 次方
- sqrt(x): √x,x 的平方根
- cbrt(x): 三次方根
- hypot(x, y): √(x² + y²)
- exp(x): e ^ x
- expm1(x): e ^ x - 1
- log(x): ln(x)
- log10: lg(x)
- log1p(x): ln(1+x)
assertEquals(Math.pow(3, 2), 9, delta); assertEquals(Math.pow(2, 3), 8, delta); assertEquals(Math.sqrt(4), 2, delta); assertEquals(Math.cbrt(27), 3, delta); assertEquals(Math.hypot(3, 4), 5, delta); // √(x² + y²) assertEquals(Math.exp(2.5), Math.pow(Math.E, 2.5), delta); // e ^ x assertEquals(Math.expm1(2), Math.exp(2) - 1, delta); // e ^ x - 1 assertEquals(Math.log(Math.exp(1.5)), 1.5, delta); // ln(x) assertEquals(Math.log10(1000), 3, delta); // lg(x) assertEquals(Math.log1p(Math.E - 1), 1, delta); // ln(1 + x)
雙曲函數(shù)
- sinh(x): (e ^ x - e ^ -x) / 2
- cosh(x): (e ^ x + e ^ -x) / 2
- tanh(x): sinh(x) / cosh(x)
assertEquals(Math.sinh(2), (Math.exp(2) - Math.exp(-2)) / 2, delta); // sinh(x) = (e ^ x - e ^ -x) / 2 assertEquals(Math.cosh(2), (Math.exp(2) + Math.exp(-2)) / 2, delta); // cosh(x) = (e ^ x + e ^ -x) / 2 assertEquals(Math.tanh(2), Math.sinh(2) / Math.cosh(2), delta); // tanh(x) = sinh(x) / cosh(x)
精確計(jì)算
普通的數(shù)值計(jì)算在溢出時(shí)是沒有感知的,比如 Long.MAX_VALUE + 1 將得到結(jié)果 Long.MIN_VALUE,為了解決這種不合理,Math 提供了一些輔助函數(shù),在結(jié)果溢出時(shí)將拋出異常
- addExact(x, y): 加法
- multiplyExact(x, y): 乘法
- decrementExact(x, y): 遞減
- incrementExact(x, y): 遞增
- negateExact(x, y): 相反數(shù)
- multiplyFull(x, y): 接受兩個(gè) int 返回一個(gè) long,防止溢出
- multiplyHigh(x, y): 返回兩個(gè) long 乘積的高 64 位
assertEquals(Long.MAX_VALUE + 1, Long.MIN_VALUE); // 溢出 assertThrows(ArithmeticException.class, () -> Math.addExact(Long.MAX_VALUE, 1)); // 加法溢出拋異常 assertThrows(ArithmeticException.class, () -> Math.multiplyExact(Long.MAX_VALUE, 2)); // 乘法 assertThrows(ArithmeticException.class, () -> Math.decrementExact(Long.MIN_VALUE)); // 遞減 assertThrows(ArithmeticException.class, () -> Math.incrementExact(Long.MAX_VALUE)); // 遞增 assertThrows(ArithmeticException.class, () -> Math.negateExact(Long.MIN_VALUE)); // 相反數(shù) assertEquals(Math.addExact(1, 2), 3); assertEquals(Math.multiplyExact(2, 3), 6); assertEquals(Math.incrementExact(6), 7); assertEquals(Math.decrementExact(6), 5); assertEquals(Math.negateExact(-6), 6); assertEquals(Math.multiplyFull(1, 2), 2); // 接受兩個(gè) int 返回一個(gè) long,防止溢出 assertEquals(Math.multiplyHigh(1, 2), 0); // 返回兩個(gè) long 乘積的高 64 位
浮點(diǎn)數(shù)
任意兩個(gè)浮點(diǎn)數(shù)之間都有無數(shù)個(gè)浮點(diǎn)數(shù),因此大部分浮點(diǎn)數(shù)是無法表示的,只能選取一個(gè)最接近的,java 提供了一些接口來獲取能表示的浮點(diǎn)數(shù)
System.out.println(Math.nextUp(1.1)); // 下一個(gè)浮點(diǎn)數(shù) System.out.println(Math.nextDown(1.1)); // 上一個(gè)浮點(diǎn)數(shù) System.out.println(Math.nextAfter(1.1, Double.POSITIVE_INFINITY)); // 下一個(gè)浮點(diǎn)數(shù) System.out.println(Math.nextAfter(1.1, Double.NEGATIVE_INFINITY)); // 上一個(gè)浮點(diǎn)數(shù)
隨機(jī)數(shù)
math 庫(kù)隨機(jī)數(shù)
System.out.println(Math.random()); // 0 ~ 1 之間的隨機(jī)數(shù)
java.lang.Random
Random 類提供了更豐富的隨機(jī)方法,可以返回各種不同類型的隨機(jī)數(shù)
Random r = new Random(); System.out.println(r.nextInt()); System.out.println(r.nextLong()); System.out.println(r.nextFloat()); System.out.println(r.nextDouble());
Random 還提供了流式 api
Random r = new Random(); List<Integer> li = r.ints().limit(10).boxed().map((x) -> Math.abs(x % 100)).collect(Collectors.toList()); System.out.println(li);
java.util.Random 是線程安全的,但是,跨線程的同時(shí)使用 java.util.Random 實(shí)例可能會(huì)遇到爭(zhēng)用,從而導(dǎo)致性能下降。在多線程設(shè)計(jì)中考慮使用java.util.concurrent.ThreadLocalRandom 代替 java.util.Random,ThreadLocalRandom 和 Random 擁有一致的接口
鏈接
Math 測(cè)試代碼: https://github.com/hatlonely/hellojava/blob/master/src/test/java/util/MathTest.java
隨機(jī)數(shù)測(cè)試代碼: https://github.com/hatlonely/hellojava/blob/master/src/test/java/util/RandomTest.java
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實(shí)現(xiàn)文件變化監(jiān)聽代碼實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)文件變化監(jiān)聽代碼實(shí)例,通過定時(shí)任務(wù),輪訓(xùn)查詢文件的最后修改時(shí)間,與上一次進(jìn)行對(duì)比,如果發(fā)生變化,則說明文件已經(jīng)修改,進(jìn)行重新加載或?qū)?yīng)的業(yè)務(wù)邏輯處理,需要的朋友可以參考下2024-01-01Spring?Boot異步線程間數(shù)據(jù)傳遞的四種方式
這篇文章主要為大家介紹了Spring?Boot異步線程間數(shù)據(jù)傳遞的四種方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01java中hashCode方法與equals方法的用法總結(jié)
總的來說,Java中的集合(Collection)有兩類,一類是List,再有一類是Set。前者集合內(nèi)的元素是有序的,元素可以重復(fù);后者元素?zé)o序,但元素不可重復(fù)2013-10-10java實(shí)現(xiàn)多線程之定時(shí)器任務(wù)
本篇文章主要介紹了java實(shí)現(xiàn)多線程之定時(shí)器任務(wù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Spring啟動(dòng)流程refresh()源碼深入解析
這篇文章主要給大家介紹了關(guān)于Spring啟動(dòng)流程refresh()源碼深入解析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Spring Security 在 Spring Boot 中的使用詳解【集中式】
這篇文章主要介紹了Spring Security 在 Spring Boot 中的使用【集中式】,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10圖文詳解如何將java編寫的程序轉(zhuǎn)為exe文件
我們寫的程序,要讓小伙伴打開即用,可以將java程序生成可執(zhí)行文件,下面這篇文章主要給大家介紹了關(guān)于一步步教你如何將java編寫的程序轉(zhuǎn)為exe文件的相關(guān)資料,需要的朋友可以參考下2023-01-01Spark學(xué)習(xí)筆記之Spark中的RDD的具體使用
這篇文章主要介紹了Spark學(xué)習(xí)筆記之Spark中的RDD的具體使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Java開發(fā)環(huán)境不再需要配置classpath問題
這篇文章主要介紹了Java開發(fā)環(huán)境不再需要配置classpath問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12