Skip to content

Conversation

@justinj
Copy link
Contributor

@justinj justinj commented Dec 25, 2013

Fix for #1960, this is my first non-documentation PR to Elixir, so any feedback on how it could be improved would be appreciated :)

Since Float.parse uses Integer.parse, the -0 would be parsed independently of what came after the decimal point and would become 0.

Since Float.parse uses Integer.parse, the -0 would be
parsed independently of what came after the decimal
point and would become 0
@meh
Copy link
Contributor

meh commented Dec 25, 2013

Personally I would just have added a head for "-" <> rest and done parse(rest) instead of checking for -0..

@justinj
Copy link
Contributor Author

justinj commented Dec 25, 2013

I considered that, but that makes this test fail:

assert Float.parse("--1.2") === :error

@ericmj
Copy link
Member

ericmj commented Dec 25, 2013

@justinj I agree with @meh. Wth a slight modification it can work for your case as well. After matching "-" <> rest, instead of recursing down into parse(rest), call parse_unsign(rest) that should do the rest of the parsing.

See https://github.com/ericmj/decimal/blob/d4a0eb028f65c158fa8cb811492e0b06a70d7705/lib/decimal.ex#L993 for an example .

@justinj
Copy link
Contributor Author

justinj commented Dec 25, 2013

Thanks a lot for the link @ericmj, how does this look?

@ericmj
Copy link
Member

ericmj commented Dec 26, 2013

@justinj Perfect 👍

josevalim pushed a commit that referenced this pull request Dec 31, 2013
Fix Float.parse to handle numbers of the form "-0.x", fixes issue #1960
@josevalim josevalim merged commit e055c2c into elixir-lang:master Dec 31, 2013
@josevalim
Copy link
Member

❤️ 💚 💙 💛 💜

@justinj justinj deleted the float-parse-negative-0 branch January 1, 2014 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants