首页 文章

Java中的双重增量[重复]

提问于
浏览
6

可能重复:如何在Java中以0.1f的增量在0.1f和1.0f之间进行迭代?

Part of my program needs to use values inside a while loop as:

0.1

0.2

0.3

...

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.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

But it actually gives me something like:

0.1

0.2

0.300000000000000000000000004

0.4

0.5

0.6

0.79999999999999999999999999

0.89999999999999999999999999

0.99999999999999999999999999

6 回答

  • 1

    要获得所需的输出,可以使用 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;
        }
    
      }
    
    }
    

    就您现在的实现而言,由于浮点数的性质,无法保证打印的精度 .

  • 1

    使用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); 
      }
    
  • 11

    您需要使用小数格式化程序来获得预期的输出 .

    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)));          
    
            }
    
        }
    }
    
  • 1

    那是因为您可以使用二进制浮点进行精确的十进制算术,因为FP无法精确表示所有十进制值 .

    你需要使用一个整数值来表示一些十进制小数单位,如百分之一或千分之一,或者使用类似BigDecimal的东西 .

  • 0

    欢迎来到浮点世界,其中0.1不是0.1 . 问题是许多数字,包括0.1,无法在_1589796中完全表示 . 因此,每次循环时,您并没有真正添加0.1到 x .

    一种方法是使用整数运算并除以10:

    int i = 0;
    while (i <= 10) {
        double x = i / 10.0;
        . . .
        i++;
    }
    

    另一种方法是使 xBigDecimal,您可以在其中指定您想要特定的精度 . 它基本上是在做上面的循环所做的事情(一个整数加一个刻度),但打包在一个很好的类中,有很多铃声和口哨声 . 哦,它具有任意精度 .

  • 0

    Double以二进制形式存储

    floatdouble 将数字存储为一定数量的有效数字和小数点(有点像科学记数法) . 重要的数字部分并不总是完美的,因为它期望它以你期望的方式运行 . (有关更好的解释,请参阅http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

    考虑使用像BigDecimal这样的类或实现有理数的类,就像这里提到的那样 - Is there a commonly used rational numbers library in Java?

    您也可以将i转换为整数,并将其从1更改为10,并在代码中对此进行补偿 .

相关问题