0

i got this class where i have to define a map< Integer,Double >

public class ExpConfig {

    public static final int LEVEL_MIN = 1;
    public static final int LEVEL_MAX = 100;
    public static final int FACTOR = 50;
    public static final double BASE_EXPONENT = 1.8D;

    public static int level = LEVEL_MIN;
    public static double exponent = BASE_EXPONENT;

    private static final Map<Integer, Double> myMap;
    static {
        Map<Integer, Double> aMap = new LinkedHashMap<Integer,Double>();
        aMap.put(20, 0.01D);//From level 1 to 20
        aMap.put(50, 0D);//From level 21 to 50
        aMap.put(100, 0.01D);//From level 51 to 100
        myMap = Collections.unmodifiableMap(aMap);
    }

    ExpConfig(){
        populateExponentMap();
    }

    private void populateExponentMap()
    {
        Map<Integer, Double> exponentMap = new LinkedHashMap<Integer,Double>();

        ExpConfig.myMap.forEach((k,v)-> {
            for(int counter=level;counter<=k;counter++,level++){
                exponent+=v;
                exponentMap.put(level, exponent);
                System.out.println(level + " : " + exponent);
            }        
        });  
    }

    //@TestMethod Run As Java Application
    public static void main(String[] args) throws IOException
    {
        ExpConfig asd = new ExpConfig();
    }
}

when i run the class as java application, i the construction i got a System.out.Print that verify the map values.

but actually the values are not expected.

1 : 1.8
2 : 1.81
3 : 1.82
4 : 1.83
5 : 1.84
6 : 1.85
7 : 1.86
8 : 1.87
9 : 1.8800000000000001
10 : 1.8900000000000001
11 : 1.9000000000000001
12 : 1.9100000000000001
13 : 1.9200000000000002
14 : 1.9300000000000002
15 : 1.9400000000000002
16 : 1.9500000000000002
17 : 1.9600000000000002
18 : 1.9700000000000002
19 : 1.9800000000000002
20 : 1.9900000000000002
21 : 1.9900000000000002
22 : 1.9900000000000002
23 : 1.9900000000000002
24 : 1.9900000000000002
25 : 1.9900000000000002
26 : 1.9900000000000002
27 : 1.9900000000000002
28 : 1.9900000000000002
29 : 1.9900000000000002
30 : 1.9900000000000002
31 : 1.9900000000000002
32 : 1.9900000000000002
33 : 1.9900000000000002
34 : 1.9900000000000002
35 : 1.9900000000000002
36 : 1.9900000000000002
37 : 1.9900000000000002
38 : 1.9900000000000002
39 : 1.9900000000000002
40 : 1.9900000000000002
41 : 1.9900000000000002
42 : 1.9900000000000002
43 : 1.9900000000000002
44 : 1.9900000000000002
45 : 1.9900000000000002
46 : 1.9900000000000002
47 : 1.9900000000000002
48 : 1.9900000000000002
49 : 1.9900000000000002
50 : 1.9900000000000002
51 : 2.0
52 : 2.01
53 : 2.0199999999999996
54 : 2.0299999999999994
55 : 2.039999999999999
56 : 2.049999999999999
57 : 2.0599999999999987
58 : 2.0699999999999985
59 : 2.0799999999999983
60 : 2.089999999999998
61 : 2.099999999999998
62 : 2.1099999999999977
63 : 2.1199999999999974
64 : 2.1299999999999972
65 : 2.139999999999997
66 : 2.149999999999997
67 : 2.1599999999999966
68 : 2.1699999999999964
69 : 2.179999999999996
70 : 2.189999999999996
71 : 2.1999999999999957
72 : 2.2099999999999955
73 : 2.2199999999999953
74 : 2.229999999999995
75 : 2.239999999999995
76 : 2.2499999999999947
77 : 2.2599999999999945
78 : 2.2699999999999942
79 : 2.279999999999994
80 : 2.289999999999994
81 : 2.2999999999999936
82 : 2.3099999999999934
83 : 2.319999999999993
84 : 2.329999999999993
85 : 2.3399999999999928
86 : 2.3499999999999925
87 : 2.3599999999999923
88 : 2.369999999999992
89 : 2.379999999999992
90 : 2.3899999999999917
91 : 2.3999999999999915
92 : 2.4099999999999913
93 : 2.419999999999991
94 : 2.429999999999991
95 : 2.4399999999999906
96 : 2.4499999999999904
97 : 2.45999999999999
98 : 2.46999999999999
99 : 2.4799999999999898
100 : 2.4899999999999896

the key is correct but the value is not, the expected result would be with only 2 decimal numbers.

I know the solution is rounding the values, but why is this happening?

RudiDudi
  • 455
  • 1
  • 7
  • 18
  • 1
    its due the fact how those numbers are stored internally ( binary system ). Its actually quite common that there is that nasty last number. There are even numbers that can't be expressed in binary and thus are represented in such fashion – Najzero Nov 10 '16 at 11:19
  • It looks like 1.87 can't be exactly represented in binary system so it gets rounded somewhere. – xenteros Nov 10 '16 at 11:21

0 Answers0