Skip to content

Commit 70d7a28

Browse files
committed
Fix Float.parse to handle numbers of the form "-0.x"
Since Float.parse uses Integer.parse, the -0 would be parsed independently of what came after the decimal point and would become 0
1 parent 99b1d9b commit 70d7a28

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

lib/elixir/lib/float.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ defmodule Float do
2222
2323
"""
2424
@spec parse(binary) :: { float, binary } | :error
25+
26+
# Integer.parse will parse "-0" as 0, so it needs to be handled separately
27+
def parse(<< "-0.", rest :: binary >>) do
28+
case parse(<< ?., rest :: binary >>, 0) do
29+
:error -> :error
30+
{ parsed, remainder } -> { -parsed, remainder }
31+
end
32+
end
33+
2534
def parse(binary) when is_binary(binary) do
2635
case Integer.parse binary do
2736
:error -> :error

lib/elixir/test/elixir/float_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule FloatTest do
66
test :parse do
77
assert Float.parse("12") === {12.0, ""}
88
assert Float.parse("-12") === {-12.0, ""}
9+
assert Float.parse("-0.1") === {-0.1, ""}
910
assert Float.parse("123456789") === {123456789.0, ""}
1011
assert Float.parse("12.5") === {12.5, ""}
1112
assert Float.parse("12.524235") === {12.524235, ""}

0 commit comments

Comments
 (0)