Java 乘法错误

发布于 2024-12-02 23:33:23 字数 353 浏览 2 评论 0原文

当我运行以下代码时,出现错误:

double i = 1;
double pound;
System.out.println("Kilograms\t Pounds");
for(i = 1; i<199; i+=2){
pound = i * 2.2;
System.out.println(i + "\t\t" + pound + "\n");
}

错误:

Kilograms     Pounds
1.0          2.2  
3.0          6.6000000005  
5.0          11.0  
7.0          15.400000002  

When I run the following code, I get an error:

double i = 1;
double pound;
System.out.println("Kilograms\t Pounds");
for(i = 1; i<199; i+=2){
pound = i * 2.2;
System.out.println(i + "\t\t" + pound + "\n");
}

The error:

Kilograms     Pounds
1.0          2.2  
3.0          6.6000000005  
5.0          11.0  
7.0          15.400000002  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

带上头具痛哭 2024-12-09 23:33:23

我运行了这部分代码,我假设您指的是精度错误,例如获取诸如 398.20000000000005407.00000000000006 而不是 398 的数字> 和 407

这是由于浮点格式的数字表达的限制。一些可以用小数点后位数有限的十进制格式表示的数字无法使用有限的精度以浮点格式表示,因此您将得到这些错误。

编辑

我在您发布您定义的错误之前写了我的答案。但没什么不同。请参阅此链接了解有关浮点精度损失的更多信息。

还值得注意的是,在比较两个浮点数(已计算出的)是否“相等”时,您应该指定一些小值,或delta,它是两个数字之间的最大差异允许他们被认为是平等的,而不是测试绝对平等。

I ran this part of the code and I assume you're referrring to the precision errors, such as getting numbers like 398.20000000000005 and 407.00000000000006 instead of 398 and 407.

This is due the limitation of expression a number in floating-point format. Some numbers that can be expressed in decimal format with a limited number of digits after the decimal point just cannot be represented in a floating-point format using limited precision, thus you will get these errors.

EDIT

I wrote my answer before you posted what you defined as the error. But nothing is different. Please see this link for more information about floating-point precision loss.

It's also worthwhile to note that when comparing two floating-point numbers (that have been calculated) for "equality", you should specify some small value, or delta, which is the maximum difference between the two numbers allowed for them to be considered equal, rather than testing for absolute equality.

在巴黎塔顶看东京樱花 2024-12-09 23:33:23

正如彼得所说,这是由于浮点数的不精确性造成的。要修复它,请使用 printf,如下所示:

System.out.printf("%.0f\t\t%.1f\n",i,pound);

请参阅 此处此处了解更多信息关于如何使用 printf。

As Peter said, this is due to the imprecision of floating point numbers. To fix it, use printf, like this:

System.out.printf("%.0f\t\t%.1f\n",i,pound);

See here or here for more information on how to use printf.

忆沫 2024-12-09 23:33:23

错误消息会有帮助。

第三行,System.println应该是System.out.println:

System.out.println("Kilograms\t Pounds");

The error message would help.

On the third line, System.println should be System.out.println:

System.out.println("Kilograms\t Pounds");
¢蛋碎的人ぎ生 2024-12-09 23:33:23

系统没有像 println (System.out.println) 这样的方法,我也会使用整数作为 i。

System doesnt have any method like println (System.out.println) also I would use integer for i.

下壹個目標 2024-12-09 23:33:23
  1. 正如所指出的,您没有提供完整的代码示例

  2. 不幸的是,您也没有具体说明确切的问题。

我想你的意思可能是“1 * 2.2!= 2.0”。

如果是这样,问题是“整数截断” - 当您将整数乘以(加、减、除或乘)浮点数时,小数部分会丢失。

按照设计:)

这些文章可能会有所帮助:

  1. As pointed out, you didn't give a complete code sample

  2. Unfortunately, you weren't specific about the exact problem, either.

I THINK you probably mean "1 * 2.2 != 2.0".

If so, the problem is "integer truncation" - when you multiply (add, subtract, divide or multiply) and integer by a float, the fractional part is LOST.

By design :)

These articles might help:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文