Java 双精度或浮点数据类型不';在循环中不能正确相加?

Java 双精度或浮点数据类型不';在循环中不能正确相加?,java,for-loop,floating-point,floating-accuracy,Java,For Loop,Floating Point,Floating Accuracy,在一个循环中,我添加0.10,直到达到所需的#,并得到索引。这是我的代码: private static int getIndexOfUnits(float units) { int index = -1; float addup = 0.10f; for(float i = 1.00f; i < units; i=(float)i+addup) { index++; System.out.println("I = " + i

在一个循环中,我添加0.10,直到达到所需的#,并得到索引。这是我的代码:

    private static int getIndexOfUnits(float units) {
    int index = -1;
    float addup = 0.10f;
    for(float i = 1.00f; i < units; i=(float)i+addup) {
        index++;
        System.out.println("I = " + i + " Index = " + index);
    }

    return index;
}
如果单位是18.90或29.90这样的大数字,它给出了错误的索引。索引通常比它应该的小1。初始值仅添加了0.10,但在2.3之后,它将获得2.39999。。。。在向其添加0.10时。我相信这是一个精确的问题。如何处理它,并确保在大#上获得正确的索引,无论使用float还是double

任何想法

来自:

为什么我的数字,比如0.1+0.2加起来不等于0.3,然后 相反,我得到了一个奇怪的结果,比如0.300000000004?

因为在内部,计算机使用一种格式(二进制浮点) 这根本不能准确地表示0.1、0.2或0.3这样的数字

当编译或解释代码时,您的“0.1”已被删除 按该格式四舍五入到最接近的数字,这将导致较小的 甚至在计算之前就存在舍入误差

如果需要数字进行精确相加,则不能使用
float
double
。改用
BigDecimal

来自:

为什么我的数字,比如0.1+0.2加起来不等于0.3,然后 相反,我得到了一个奇怪的结果,比如0.300000000004?

因为在内部,计算机使用一种格式(二进制浮点) 这根本不能准确地表示0.1、0.2或0.3这样的数字

当编译或解释代码时,您的“0.1”已被删除 按该格式四舍五入到最接近的数字,这将导致较小的 甚至在计算之前就存在舍入误差


如果需要数字进行精确相加,则不能使用
float
double
。请改用
BigDecimal

问题在于float使用的格式不能代表所有十进制数,因此有时会丢失精度


改用。

问题是float使用的格式不能代表所有十进制数,因此有时会丢失精度


请改用。

精确表示法不适用于浮点数据类型

将对您有所帮助。下面的示例可能会对您有所帮助

BigDecimal decimal=new BigDecimal(10.0);
    for (int i = 0; i < 10; i++) {
        decimal=decimal.add(new BigDecimal(.1));
        System.out.println(decimal.floatValue());

    }
BigDecimal decimal=新的BigDecimal(10.0);
对于(int i=0;i<10;i++){
decimal=decimal.add(新的BigDecimal(.1));
System.out.println(decimal.floatValue());
}

精确表示法不适用于浮点数据类型

将对您有所帮助。下面的示例可能会对您有所帮助

BigDecimal decimal=new BigDecimal(10.0);
    for (int i = 0; i < 10; i++) {
        decimal=decimal.add(new BigDecimal(.1));
        System.out.println(decimal.floatValue());

    }
BigDecimal decimal=新的BigDecimal(10.0);
对于(int i=0;i<10;i++){
decimal=decimal.add(新的BigDecimal(.1));
System.out.println(decimal.floatValue());
}

这种事情(索引/迭代)不应该使用float

每次尝试计算I:

I = 1.0f + (i*addup);

而且您不会累积浮点错误。

对于这类事情(索引/迭代),您不应该使用浮点

每次尝试计算I:

I = 1.0f + (i*addup);

而且您不会累积浮点错误。

我认为这可能就是您要寻找的:

Java从导入包中提供了一个类:import Java.text.DecimalFormat,名为DecimalFormat。其签名为:

DecimalFormat myFormat = new DecimalFormat("0.0");
它接受一个字符串参数,您可以在其中指定格式的显示方式

以下是如何将其应用于代码:

DecimalFormat myFormat;
private static int getIndexOfUnits(float units) {
myFormat = new DecimalFormat("0.0");
int index = -1;
float addup = 0.10f;
for(float i = 1.00f; i < units; i=(float)i+addup) {
    index++;
    System.out.println("I = " + myFormat.format(i) + " Index = " + index);
}

return index;
DecimalFormat myFormat;
私有静态int getIndexOfUnits(浮点单位){
myFormat=新的十进制格式(“0.0”);
int指数=-1;
浮点数加总=0.10f;
对于(浮点数i=1.00f;i<单位;i=(浮点数)i+相加){
索引++;
System.out.println(“I=“+myFormat.format(I)+”Index=“+Index”);
}
收益指数;
}


在您的println中,您可以看到DecimalFormat的类
format()
方法是在
float i
上使用对DecimalFormat的myFormat对象引用调用的-这就是进行格式化的地方。

我想这可能就是您正在寻找的:

Java从导入包中提供了一个类:import Java.text.DecimalFormat,名为DecimalFormat。其签名为:

DecimalFormat myFormat = new DecimalFormat("0.0");
它接受一个字符串参数,您可以在其中指定格式的显示方式

以下是如何将其应用于代码:

DecimalFormat myFormat;
private static int getIndexOfUnits(float units) {
myFormat = new DecimalFormat("0.0");
int index = -1;
float addup = 0.10f;
for(float i = 1.00f; i < units; i=(float)i+addup) {
    index++;
    System.out.println("I = " + myFormat.format(i) + " Index = " + index);
}

return index;
DecimalFormat myFormat;
私有静态int getIndexOfUnits(浮点单位){
myFormat=新的十进制格式(“0.0”);
int指数=-1;
浮点数加总=0.10f;
对于(浮点数i=1.00f;i<单位;i=(浮点数)i+相加){
索引++;
System.out.println(“I=“+myFormat.format(I)+”Index=“+Index”);
}
收益指数;
}


在println中,您可以看到使用DecimalFormat的myFormat对象引用调用了DecimalFormat的类
format()
方法
float i
,这就是进行格式化的地方。

您意识到0.10在java(以及其他所有语言)浮点系统中没有精确的表示吗?不管您提供多少精度,即使java有百万位精度浮点类型。您需要使用一个专门的decimal类或fractions类。您意识到0.10在java(以及其他所有语言)的浮点系统中没有精确的表示吗?不管您提供多少精度,即使java有百万位精度浮点类型。您需要使用专门的十进制类或分数类。