日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java biginteger 运算_Java大数字运算之BigInteger 原创

發(fā)布時間:2025/3/12 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java biginteger 运算_Java大数字运算之BigInteger 原创 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在 Java中,有許多數(shù)字處理的類,比如Integer 類。但是Integer 類有一定的局限性,下面我們就來看看比 Integer 類更厲害的一個,BigInteger類。

BigInteger類型的數(shù)字范圍較 Integer 類型的數(shù)字范圍要大得多。我們都知道 Integer 是 Int 的包裝類,int 的最大值為 231-1,如果要計算更大的數(shù)字,使用Integer 數(shù)據(jù)類型就無法實現(xiàn)了,所以 Java 中提供了BigInteger 類來處理更大的數(shù)字。 BigInteger 支持任意精度的整數(shù),也就是說在運算中 BigInteger 類型可以準確地表示任何大小的整數(shù)值而不會丟失任何信息。

在 BigInteger類中封裝了多種操作!除了基本的加減乘除操作之外,還提供了絕對值、相反數(shù)、最大公約數(shù)以及判斷是否為質(zhì)數(shù)等操作。

使用BigInteger 類,可以實例化一個BigInteger 對象,并自動調(diào)用相應(yīng)的構(gòu)造函數(shù)。BigInteger 類具有很多構(gòu)造函數(shù),但最直接的一種方式是參數(shù)以字符串形式代表要處理的數(shù)字。

語法如下:

public BigInteger(String val)

其中,val 是十進制字符串。

如果將 2 轉(zhuǎn)換為 BigInteger 類型,可以使用以下語句進行初始化操作:

BigInteger twoInstance = new BigInteger ("2");

一旦創(chuàng)建了對象實例,就可以調(diào)用 BigInteger 類中的一些方法進行運算操作,包括基本的數(shù)學運算和位運算以及一些取相反數(shù)、取絕對值等操作。下面是 BigInteger 類幾種常用的運算方法。

public BigInteger add(BigInteger val):做加法運算

public BigInteger subtract(BigInteger val):做減法運算

public BigInteger multiply(BigInteger val):做乘法運算

public BigInteger divide(BigInteger val):做除法運算

public BigInteger remainder(BigInteger val):做取余操作

public BigInteger pow(int exponet):進行取參數(shù)的 exponet 次方操作

public BigInteger negate():取相反數(shù)

public BigInteger shiftLegt(int n):將數(shù)字左移 n 位,如果 n 為負數(shù),做右移操作

public BigInteger shiftRight(int n):將數(shù)字右移 n 位,如果 n 為負數(shù),做左移操作

public int compareTo(BigInteger val):做數(shù)字比較操作

public BigInteger max(BigInteger val):返回較大的數(shù)值

下面是一個實例。在項目中創(chuàng)建一個類,在類的主方法中創(chuàng)建 BigInteger 類的實例對象,調(diào)用該對象的各種方法實現(xiàn)大整數(shù)的加減乘除和其他運算,并輸出運行結(jié)果。

public static void main(String[] args) {

BigInteger bigInstance = new BigInteger("4"); //實例化一個大數(shù)字

//取該大數(shù)字加2的操作

System.out.println("加法操作:"+

bigInstance.add(new BigInteger("2")));

//取該大數(shù)字減2的操作

System.out.println("減法操作:"+

bigInstance.subtract(new BigInteger("2")));

//取該大數(shù)字乘以2的操作

System.out.println("乘法操作:"+

bigInstance.multiply(new BigInteger("2")));

//取該大數(shù)字除以2的操作

System.out.println("除法操作:"+

bigInstance.divide(new BigInteger("2")));

//取該大數(shù)字除以3的商

System.out.println("取商:"+

bigInstance.divideAndRemainder(new BigInteger("3"))[0]);

//取該大數(shù)字除以3的余數(shù)

System.out.println("取余數(shù):"+

bigInstance.divideAndRemainder(new BigInteger("3"))[1]);

//取該大數(shù)字的2次方

System.out.println("做2次方操作:"+

bigInstance.pow(2));

//取該大數(shù)字的相反數(shù)

System.out.println("取相反數(shù)操作:"+

bigInstance.negate());

}

}

以上就是關(guān)于 BigInteger 類的詳解和實例分析,喜歡的朋友請繼續(xù)關(guān)注聚米學院。

總結(jié)

以上是生活随笔為你收集整理的java biginteger 运算_Java大数字运算之BigInteger 原创的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。