Java中數(shù)學相關類的使用教程
更新時間:2023年05月19日 10:59:24 作者:魚找水需要時間
Java是一種廣泛使用的編程語言,它提供了許多數(shù)學運算的函數(shù)和方法,使得開發(fā)者可以輕松地進行各種數(shù)學計算,下面這篇文章主要給大家介紹了關于Java中數(shù)學相關類使用的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
1.java.lang.Math
java.lang.Math
類包含用于執(zhí)行基本數(shù)學運算的方法,如初等指數(shù)、對數(shù)、平方根和三角函數(shù)。類似這樣的工具類,其所有方法均為靜態(tài)方法,并且不會創(chuàng)建對象。
public static double abs(double a)
:返回 double 值的絕對值。
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。(相當于四舍五入方法)
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)的隨機值
- 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的包裝類,能存儲的最大整型值為231-1,Long類也是有限的,最大為263-1。如果要表示再大的整數(shù),不管是基本數(shù)據(jù)類型還是他們的包裝類都無能為力,更不用說進行運算了。
- java.math包的BigInteger可以表示不可變的任意精度的整數(shù)。BigInteger 提供所有 Java 的基本整數(shù)操作符的對應物,并提供 java.lang.Math 的所有相關方法。另外,BigInteger 還提供以下運算:模算術、GCD 計算、質數(shù)測試、素數(shù)生成、位操作以及一些其他操作。
- 構造器
- BigInteger(String val):根據(jù)字符串構建BigInteger對象
- 方法
- public BigInteger abs():返回此 BigInteger 的絕對值的 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) 的兩個 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));//錯誤的,無法直接使用+進行求和 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類可以用來做科學計算或工程計算,但在商業(yè)計算中,要求數(shù)字精度比較高,故用到java.math.BigDecimal類。
- BigDecimal類支持不可變的、任意精度的有符號十進制定點數(shù)。
- 構造器
- 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
用于產生隨機數(shù)
- boolean nextBoolean():返回下一個偽隨機數(shù),它是取自此隨機數(shù)生成器序列的均勻分布的 boolean 值。
- void nextBytes(byte[] bytes):生成隨機字節(jié)并將其置于用戶提供的 byte 數(shù)組中。
- double nextDouble():返回下一個偽隨機數(shù),它是取自此隨機數(shù)生成器序列的、在 0.0 和 1.0 之間均勻分布的 double 值。
- float nextFloat():返回下一個偽隨機數(shù),它是取自此隨機數(shù)生成器序列的、在 0.0 和 1.0 之間均勻分布的 float 值。
- double nextGaussian():返回下一個偽隨機數(shù),它是取自此隨機數(shù)生成器序列的、呈高斯(“正態(tài)”)分布的 double 值,其平均值是 0.0,標準差是 1.0。
- int nextInt():返回下一個偽隨機數(shù),它是此隨機數(shù)生成器的序列中均勻分布的 int 值。
- int nextInt(int n):返回一個偽隨機數(shù),它是取自此隨機數(shù)生成器序列的、在 0(包括)和指定值(不包括)之間均勻分布的 int 值。
- long nextLong():返回下一個偽隨機數(shù),它是取自此隨機數(shù)生成器序列的均勻分布的 long 值。
@Test public void test04(){ Random r = new Random(); System.out.println("隨機整數(shù):" + r.nextInt()); System.out.println("隨機小數(shù):" + r.nextDouble()); System.out.println("隨機布爾值:" + r.nextBoolean()); }
附:更多java數(shù)學函數(shù)Math類實例
Math.abs(12.3); //12.3 返回這個數(shù)的絕對值 Math.abs(-12.3); //12.3 Math.copySign(1.23, -12.3); //-1.23,返回第一個參數(shù)的量值和第二個參數(shù)的符號 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); //返回最近的且大于這個數(shù)的整數(shù)13.0 Math.ceil(-12.3); //-12.0 Math.floor(12.3); //返回最近的且小于這個數(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 //對數(shù)函數(shù) Math.log(e); //1 以e為底的對數(shù) Math.log10(100); //10 以10為底的對數(shù) Math.log1p(x); //Ln(x+ 1) //返回較大值和較小值 Math.max(x, y); //返回x、y中較大的那個數(shù) Math.min(x, y); //返回x、y中較小的那個數(shù) //返回 x的y次冪 Math.pow(x, y); Math.pow(2, 3); //即23 即返回:8 //隨機返回[0,1)之間的無符號double值 Math.random(); //返回最接近這個數(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); //同上,返回經過點(x,y)與原點的的直線和經過點(x0,y0)與原點的直線之間所成的夾角 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); //角度轉換成弧度,返回:angrad * 180d / PI Math.toRadians(angdeg); //弧度轉換成角度,返回:angdeg / 180d * PI
總結
到此這篇關于Java中數(shù)學相關類使用的文章就介紹到這了,更多相關Java數(shù)學相關類內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot+Websocket實現(xiàn)一個簡單的網頁聊天功能代碼
本篇文章主要介紹了SpringBoot+Websocket實現(xiàn)一個簡單的網頁聊天功能代碼,具有一定的參考價值,有需要的可以了解一下2017-08-08Spring boot創(chuàng)建自定義starter的完整步驟
這篇文章主要給大家介紹了關于Spring boot創(chuàng)建自定義starter的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-09-09SpringBoot整合Sa-Token實現(xiàn)?API?接口簽名安全校驗功能
這篇文章主要介紹了SpringBoot整合Sa-Token實現(xiàn)?API?接口簽名安全校驗功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07