Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upFix String.toFloat #342
Conversation
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Dandandan
Aug 20, 2015
Contributor
Are you sure NaN / Infinity should even be handled (as they are not available as literals in Elm)?
|
Are you sure |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
slava-sh
Aug 20, 2015
Contributor
I think that toFloat (toString someFloat) should not throw. There are no NaN and Infinity literals in Haskell and read . show is an identity.
|
I think that |
jvoigtlaender
referenced this pull request
Jan 19, 2016
Closed
fix result of String.toFloat "." (#336) #343
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
jvoigtlaender
Jan 19, 2016
Contributor
How is this pull request related to https://github.com/elm-lang/core/pull/343?
|
How is this pull request related to https://github.com/elm-lang/core/pull/343? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
slava-sh
Jan 19, 2016
Contributor
This PR improves String.toFloat by disallowing dangling dots and adding proper handling of NaN and Infinity:
> String.toFloat ".1"
Err ("could not convert string '.1' to a Float") : Result.Result String Float> 0 / 0
NaN : Float
> String.toFloat (toString (0 / 0))
Ok NaN : Result.Result String Float> -1 / 0
-Infinity : Float
> String.toFloat (toString (-1 / 0))
Ok -Infinity : Result.Result String FloatDisallowing dangling dots is reasonable because Elm itself does not support them:
> 1 + .1
-- SYNTAX PROBLEM -------------------------------------------- repl-temp-000.elm
I ran into something unexpected when parsing your code!
3│ 1 + .1
^The other PR fixes #336 by making String.toFloat "." return an Err. It does not fix String.toFloat "-.", which is still an Ok NaN.
|
This PR improves > String.toFloat ".1"
Err ("could not convert string '.1' to a Float") : Result.Result String Float> 0 / 0
NaN : Float
> String.toFloat (toString (0 / 0))
Ok NaN : Result.Result String Float> -1 / 0
-Infinity : Float
> String.toFloat (toString (-1 / 0))
Ok -Infinity : Result.Result String FloatDisallowing dangling dots is reasonable because Elm itself does not support them: > 1 + .1
-- SYNTAX PROBLEM -------------------------------------------- repl-temp-000.elm
I ran into something unexpected when parsing your code!
3│ 1 + .1
^The other PR fixes #336 by making |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
jvoigtlaender
Jan 19, 2016
Contributor
Ah, I see, thanks for clarifying. So, just to be sure: Does this PR solve a superset of the problems that the other PR solves? So the "." case is handled here as well?
My original question was because both PRs are mentioned to address #336, and the code changes are overlapping, so there would be a conflict if one gets merged.
Am I right that your position is that the other PR should be closed and this one merged?
What about Evan's comment over there, about first using a regular expression to reject invalid strings? Maybe that would simplify the solution here as well, since after that check has passed there are fewer cases to consider?
|
Ah, I see, thanks for clarifying. So, just to be sure: Does this PR solve a superset of the problems that the other PR solves? So the "." case is handled here as well? My original question was because both PRs are mentioned to address #336, and the code changes are overlapping, so there would be a conflict if one gets merged. Am I right that your position is that the other PR should be closed and this one merged? What about Evan's comment over there, about first using a regular expression to reject invalid strings? Maybe that would simplify the solution here as well, since after that check has passed there are fewer cases to consider? |
jvoigtlaender
referenced this pull request
Jan 19, 2016
Closed
String.toFloat "." yields Ok NaN #336
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
slava-sh
Jan 19, 2016
Contributor
Yes, that's right.
A regex would indeed simplify the code. Also, Evan's regex allows e notation (e.g. toFloat "1.2e3"), which is currently not supported but should probably be.
|
Yes, that's right. A regex would indeed simplify the code. Also, Evan's regex allows e notation (e.g. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
mgold
Jan 21, 2016
Contributor
e notation is valid as a literal and therefore should be valid in String.toFloat. (Similarly, hex literals should be accepted by String.toInt, although they currently are not.)
|
e notation is valid as a literal and therefore should be valid in |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
jvoigtlaender
Jan 22, 2016
Contributor
So, @slava-sh, are you willing to revise your pull request by using a regex and also supporting e notation?
|
So, @slava-sh, are you willing to revise your pull request by using a regex and also supporting e notation? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
Yes, I'll do this. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
|
Done. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
jvoigtlaender
referenced this pull request
Jan 31, 2016
Closed
Json.Decode.float chokes on Infinity #496
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
evancz
Sep 22, 2016
Member
I added this to #721, but it is very unclear that "Fix String.toFloat" is a legitimate title for this suggestion. In any case, it is now tracked in a meta issue so these decisions can be made in a comprehensive way.
|
I added this to #721, but it is very unclear that "Fix String.toFloat" is a legitimate title for this suggestion. In any case, it is now tracked in a meta issue so these decisions can be made in a comprehensive way. |
slava-sh commentedAug 11, 2015
toFloat "1."andtoFloat ".1"now result in an error.Closes #336.