Hello I am currently working with a Map in Java. I want to map Integers in the range 0 to 40 to Floats in the range 0.8 to 1.2 (in 0.1 intervals). So 0 maps to 0.0, 20 maps to 1.0 and 40 maps to 1.2, you get the idea. But when I was checking the output if everything worked fine I noticed that something weird happened to my float values (see Output). Why is this happening and how can I fix it so the values in my map are actually values in the range 0.8-1.2 in 0.1 intervals?
Edit: Also if there is a better way to map my Int values to Floats I welcome your suggestions, doing this with a map was just my first thought :)
private Map<Integer, Float> myMap= new HashMap<>();
float value = 0.8f;
for (int i = 0; i <= 40; i++) {
myMap.put(i, value);
value += 0.01f;
}
System.out.println(myMap.entrySet());
Output:
[0=0.8, 1=0.81, 2=0.82, 3=0.83, 4=0.84, 5=0.84999996, 6=0.85999995, 7=0.86999995,
8=0.87999994, 9=0.8899999, 10=0.8999999, 11=0.9099999, 12=0.9199999, 13=0.9299999,
14=0.9399999, 15=0.94999987, 16=0.95999986, 17=0.96999985, 18=0.97999984, 19=0.98999983,
20=0.9999998, 21=1.0099999, 22=1.0199999, 23=1.0299999, 24=1.0399998, 25=1.0499998,
26=1.0599998, 27=1.0699998, 28=1.0799998, 29=1.0899998, 30=1.0999998, 31=1.1099998,
32=1.1199998, 33=1.1299998, 34=1.1399997, 35=1.1499997, 36=1.1599997, 37=1.1699997,
38=1.1799997, 39=1.1899997, 40=1.1999997]