Skip to content

Commit 4135ab1

Browse files
tsuberyJosé Valim
authored andcommitted
Fix ceil function with zero as input (#5594)
Zero has special encoding as IEEE754 floating point number with a 0 value for exponent. This encoding requires special handling in current Float.round implementation. Signed-off-by: José Valim <jose.valim@plataformatec.com.br>
1 parent 89f7dbc commit 4135ab1

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

lib/elixir/lib/float.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ defmodule Float do
203203
count = count - exp + 1023
204204

205205
cond do
206-
count <= 0 -> # There is no decimal precision
206+
count <= 0 or # There is no decimal precision
207+
(0 == exp and <<0::52>> == significant) -> #zero or minus zero
207208
float
208209

209210
count >= 104 -> # Precision beyond 15 digits

lib/elixir/test/elixir/float_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ defmodule FloatTest do
8080
assert Float.ceil(0.32453e-10) === 1.0
8181
assert Float.ceil(-0.32453e-10) === 0.0
8282
assert Float.ceil(1.32453e-10) === 1.0
83+
assert Float.ceil(0.0) === 0.0
8384
end
8485

8586
test "ceil with precision" do
@@ -94,6 +95,8 @@ defmodule FloatTest do
9495

9596
assert Float.ceil(12.32453e-20, 2) === 0.01
9697
assert Float.ceil(-12.32453e-20, 2) === 0.0
98+
99+
assert Float.ceil(0.0, 2) === 0.0
97100
end
98101

99102
test "round" do

0 commit comments

Comments
 (0)