1

I'm observing some unexpected floating point behaviour in Elixir.

4.7 / 0.1 = 47.0 (good!)
4.8 / 0.1 = 47.9999999999 (bad!)
4.9 / 0.1 = 49 (good!)

While I understand the limitations of fp accuracy, in this case, the answer just looks wrong.

Curiously, I tried this in python as well, and got the same result, which is even more mysterious. When I changed the format to 4.8 * (1/0.1), I get the right answer (48.0).

What is going on here?

cjm2671
  • 18,348
  • 31
  • 102
  • 161

2 Answers2

3

This is actually a normal behavior for IEEE 754 floating point numbers, and unrelated to erlang/elixir. Python or Nodejs would return the same thing.

For more information, you can read this detailed explanation.

sabiwara
  • 2,775
  • 6
  • 11
0

While I understand the limitations of fp accuracy

Clearly not :-)

As always, if you need decimal precision, use Decimal:

iex(2)> Decimal.div(Decimal.new("4.8"), Decimal.new("0.1"))
#Decimal<48>
iex(3)> Decimal.mult(Decimal.new("4.8"), Decimal.new("10"))
#Decimal<48.0>
Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74