问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

Java中,如何对大数开根号啊!

发布网友 发布时间:2022-04-23 03:23

我来回答

4个回答

热心网友 时间:2023-09-20 10:56

java中对于大数BigInteger,BigDecimal开根号没有提供函数,可以参考以下实现方法:

import java.math.BigDecimal;
import java.math.BigInteger;
public class BigSquareRoot {
final static BigInteger HUNDRED = BigInteger.valueOf(100);

public static BigDecimal sqrt(BigDecimal number, int scale, int roundingMode) {
if (number.compareTo(BigDecimal.ZERO) < 0)
throw new ArithmeticException("sqrt with negative");
BigInteger integer = number.toBigInteger();
StringBuffer sb = new StringBuffer();
String strInt = integer.toString();
int lenInt = strInt.length();
if (lenInt % 2 != 0) {
strInt = '0' + strInt;
lenInt++;
}
BigInteger res = BigInteger.ZERO;
BigInteger rem = BigInteger.ZERO;
for (int i = 0; i < lenInt / 2; i++) {
res = res.multiply(BigInteger.TEN);
rem = rem.multiply(HUNDRED);

BigInteger temp = new BigInteger(strInt.substring(i * 2, i * 2 + 2));
rem = rem.add(temp);

BigInteger j = BigInteger.TEN;
while (j.compareTo(BigInteger.ZERO) > 0) {
j = j.subtract(BigInteger.ONE);
if (((res.add(j)).multiply(j)).compareTo(rem) <= 0) {
break;
}
}

res = res.add(j);
rem = rem.subtract(res.multiply(j));
res = res.add(j);
sb.append(j);
}
sb.append('.');
BigDecimal fraction = number.subtract(number.setScale(0, BigDecimal.ROUND_DOWN));
int fracLen = (fraction.scale() + 1) / 2;
fraction = fraction.movePointRight(fracLen * 2);
String strFrac = fraction.toPlainString();
for (int i = 0; i <= scale; i++) {
res = res.multiply(BigInteger.TEN);
rem = rem.multiply(HUNDRED);

if (i < fracLen) {
BigInteger temp = new BigInteger(strFrac.substring(i * 2, i * 2 + 2));
rem = rem.add(temp);
}

BigInteger j = BigInteger.TEN;
while (j.compareTo(BigInteger.ZERO) > 0) {
j = j.subtract(BigInteger.ONE);
if (((res.add(j)).multiply(j)).compareTo(rem) <= 0) {
break;
}
}
res = res.add(j);
rem = rem.subtract(res.multiply(j));
res = res.add(j);
sb.append(j);
}
return new BigDecimal(sb.toString()).setScale(scale, roundingMode);
}

public static BigDecimal sqrt(BigDecimal number, int scale) {
return sqrt(number, scale, BigDecimal.ROUND_HALF_UP);
}

public static BigDecimal sqrt(BigDecimal number) {
int scale = number.scale() * 2;
if (scale < 50)
scale = 50;
return sqrt(number, scale, BigDecimal.ROUND_HALF_UP);
}

public static void main(String args[]) {
BigDecimal num = new BigDecimal("6510354513.6564897413514568413");
long time = System.nanoTime();
BigDecimal root = sqrt(num, 1000);
time = System.nanoTime() - time;
System.out.println(root);
System.out.println(root.pow(2));
System.out.println(time);
}
}
执行结果:
80686.7678969512927493416316741557266722739984372151634715876752358049492663077817843059095146637911180490885758744651273281303288317374885332607051330176028572558172054217029042642080284121950891605518862273493239191320132148293688445347529243846517751025383884710742819252354014378344895438280908159584992112041354808433466321589387318739165992813377399669170549811704076258078653548749003251504791227309054913701062929858500433745971631998487835576600579373929233933246442803804132298016737159672317482520249763464713581048142915509001995943192415694815489364740152312416736301233269587910628885614893125235822493317184917626076223325819402403220531926392808333854523694780539563293133232729900988243013464020440976396084796739002581380094075169287492710301071487312491530290342213569053680461901907481289230152643599970138861788489599118674849815164425194138401918499233009571650761625943781367455101019720348741842171772915942278011905594031830367343193606047124373968951524359600676406162506362881367
6510354513.65648974135145684129999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999261018570188221952291201089398906052231483112444155137889353596770526763176032152876816872483428732651504536335267475372360148274382594285697529542224437785449440331424040251426099649509195223143651828659587364153208947136985285283628868915378240201094213227734756251495878566340409599684823659444904292099882985668906808623177208665251919370268937893457306121802766566270627041680526252368411023459487996551104603913903029501700835230597885867761183071537171958826568685086349210032834841075060512309444622145616551108119893623864579013813710941167261179972571233574705638862140357465569295994523261742960807593601727929262728856153880503561459736300910103299707873770713267018154171165545178430002342459940561678884530151166769964180453998734209554051691222326553700712948508454649608942746651683572634224323098274435576290709769148239120722342126902645574609770558211829972705514153006434846614016006956921594506606758832662240593485962629910320205678474047322232577044567479336985830934534290515788689
46726188

