0

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]
CoffeeKid
  • 123
  • 5
  • Well that is the cause of my problem, but what would be the fix for it :D ? – CoffeeKid Nov 04 '21 at 20:51
  • 2
    It depends. What is your actual need? What do you need these numbers for? – Matteo NNZ Nov 04 '21 at 20:53
  • 1
    @CoffeeKid Don't use floating point types. – Turing85 Nov 04 '21 at 20:53
  • I want to multiply another value, lets call it "y" by a factor between 0.8 and 1.2 (the values in my map) depending on the key I get from a function. – CoffeeKid Nov 04 '21 at 20:56
  • 1
    @CoffeeKid would it be possible for you to work with integer values two orders of magnitude bigger and only add a dot character before the last two digits when displaying the numbers? – Federico klez Culloca Nov 04 '21 at 20:59
  • @CoffeeKid there is a reason [why floating numbers in programming are not exact](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) - and it's not only Java, it's really any language. So there's no "fix for it" unfortunately. If you need to get an exact number, you can round the multiplication result to two digits. But again, it depends what you need to do with these numbers (show them on a GUI? Use them to express a wire transfer?...), so saying "I want to multiply" is not enough unfortunately. – Matteo NNZ Nov 04 '21 at 21:03
  • Thanks for all the useful answers, I fixed my problem. I will look a bit more into the topic of floating point numbers and how to deal with them (or how to avoid using them :D). – CoffeeKid Nov 04 '21 at 21:09
  • 1
    For accuracy, use `BigDecimal` objects rather than `float`/`double` floating-point types. `Map< Integer , BigDecimal >` – Basil Bourque Nov 04 '21 at 21:51

0 Answers0