Java中數(shù)學(xué)相關(guān)類的使用教程
1.java.lang.Math
java.lang.Math
類包含用于執(zhí)行基本數(shù)學(xué)運(yùn)算的方法,如初等指數(shù)、對(duì)數(shù)、平方根和三角函數(shù)。類似這樣的工具類,其所有方法均為靜態(tài)方法,并且不會(huì)創(chuàng)建對(duì)象。
public static double abs(double a)
:返回 double 值的絕對(duì)值。
double d1 = Math.abs(-5); //d1的值為5 double d2 = Math.abs(5); //d2的值為5
public static double ceil(double a)
:返回大于等于參數(shù)的最小的整數(shù)。
double d1 = Math.ceil(3.3); //d1的值為 4.0 double d2 = Math.ceil(-3.3); //d2的值為 -3.0 double d3 = Math.ceil(5.1); //d3的值為 6.0
public static double floor(double a)
:返回小于等于參數(shù)最大的整數(shù)。
double d1 = Math.floor(3.3); //d1的值為3.0 double d2 = Math.floor(-3.3); //d2的值為-4.0 double d3 = Math.floor(5.1); //d3的值為 5.0
public static long round(double a)
:返回最接近參數(shù)的 long。(相當(dāng)于四舍五入方法)
long d1 = Math.round(5.5); //d1的值為6 long d2 = Math.round(5.4); //d2的值為5 long d3 = Math.round(-3.3); //d3的值為-3 long d4 = Math.round(-3.8); //d4的值為-4
- public static double pow(double a,double b):返回a的b冪次方法
- public static double sqrt(double a):返回a的平方根
- public static double random():返回[0,1)的隨機(jī)值
- public static final double PI:返回圓周率
- public static double max(double x, double y):返回x,y中的最大值
- public static double min(double x, double y):返回x,y中的最小值
- 其它:acos,asin,atan,cos,sin,tan 三角函數(shù)
double result = Math.pow(2,31); double sqrt = Math.sqrt(256); double rand = Math.random(); double pi = Math.PI;
2.java.math包
2.1 BigInteger
- Integer類作為int的包裝類,能存儲(chǔ)的最大整型值為231-1,Long類也是有限的,最大為263-1。如果要表示再大的整數(shù),不管是基本數(shù)據(jù)類型還是他們的包裝類都無(wú)能為力,更不用說(shuō)進(jìn)行運(yùn)算了。
- java.math包的BigInteger可以表示不可變的任意精度的整數(shù)。BigInteger 提供所有 Java 的基本整數(shù)操作符的對(duì)應(yīng)物,并提供 java.lang.Math 的所有相關(guān)方法。另外,BigInteger 還提供以下運(yùn)算:模算術(shù)、GCD 計(jì)算、質(zhì)數(shù)測(cè)試、素?cái)?shù)生成、位操作以及一些其他操作。
- 構(gòu)造器
- BigInteger(String val):根據(jù)字符串構(gòu)建BigInteger對(duì)象
- 方法
- public BigInteger abs():返回此 BigInteger 的絕對(duì)值的 BigInteger。
- BigInteger add(BigInteger val) :返回其值為 (this + val) 的 BigInteger
- BigInteger subtract(BigInteger val) :返回其值為 (this - val) 的 BigInteger
- BigInteger multiply(BigInteger val) :返回其值為 (this * val) 的 BigInteger
- BigInteger divide(BigInteger val) :返回其值為 (this / val) 的 BigInteger。整數(shù)相除只保留整數(shù)部分。
- BigInteger remainder(BigInteger val) :返回其值為 (this % val) 的 BigInteger。
- BigInteger[] divideAndRemainder(BigInteger val):返回包含 (this / val) 后跟 (this % val) 的兩個(gè) BigInteger 的數(shù)組。
- BigInteger pow(int exponent) :返回其值為 (this^exponent) 的 BigInteger。
@Test public void test01(){ //long bigNum = 123456789123456789123456789L; BigInteger b1 = new BigInteger("12345678912345678912345678"); BigInteger b2 = new BigInteger("78923456789123456789123456789"); //System.out.println("和:" + (b1+b2));//錯(cuò)誤的,無(wú)法直接使用+進(jìn)行求和 System.out.println("和:" + b1.add(b2)); System.out.println("減:" + b1.subtract(b2)); System.out.println("乘:" + b1.multiply(b2)); System.out.println("除:" + b2.divide(b1)); System.out.println("余:" + b2.remainder(b1)); }
2.2 BigDecimal
- 一般的Float類和Double類可以用來(lái)做科學(xué)計(jì)算或工程計(jì)算,但在商業(yè)計(jì)算中,要求數(shù)字精度比較高,故用到j(luò)ava.math.BigDecimal類。
- BigDecimal類支持不可變的、任意精度的有符號(hào)十進(jìn)制定點(diǎn)數(shù)。
- 構(gòu)造器
- public BigDecimal(double val)
- public BigDecimal(String val)
- 常用方法
- public BigDecimal add(BigDecimal augend)
- public BigDecimal subtract(BigDecimal subtrahend)
- public BigDecimal multiply(BigDecimal multiplicand)
- public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode):divisor是除數(shù),scale指明保留幾位小數(shù),roundingMode指明舍入模式(ROUNDUP :向上加1、ROUNDDOWN :直接舍去、ROUNDHALFUP:四舍五入)
@Test public void test03(){ BigInteger bi = new BigInteger("12433241123"); BigDecimal bd = new BigDecimal("12435.351"); BigDecimal bd2 = new BigDecimal("11"); System.out.println(bi); // System.out.println(bd.divide(bd2)); System.out.println(bd.divide(bd2, BigDecimal.ROUND_HALF_UP)); System.out.println(bd.divide(bd2, 15, BigDecimal.ROUND_HALF_UP)); }
2.3 java.util.Random
用于產(chǎn)生隨機(jī)數(shù)
- boolean nextBoolean():返回下一個(gè)偽隨機(jī)數(shù),它是取自此隨機(jī)數(shù)生成器序列的均勻分布的 boolean 值。
- void nextBytes(byte[] bytes):生成隨機(jī)字節(jié)并將其置于用戶提供的 byte 數(shù)組中。
- double nextDouble():返回下一個(gè)偽隨機(jī)數(shù),它是取自此隨機(jī)數(shù)生成器序列的、在 0.0 和 1.0 之間均勻分布的 double 值。
- float nextFloat():返回下一個(gè)偽隨機(jī)數(shù),它是取自此隨機(jī)數(shù)生成器序列的、在 0.0 和 1.0 之間均勻分布的 float 值。
- double nextGaussian():返回下一個(gè)偽隨機(jī)數(shù),它是取自此隨機(jī)數(shù)生成器序列的、呈高斯(“正態(tài)”)分布的 double 值,其平均值是 0.0,標(biāo)準(zhǔn)差是 1.0。
- int nextInt():返回下一個(gè)偽隨機(jī)數(shù),它是此隨機(jī)數(shù)生成器的序列中均勻分布的 int 值。
- int nextInt(int n):返回一個(gè)偽隨機(jī)數(shù),它是取自此隨機(jī)數(shù)生成器序列的、在 0(包括)和指定值(不包括)之間均勻分布的 int 值。
- long nextLong():返回下一個(gè)偽隨機(jī)數(shù),它是取自此隨機(jī)數(shù)生成器序列的均勻分布的 long 值。
@Test public void test04(){ Random r = new Random(); System.out.println("隨機(jī)整數(shù):" + r.nextInt()); System.out.println("隨機(jī)小數(shù):" + r.nextDouble()); System.out.println("隨機(jī)布爾值:" + r.nextBoolean()); }
附:更多java數(shù)學(xué)函數(shù)Math類實(shí)例
Math.abs(12.3); //12.3 返回這個(gè)數(shù)的絕對(duì)值 Math.abs(-12.3); //12.3 Math.copySign(1.23, -12.3); //-1.23,返回第一個(gè)參數(shù)的量值和第二個(gè)參數(shù)的符號(hào) Math.copySign(-12.3, 1.23); //12.3 Math.signum(x); //如果x大于0則返回1.0,小于0則返回-1.0,等于0則返回0 Math.signum(12.3); //1.0 Math.signum(-12.3); //-1.0 Math.signum(0); //0.0 //指數(shù) Math.exp(x); //e的x次冪 Math.expm1(x); //e的x次冪 - 1 Math.scalb(x, y); //x*(2的y次冪) Math.scalb(12.3, 3); //12.3*23 //取整 Math.ceil(12.3); //返回最近的且大于這個(gè)數(shù)的整數(shù)13.0 Math.ceil(-12.3); //-12.0 Math.floor(12.3); //返回最近的且小于這個(gè)數(shù)的整數(shù)12.0 Math.floor(-12.3); //-13.0 //x和y平方和的二次方根 Math.hypot(x, y); //√(x2+y2) //返回概述的二次方根 Math.sqrt(x); //√(x) x的二次方根 Math.sqrt(9); //3.0 Math.sqrt(16); //4.0 //返回該數(shù)的立方根 Math.cbrt(27.0); //3 Math.cbrt(-125.0); //-5 //對(duì)數(shù)函數(shù) Math.log(e); //1 以e為底的對(duì)數(shù) Math.log10(100); //10 以10為底的對(duì)數(shù) Math.log1p(x); //Ln(x+ 1) //返回較大值和較小值 Math.max(x, y); //返回x、y中較大的那個(gè)數(shù) Math.min(x, y); //返回x、y中較小的那個(gè)數(shù) //返回 x的y次冪 Math.pow(x, y); Math.pow(2, 3); //即23 即返回:8 //隨機(jī)返回[0,1)之間的無(wú)符號(hào)double值 Math.random(); //返回最接近這個(gè)數(shù)的整數(shù),如果剛好居中,則取偶數(shù) Math.rint(12.3); //12.0 Math.rint(-12.3); //-12.0 Math.rint(78.9); //79.0 Math.rint(-78.9); //-79.0 Math.rint(34.5); //34.0 Math.rint(35.5); //36.0 Math.round(12.3); //與rint相似,返回值為long //三角函數(shù) Math.sin(α); //sin(α)的值 Math.cos(α); //cos(α)的值 Math.tan(α); //tan(α)的值 //求角 Math.asin(x/z); //返回角度值[-π/2,π/2] arc sin(x/z) Math.acos(y/z); //返回角度值[0~π] arc cos(y/z) Math.atan(y/x); //返回角度值[-π/2,π/2] Math.atan2(y-y0, x-x0); //同上,返回經(jīng)過(guò)點(diǎn)(x,y)與原點(diǎn)的的直線和經(jīng)過(guò)點(diǎn)(x0,y0)與原點(diǎn)的直線之間所成的夾角 Math.sinh(x); //雙曲正弦函數(shù)sinh(x)=(exp(x) - exp(-x)) / 2.0; Math.cosh(x); //雙曲余弦函數(shù)cosh(x)=(exp(x) + exp(-x)) / 2.0; Math.tanh(x); //tanh(x) = sinh(x) / cosh(x); //角度弧度互換 Math.toDegrees(angrad); //角度轉(zhuǎn)換成弧度,返回:angrad * 180d / PI Math.toRadians(angdeg); //弧度轉(zhuǎn)換成角度,返回:angdeg / 180d * PI
總結(jié)
到此這篇關(guān)于Java中數(shù)學(xué)相關(guān)類使用的文章就介紹到這了,更多相關(guān)Java數(shù)學(xué)相關(guān)類內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot+Websocket實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)頁(yè)聊天功能代碼
本篇文章主要介紹了SpringBoot+Websocket實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)頁(yè)聊天功能代碼,具有一定的參考價(jià)值,有需要的可以了解一下2017-08-08Java實(shí)現(xiàn)刪除PDF中指定頁(yè)面
這篇文章主要為大家詳細(xì)介紹了如何使用一個(gè)免費(fèi)的國(guó)產(chǎn)Java庫(kù)來(lái)刪除PDF中的指定頁(yè)面或者刪除PDF中的空白頁(yè),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11MyBatis使用注解開發(fā)實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了MyBatis使用注解開發(fā)實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Spring boot創(chuàng)建自定義starter的完整步驟
這篇文章主要給大家介紹了關(guān)于Spring boot創(chuàng)建自定義starter的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Springmvc ajax跨域請(qǐng)求處理方法實(shí)例詳解
這篇文章主要介紹了Springmvc ajax跨域請(qǐng)求處理方法實(shí)例詳解,需要的朋友可以參考下2017-10-10SpringBoot整合Sa-Token實(shí)現(xiàn)?API?接口簽名安全校驗(yàn)功能
這篇文章主要介紹了SpringBoot整合Sa-Token實(shí)現(xiàn)?API?接口簽名安全校驗(yàn)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07使用JavaIO流和網(wǎng)絡(luò)制作一個(gè)簡(jiǎn)單的圖片爬蟲
這篇文章主要介紹了使用JavaIO流和網(wǎng)絡(luò)制作一個(gè)簡(jiǎn)單的圖片爬蟲,通過(guò)關(guān)鍵字爬取百度圖片,這個(gè)和我們使用搜索引擎搜索百度圖片是一樣的,只是通過(guò)爬蟲可以學(xué)習(xí)技術(shù)的使用,需要的朋友可以參考下2023-04-04