round rounding 2.4999999999999999 up instead of down

175 views
Skip to first unread message

Rudolf Bargholz

unread,
Nov 19, 2015, 12:15:41 PM11/19/15
to elixir-lang-talk
Hi,

Windows 10
Elixir 1.1.1
Erlang/OTP 18 [64-bit]

Is this expected behaviour, or not?

  iex(37)> round 2.499999999999999 
  2
  iex(38)> round 2.4999999999999999
  3

Regards

Rudolf

r...@dimpixel.com

unread,
Nov 19, 2015, 12:29:54 PM11/19/15
to elixir-lang-talk
I'm not familiar with the reason why, but it appears to be expected behavior based on what I'm seeing in other languages. When evaluating 2.4999999999999999 in Elixir, Ruby, Go and Python 2.5 is returned, which would change your rounding results.

iex(1)> 2.4999999999999999
2.5

- Rob

John W Higgins

unread,
Nov 19, 2015, 12:30:02 PM11/19/15
to elixir-l...@googlegroups.com
Yes, you have absolutely no guarantees when you are trying to round floating point numbers - the 2.4999999999999999 number most likely does not exist exactly as a floating point number so it ends up becoming something like 2.5 which rounds up to 3.

You cannot use floating point numbers and assume anything concerning whether or not you will have some issue with regards to the exact representation of the number.

There are multiple examples of this that exist in any language when using floating point numbers. If you require such precision then you need to use a decimal library which allows for the exact representation of the values.

John

Jim Freeze

unread,
Nov 19, 2015, 12:30:21 PM11/19/15
to elixir-l...@googlegroups.com
My guess is that 2.499999999999999  is < 2.5
and 2.4999999999999999 is == to 2.5

iex(1)> 2.4999999999999999 == 2.5
true
iex(2)> 2.499999999999999 == 2.5
false

Jim

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/99b6063d-e591-43f9-90ce-667c85bcea43%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Dr. Jim Freeze, Ph.D.

Rudolf Bargholz

unread,
Nov 19, 2015, 12:36:35 PM11/19/15
to elixir-lang-talk
Thanks to all for the clarification. Was just a little unsure how to interpret the result.

Rudolf

Robert Virding

unread,
Nov 19, 2015, 7:39:18 PM11/19/15
to elixir-lang-talk
Floating point numbers often don't behave as you would expect, one major reason is that we see them as decimal while they are implemented as binary. For example 0.1 has no exact representation. Here are a couple of sights which discuss the problem:

http://floating-point-gui.de/
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Robert

Phani Mahesh

unread,
Nov 23, 2015, 11:08:34 AM11/23/15
to elixir-lang-talk
Related: Visit http://0.30000000000000004.com. You may find this interesting and/or amusing.

Ed W

unread,
Nov 25, 2015, 12:56:22 PM11/25/15
to elixir-l...@googlegroups.com
The other answers are also partly correct, but Elixir round function is broken and won't behave as you desire - don't use it.  I opened a bug report some months back, but it doesn't seem to have reached a conclusion I think?

If you need to accurately round a float, eg you are being naughty and doing currency calculations using a float and want to round back to actual money values at the end, then use the decimal library and it's rounding methods. 

Good luck

Ed W
Reply all
Reply to author
Forward
0 new messages