双或浮动数据类型不正确地在循环中引用? [英] double or float datatype doesn't addup properly in a loop?

查看:165
本文介绍了双或浮动数据类型不正确地在循环中引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个循环中,我加了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;



$ b $ p
$ b如果通过的单位是5.7,我看到的输入是:

  I = 1.0指数= 0 
I = 1.1指数= 1
I = 1.2指数= 2
I = 1.3000001指数= 3
I = 1.4000001指数= 4
I = 1.5000001指数= 5
I = 1.6000001指数= 6
I = 1.7000002指数= 7
I = 1.8000002指数= 8
I = 1.9000002指数= 9
I = 2.0000002指数= 10
I = 2.1000001指数= 11
I = 2.2指数= 12
I = 2.3指数= 13
I = 2.3999999指数= 14
I = 2.4999998指数= 15
I = 2.5999997指数= 16
I = 2.6999996指数= 17
I = 2.7999995指数= 18
I = 2.8999994指数= 19
I = 2.9999993指数= 20
I = 3.0999992指数= 21
I = 3.199999指数= 22
I = 3.299999 Index = 23
I = 3.399999 Index = 24
I = 3.4999988 Index = 25
I = 3.5999987 Index = 26
I = 3.6999986 Index = 27
I = 3.7999985指数= 28
I = 3.8999984指数= 29
I = 3.9999983指数= 30
I = 4.0999985指数= 31
I = 4.1999984指数= 32
I = 4.2999983指数= 33
I = 4.399998指数= 34
I = 4.499998指数= 35
I = 4.599998指数= 36
I = 4.699998指数= 37
I = 4.799998指数= 38
I = 4.8999977指数= 39
I = 4.9999976指数= 40
I = 5.0999975指数= 41
I = 5.1999974指数= 42
I = 5.2999973指数= 43
I = 5.399997指数= 44
I = 5.499997指数= 45
I = 5.599997 Index = 46
I = 5.699997 Index = 47

单位是18.90或29.90这样的大数字,给出了错误的指标。索引通常比应该少1。最初只添加0.10,但在2.3之后,它得到2.39999 ....添加0.10。我相信这是一个精确的问题。如何处理它,并确保我得到正确的大#的指数也无论使用浮动或双。



任何想法!!!!

解决方案

href =http://floating-point-gui.de/basic/ =noreferrer>浮点指南:


为什么我的数字,比如0.1 + 0.2加起来不错0.3回合,而
取而代之,我得到了一个奇怪的结果,比如0.30000000000000004?



因为在内部,计算机使用的格式(二进制浮点)
不能准确地表示一个数字,如0.1,0.2或0.3。

在编译或解释代码时,您的0.1已经是
,以该格式四舍五入到最接近的数字,甚至在计算发生之前会导致小的
舍入误差。

您不使用 float double 如果你需要数字来合计exaclty。改用 BigDecimal


In a loop I am adding 0.10 till I reach the desired #, and getting the index. This is my code :

    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;
}

If the units passed is 5.7, the ourput I see is :

I = 1.0 Index = 0
I = 1.1 Index = 1
I = 1.2 Index = 2
I = 1.3000001 Index = 3
I = 1.4000001 Index = 4
I = 1.5000001 Index = 5
I = 1.6000001 Index = 6
I = 1.7000002 Index = 7
I = 1.8000002 Index = 8
I = 1.9000002 Index = 9
I = 2.0000002 Index = 10
I = 2.1000001 Index = 11
I = 2.2 Index = 12
I = 2.3 Index = 13
I = 2.3999999 Index = 14
I = 2.4999998 Index = 15
I = 2.5999997 Index = 16
I = 2.6999996 Index = 17
I = 2.7999995 Index = 18
I = 2.8999994 Index = 19
I = 2.9999993 Index = 20
I = 3.0999992 Index = 21
I = 3.199999 Index = 22
I = 3.299999 Index = 23
I = 3.399999 Index = 24
I = 3.4999988 Index = 25
I = 3.5999987 Index = 26
I = 3.6999986 Index = 27
I = 3.7999985 Index = 28
I = 3.8999984 Index = 29
I = 3.9999983 Index = 30
I = 4.0999985 Index = 31
I = 4.1999984 Index = 32
I = 4.2999983 Index = 33
I = 4.399998 Index = 34
I = 4.499998 Index = 35
I = 4.599998 Index = 36
I = 4.699998 Index = 37
I = 4.799998 Index = 38
I = 4.8999977 Index = 39
I = 4.9999976 Index = 40
I = 5.0999975 Index = 41
I = 5.1999974 Index = 42
I = 5.2999973 Index = 43
I = 5.399997 Index = 44
I = 5.499997 Index = 45
I = 5.599997 Index = 46
I = 5.699997 Index = 47

If the units is big number like 18.90 or 29.90, it gives wrong index. Index is normally 1 less then it should be. Initial only 0.10 was added but after 2.3, it gets 2.39999.... on adding 0.10 to it. I believe this is a matter of precision. How to handle it and make sure that I get the right index on big #'s also regardless of using float or double.

Any ideas !!!!

解决方案

From the Floating-Point Guide:

Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004?

Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

When the code is compiled or interpreted, your "0.1" is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.

You cnnot use float or double if you need numbers to add up exaclty. Use BigDecimal instead.

这篇关于双或浮动数据类型不正确地在循环中引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