Java 乘法错误
当我运行以下代码时,出现错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我运行了这部分代码,我假设您指的是精度错误,例如获取诸如
398.20000000000005
和407.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
and407.00000000000006
instead of398
and407
.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.
正如彼得所说,这是由于浮点数的不精确性造成的。要修复它,请使用 printf,如下所示:
请参阅 此处或此处了解更多信息关于如何使用 printf。
As Peter said, this is due to the imprecision of floating point numbers. To fix it, use printf, like this:
See here or here for more information on how to use printf.
错误消息会有帮助。
第三行,System.println应该是System.out.println:
The error message would help.
On the third line, System.println should be System.out.println:
系统没有像 println (System.out.println) 这样的方法,我也会使用整数作为 i。
System doesnt have any method like println (System.out.println) also I would use integer for i.
正如所指出的,您没有提供完整的代码示例
不幸的是,您也没有具体说明确切的问题。
我想你的意思可能是“1 * 2.2!= 2.0”。
如果是这样,问题是“整数截断” - 当您将整数乘以(加、减、除或乘)浮点数时,小数部分会丢失。
按照设计:)
这些文章可能会有所帮助:
http://www .brighthub.com/computing/windows-platform/articles/11241.aspx
http://download.oracle.com/javase/tutorial/java/data/beyondmath.html
http://www.codestyle.org/java/FAQ.shtml#roundupdoubleint
As pointed out, you didn't give a complete code sample
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:
http://www.brighthub.com/computing/windows-platform/articles/11241.aspx
http://download.oracle.com/javase/tutorial/java/data/beyondmath.html
http://www.codestyle.org/java/FAQ.shtml#roundupdoubleint