gpt4 book ai didi

java - float 精度损失

转载 作者:行者123 更新时间:2023-12-01 20:27:10 27 4
gpt4 key购买 nike

我正在尝试计算 float 的总和。所有小数字都能正确输出,当我使用非常大的数字作为输入时,输出总是会偏离几个整数。例如,H = 5764801 W = 1679616,纸面上的计算结果为 335923 30275911。但在我的程序中,打印的是 335923 30275908 。这是代码:

public void printOutput(int H, int W) // The inputs
{
if(H == 1 && W == 1)
{
System.out.println(0 + " " + 1);
return;
}

List<Integer> pfw = primeFactors(W);

int y = 1;

while(H != (int) (Math.pow(Math.pow(W, 1f/y) + 1f, y))) y++;

final float N = findWholeNumber(pfw);

float height = 0;
for(int x = 1; x <= y + 1; x++)
{
height += (float) (W * Math.pow((N + 1f) / N, x-1f) + 1e-8); //Here is the summation
}
float cats = 1;
for(int x = 2; x <= y + 1; x++)
cats += (float) (Math.pow(N, x-1));

int notWorking = (int) (cats - W);
System.out.println(notWorking + " " + (int)height); //Outputs printing
}

private int findWholeNumber(List<Integer> factors)
{
List<Integer> common = new ArrayList<Integer>();
for(int i = 0; i < factors.size(); i++)
{
if(common.contains(factors.get(i))) continue;
common.add(factors.get(i));
}
int num = common.get(0);
for(int i = 1; i < common.size(); i++)
num *= common.get(i);
return num;
}

private List<Integer> primeFactors(int num)
{
List<Integer> pf = new ArrayList<Integer>();

if(num == 1)
{
pf.add(1);
return pf;
}

for(int j = 2; j <= num; j++)
while(num % j == 0) // is prime
{
pf.add(j);
num /= j;
}

return pf;
}

}

最佳答案

float 的精度有限,因为尾数的宽度有限。

您可以尝试 double 对于您的情况,其精度更高(因为它的尾数更宽),但它也是有限的。

更多信息:https://en.wikipedia.org/wiki/IEEE_floating_point#IEEE_754-2008What is the maximum number in the mantissa part of a Java float?

如果您需要无限的精度,请尝试BigDecimal。有效数字的数量仅受内存量的限制。

如果您只需要整数值,BigInteger 是一个选项。

关于java - float 精度损失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43709732/

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