java Java中的双增量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13832864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Double increments in Java
提问by user1483799
Possible Duplicate:
How to iterate between 0.1f and 1.0f with 0.1f increments in Java?
Part of my program needs to use values inside a while loop as:
我的程序的一部分需要在 while 循环中使用值作为:
0.1
0.1
0.2
0.2
0.3
0.3
...
...
0.9
0.9
so I need to provide them inside that loop. Here is the code:
所以我需要在该循环中提供它们。这是代码:
double x = 0.0;
while ( x<=1 )
{
// increment x by 0.1 for each iteration
x += 0.1;
}
I need the output to be EXACTLY:
我需要输出完全正确:
0.1
0.1
0.2
0.2
0.3
0.3
0.4
0.4
0.5
0.5
0.6
0.6
0.7
0.7
0.8
0.8
0.9
0.9
But it actually gives me something like:
但它实际上给了我类似的东西:
0.1
0.1
0.2
0.2
0.300000000000000000000000004
0.300000000000000000000000004
0.4
0.4
0.5
0.5
0.6
0.6
0.79999999999999999999999999
0.79999999999999999999999999
0.89999999999999999999999999
0.89999999999999999999999999
0.99999999999999999999999999
0.99999999999999999999999999
回答by Ted Hopp
Welcome to the world of floating point, where 0.1 isn't 0.1. The problem is that many numbers, including 0.1, cannot be represented exactly in a double
. So you aren't really adding exactly 0.1 to x
each time through the loop.
欢迎来到浮点数的世界,这里 0.1 不是 0.1。问题是许多数字,包括 0.1,无法在double
. 所以你并没有真正在x
每次循环中添加 0.1 。
One approach is to use integer arithmetic and divide by 10:
一种方法是使用整数算术并除以 10:
int i = 0;
while (i <= 10) {
double x = i / 10.0;
. . .
i++;
}
Another approach is to make x
a BigDecimal
, where you can specify that you want a particular precision. It basically is doing what the above loop does (an integer plus a scale), but packaged up in a nice class with lots of bells and whistles. Oh, and it has arbitrary precision.
另一种方法是制作x
a BigDecimal
,您可以在其中指定您想要特定的精度。它基本上是在做上面循环所做的事情(一个整数加上一个比例),但打包在一个很好的类中,有很多花里胡哨的东西。哦,它具有任意精度。
回答by simbu94
you need to use the decimal formatter to get the expected output.
您需要使用十进制格式化程序来获得预期的输出。
Below is the code for generating the expected output:
下面是生成预期输出的代码:
import java.text.DecimalFormat;
public class FloatIncrement {
public static void main (String args[]){
double x= 0.0;
DecimalFormat form = new DecimalFormat("#.#");
while(x<0.9){
x= x+0.1;
System.out.println("X : "+Double.valueOf(form.format(x)));
}
}
}
回答by squiguy
To get output you want, you could use DecimalFormat
. Here is some sample code.
要获得您想要的输出,您可以使用DecimalFormat
. 这是一些示例代码。
import java.text.DecimalFormat;
public class DF {
public static void main(String [] args) {
double x = 0.1;
DecimalFormat form = new DecimalFormat("#.#");
while (x <= .9) {
System.out.println(Double.valueOf(form.format(x)));
x += 0.1;
}
}
}
As far as the implementation you have now, there is no guarantee as to the precision of what gets printed due to the nature of floating point numbers.
就您现在的实现而言,由于浮点数的性质,无法保证打印内容的精度。
回答by Mohammod Hossain
Using BigDecimal
double x = 0.0;
int decimalPlaces = 2;
while ( x<=1 )
{
x += 0.1;
BigDecimal bd = new BigDecimal(x);
bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
x = bd.doubleValue();
System.out.println(x);
}
回答by Lawrence Dol
That's because you can use binary floating point to do precise decimal arithmetic because FP cannot precisely represent all decimal values.
那是因为您可以使用二进制浮点数进行精确的十进制运算,因为 FP 无法精确表示所有十进制值。
You need to use an integer value representing some decimal fractional unit like hundredths or thousandths or use something like BigDecimal.
您需要使用一个整数值来表示某个十进制小数单位,例如百分之几或千分之几,或者使用诸如 BigDecimal 之类的东西。
回答by Shariq
Double is stored in binary
Double 以二进制形式存储
floatand doublestore numbers as a certain number of significant figures and a radix point (kind of like scientific notation). The significant figures part is not always perfect, because it's stored as a certain number of binary digits - so you can't expect it to perform the way you're expecting it to. (for a better explanation see http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)
float和double将数字存储为一定数量的有效数字和小数点(有点像科学记数法)。有效数字部分并不总是完美的,因为它存储为一定数量的二进制数字 - 因此您不能期望它按照您期望的方式执行。(有关更好的解释,请参阅http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)
Consider using a class such as BigDecimal or a class that implements rational numbers, like the ones mentioned here - Is there a commonly used rational numbers library in Java?
考虑使用诸如 BigDecimal 之类的类或实现有理数的类,例如此处提到的类 - Java 中是否有常用的有理数库?
You could also just turn i into an integer, and change it from 1 to 10, and compensate for this in your code.
您也可以将 i 转换为整数,并将其从 1 更改为 10,并在您的代码中对此进行补偿。