Skip to content

Commit 380c228

Browse files
committed
Fix Float.parse/1 inconsistent error handling for non-scientific notation overflow
`Float.parse/1` was returning `:error` for overflow in scientific notation (e.g., "1.0e400") but raising `ArgumentError` for equivalent overflow in non-scientific notation (e.g., a 310-digit number). This violated the documented behavior that states `Float.parse/1` should return `:error` when the float exceeds the maximum size.
1 parent 52c58c7 commit 380c228

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

lib/elixir/lib/float.ex

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,14 @@ defmodule Float do
201201
end
202202

203203
defp parse_unsigned(rest, dot?, false = _e?, acc) do
204-
float =
205-
acc
206-
|> add_dot(dot?)
207-
|> :lists.reverse()
208-
|> :erlang.list_to_float()
209-
210-
{float, rest}
204+
acc
205+
|> add_dot(dot?)
206+
|> :lists.reverse()
207+
|> :erlang.list_to_float()
208+
rescue
209+
ArgumentError -> :error
210+
else
211+
float -> {float, rest}
211212
end
212213

213214
defp add_dot(acc, true), do: acc

lib/elixir/test/elixir/float_test.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ defmodule FloatTest do
5757
assert Float.parse("1.7976931348623159e308") === :error
5858
assert Float.parse("1.7976931348623159e+308") === :error
5959
assert Float.parse("9e8363") === :error
60+
61+
# Non-scientific notation overflow
62+
assert Float.parse(String.duplicate("9", 310) <> ".0") === :error
63+
assert Float.parse("-" <> String.duplicate("9", 310) <> ".0") === :error
64+
assert Float.parse(String.duplicate("9", 310) <> ".0foo") === :error
6065
end
6166

6267
test "floor/1" do

0 commit comments

Comments
 (0)