New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix String.toFloat #342

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
5 participants
@slava-sh
Contributor

slava-sh commented Aug 11, 2015

  • Added handling of NaN and Infinity.
  • toFloat "1." and toFloat ".1" now result in an error.

Closes #336.

@Dandandan

This comment has been minimized.

Show comment
Hide comment
@Dandandan

Dandandan Aug 20, 2015

Contributor

Are you sure NaN / Infinity should even be handled (as they are not available as literals in Elm)?

Contributor

Dandandan commented Aug 20, 2015

Are you sure NaN / Infinity should even be handled (as they are not available as literals in Elm)?

@slava-sh

This comment has been minimized.

Show comment
Hide comment
@slava-sh

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.

Contributor

slava-sh commented Aug 20, 2015

I think that toFloat (toString someFloat) should not throw. There are no NaN and Infinity literals in Haskell and read . show is an identity.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jan 19, 2016

Contributor

How is this pull request related to https://github.com/elm-lang/core/pull/343?

Contributor

jvoigtlaender commented Jan 19, 2016

How is this pull request related to https://github.com/elm-lang/core/pull/343?

@slava-sh

This comment has been minimized.

Show comment
Hide comment
@slava-sh

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 Float

Disallowing 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!

31 + .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.

Contributor

slava-sh commented Jan 19, 2016

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 Float

Disallowing 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!

31 + .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.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

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?

Contributor

jvoigtlaender commented Jan 19, 2016

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?

@slava-sh

This comment has been minimized.

Show comment
Hide comment
@slava-sh

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.

Contributor

slava-sh commented Jan 19, 2016

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.

@mgold

This comment has been minimized.

Show comment
Hide comment
@mgold

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.)

Contributor

mgold commented Jan 21, 2016

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.)

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

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?

Contributor

jvoigtlaender commented Jan 22, 2016

So, @slava-sh, are you willing to revise your pull request by using a regex and also supporting e notation?

@slava-sh

This comment has been minimized.

Show comment
Hide comment
@slava-sh

slava-sh Jan 22, 2016

Contributor

Yes, I'll do this.

Contributor

slava-sh commented Jan 22, 2016

Yes, I'll do this.

@slava-sh

This comment has been minimized.

Show comment
Hide comment
@slava-sh

slava-sh Jan 23, 2016

Contributor

Done.

Contributor

slava-sh commented Jan 23, 2016

Done.

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jan 24, 2016

Contributor

Cool!

I think this should be part of a bugfix release core-3.0.1, see here, but it's @evancz's to decide.

Contributor

jvoigtlaender commented Jan 24, 2016

Cool!

I think this should be part of a bugfix release core-3.0.1, see here, but it's @evancz's to decide.

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

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.

Member

evancz commented Sep 22, 2016

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.

@evancz evancz closed this Sep 22, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment