Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions lib/elixir/lib/float.ex
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,9 @@ defmodule Float do
when exp_marker in ~c"eE" and sign in ~c"-+" and digit in ?0..?9,
do: parse_unsigned(rest, true, true, [digit, sign, ?e | add_dot(acc, dot?)])

# When floats are expressed in scientific notation, :erlang.binary_to_float/1 can raise an
# ArgumentError if the e exponent is too big. For example, "1.0e400". Because of this, we
# rescue the ArgumentError here and return an error.
defp parse_unsigned(rest, dot?, true = _e?, acc) do
# :erlang.binary_to_float/1 can raise an ArgumentError if the e exponent is too big. For example,
# "1.0e400". Because of this, we rescue the ArgumentError here and return an error.
defp parse_unsigned(rest, dot?, _, acc) do
acc
|> add_dot(dot?)
|> :lists.reverse()
Expand All @@ -200,16 +199,6 @@ defmodule Float do
float -> {float, rest}
end

defp parse_unsigned(rest, dot?, false = _e?, acc) do
float =
acc
|> add_dot(dot?)
|> :lists.reverse()
|> :erlang.list_to_float()

{float, rest}
end

defp add_dot(acc, true), do: acc
defp add_dot(acc, false), do: [?0, ?. | acc]

Expand Down
5 changes: 5 additions & 0 deletions lib/elixir/test/elixir/float_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ defmodule FloatTest do
assert Float.parse("1.7976931348623159e308") === :error
assert Float.parse("1.7976931348623159e+308") === :error
assert Float.parse("9e8363") === :error

# Non-scientific notation overflow
assert Float.parse(String.duplicate("9", 310) <> ".0") === :error
assert Float.parse("-" <> String.duplicate("9", 310) <> ".0") === :error
assert Float.parse(String.duplicate("9", 310) <> ".0foo") === :error
end

test "floor/1" do
Expand Down