gpt4 book ai didi

Java 大 double 损失

转载 作者:行者123 更新时间:2023-11-29 09:54:12 35 4
gpt4 key购买 nike

 double lnumber = Math.pow(2, 1000);

打印 1.0715086071862673E301

我尝试过的事情

我尝试使用 BigDecimal 类来扩展这个数字:

 String strNumber = new BigDecimal(Double.toString(lnumber)).toPlainString();

这只是打印:

10715086071862673000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

我还尝试使用 DecimalFormat:

    DecimalFormat df = new DecimalFormat("#");
df.setMaximumFractionDigits(0);
String strNumber = String.valueOf(df.format(lnumber));

打印同样的东西:

10715086071862673000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

根据 Wolfram Alpha 的实际答案是

enter image description here

如何打印所有实际值?

最佳答案

你不能像那样混合和匹配 Math、原始类型和 BigDecimal,如果你想要真正的精度,只需使用 BigDecimal:

BigDecimal value = new BigDecimal(2);
System.out.println(value.pow(1000));

关于Java 大 double 损失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20250725/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com