当前位置:首页 » 编程语言 » java中根号怎么表示

java中根号怎么表示

发布时间: 2023-07-01 16:19:43

‘壹’ 根号3怎么打

用WPS文字文档插入的方法:

1、打开WPS文字,新建一空白文档。实现在文档中√3输入。如下图:

(1)java中根号怎么表示扩展阅读

根号3在计算机中表达为√(3)或√3,先输入√在输入3。

‘贰’ java中如何对一个数开根号

java 中对一个数开根号可以使用系统提供的Math.sqrt() 函数进行操作

例:

Math.sqrt(3);//得到结果就是3

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

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.76406162506362881367
6510354513.
46726188

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:566
制作脚本网站 发布:2025-10-20 08:17:34 浏览:855
python中的init方法 发布:2025-10-20 08:17:33 浏览:555
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:733
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:656
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:975
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:227
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:87
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:777
python股票数据获取 发布:2025-10-20 07:39:44 浏览:683