热心网友 时间:2023-09-20 10:57

BigInteger,BigDecimal 是无精度损失运算的,也就是说只要你给出合法的算法,它就能给出精确的结果。

但你知道,开方有时会是一个无限小数,也就是说,如果叫BigInteger,BigDecimal去开方将永远运行下去,这显然不是你想要的,但如果精确到小数点后N位,这又违背了无精度损失的初衷,所以就有了你提的问题。

你拿BigDecimal(1) divide BigDecimal(3) 就知道了。

热心网友 时间:2023-09-20 10:57

貌似暂时还没有
自己写

import java.math.BigDecimal;
import java.math.MathContext;

public class Test5 {

public static void main(String[] args) {
BigDecimal n = new BigDecimal("2");
BigDecimal r = sqrt(n);
System.out.println(r.toString());
}

public static BigDecimal sqrt(BigDecimal num) {
if(num.compareTo(BigDecimal.ZERO) < 0) {
return BigDecimal.ZERO;
}
BigDecimal x = num.divide(new BigDecimal("2"), MathContext.DECIMAL128);
while(x.subtract(x = sqrtIteration(x, num)).abs().compareTo(new BigDecimal("0.0000000000000000000001")) > 0);
return x;
}

private static BigDecimal sqrtIteration(BigDecimal x, BigDecimal n) {
return x.add(n.divide(x, MathContext.DECIMAL128)).divide(new BigDecimal("2"), MathContext.DECIMAL128);
}
}

热心网友 时间:2023-09-20 10:58

比如
//保留1000位有效数字
BigDecimal num = new BigDecimal("4");

MathContext mc = new MathContext(1000,RoundingMode.HALF_DOWN);

BigDecimal finalnum = new BigDecimal(Math.sqrt(num.doubleValue()) ,mc);
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
深圳公共营养师怎么申请补贴 深圳公共营养师补贴怎么领 深圳营养师工资待遇怎么样 ...出道16年一直不露额头,当他撩起刘海,效果堪比「 ”整容”_百度... ...刘海的鹿晗,重回颜值巅峰!换个发型堪比“整容”都有哪些?_百度... ...整容”叫尹正剪头,仅仅换个发型而已,为何会变化那么大呢?_百度... 会计考高级有什么要求 报考高级会计需要什么条件呢? 工程项目融资、建设、管理、运营等过程中各模式的英文简称及专业分析... 200平米别墅装修多少钱别墅装修需要注意些什么 如何科学的减肥瘦身? java中大数(100!+..+200!)表示问题 如何科学运动减肥? java 大数问题 怎样健康科学的运动瘦大腿? java中如何对较大的数字进行计算 怎么运动减掉脂肪? JAVA大数相减 牛肉怎样煮 运动减肥可以怎么样减? 要科学 Java大数类怎么表示小于(或大于)一个数 如何通过科学运动减掉肚子上的赘肉? java怎么处理大数相加 广州java短期培训哪个好 怎么科学减肥? 学java大数 据开 发,就业 怎么样? 怎么样科学的运动减肥 用java如何显示大数的每一位出来啊 如何通过科学运动减掉肚子上的赘肉? ? java求最大数和次大数 运用JAVA中大数类实现大数的模运算 JAVA 大数阶乘,不用biginteger怎么做 如何选购家用的电视网络机顶盒? 网络机顶盒怎样选购,购买电视盒子有哪些需要注意? 电视机顶盒和网络机顶盒如何选择?哪一个更加实用? 智能机顶盒怎样才能买到,只能到电信吗? 如何选购网络电视机顶盒?麻烦告诉我 如何选购电视网络机顶盒? 怎么购买网络机顶盒,需注意哪些问题 如何选择网络机顶盒 网络电视机顶盒怎么选 购买注意事项 怎么在移动网上购买机顶盒 买网络机顶盒,要买什么样的? 网络机顶盒在哪可以买到? 买盒子要注意什么,网络机顶盒怎么选? 网络机顶盒要怎么选?怎么用? 网络电视机顶盒怎么挑选 请问假如我现在买了电视机,需要安装机顶盒,应该在哪里买机顶盒,怎样安装? 电信电视盒子怎么可以买到? 苹果13 id登陆那里换取照片为什么无法换取照片呢