Skip to content
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

Clarify that Float does not include NaN or infinity #780

Merged
merged 3 commits into from
Apr 7, 2021

Conversation

glasser
Copy link
Contributor

@glasser glasser commented Sep 16, 2020

Fixes #778.

This matches the fact that you cannot represent these values in text as
FloatValue, as well as the graphql-js
implementation (https://github.com/graphql/graphql-js/blob/16009cbcb0109da03f2157a868817b886801095a/src/type/scalars.js#L108-L112).

This was perhaps already implied by the word "fractional", but "finite" seems to
be a more standard term for "IEEE 754 floats that are not infinity or NaN".

Copy link

@juriad juriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On lines 474/475 integer is specifically mentioned, however a similar problem can happen with float: 1e1000. Do we want to change the formulation there too?

@glasser
Copy link
Contributor Author

glasser commented Sep 17, 2020

@juriad is this suggestion directly related to my change, or a separate opportunity for improvement that could be considered in parallel? (I'm not sure what the answer to your question is and am maybe hoping to not have to :) )

@fedinskiy
Copy link

@glasser I assume that the mention of NaNs should be also removed from this section: https://spec.graphql.org/June2018/#sec-Value-Completion

@glasser
Copy link
Contributor Author

glasser commented Dec 10, 2020

@fedinskiy What change do you think is relevant there? This seems consistent with that mention: that mention implies that a NaN produced by execution in your program's native floating point type should be mapped to null, which is consistent with NaN not being a valid value for Float.

Base automatically changed from master to main February 3, 2021 04:50
@leebyron leebyron added the ✏️ Editorial PR is non-normative or does not influence implementation label Apr 5, 2021
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Apr 5, 2021

CLA Signed

The committers are authorized under a signed CLA.

@leebyron
Copy link
Collaborator

leebyron commented Apr 5, 2021

@glasser do you mind taking a look at the CLA bot?

@leebyron
Copy link
Collaborator

leebyron commented Apr 6, 2021

I think we need to readdress @fedinskiy's comment as well. In retrospect it seems wrong that NaN gets coerced to null instead of raising an error during value coercion.

glasser and others added 2 commits April 5, 2021 23:30
Fixes graphql#778.

This matches the fact that you cannot represent these values in text as
FloatValue, as well as the graphql-js
implementation (https://github.com/graphql/graphql-js/blob/16009cbcb0109da03f2157a868817b886801095a/src/type/scalars.js#L108-L112).

This was perhaps already implied by the word "fractional", but "finite" seems to
be a more standard term for "IEEE 754 floats that are not infinity or NaN".
@leebyron leebyron mentioned this pull request Apr 6, 2021
@leebyron leebyron force-pushed the glasser/finite-floats branch 3 times, most recently from 18d7924 to 0f46b62 Compare April 6, 2021 07:18
@@ -648,7 +648,7 @@ CompleteValue(fieldType, fields, result, variableValues):
* If {completedResult} is {null}, throw a field error.
* Return {completedResult}.
* If {result} is {null} (or another internal value similar to {null} such as
{undefined} or {NaN}), return {null}.
{undefined}), return {null}.
Copy link
Collaborator

@leebyron leebyron Apr 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is technically a normative change, not just editorial. However I did some checking and found the reference implementation (and most others) do not adhere to short-circuiting NaN to null.

They instead treat NaN as an invalid Float and throw an error, which is exactly the behavior now accurately described above.

Copy link
Contributor

@sungam3r sungam3r Apr 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GraphQL.NET throws on NaN.

@leebyron leebyron added this to the May2021 milestone Apr 6, 2021
@glasser
Copy link
Contributor Author

glasser commented Apr 6, 2021

Having trouble with the CLA but will keep iterating with LFX support (https://jira.linuxfoundation.org/plugins/servlet/theme/portal/4/SUPPORT-4646). Your updates to my text seem reasonable (I haven't verified your claims about the behavior of existing implementations with NaN but believe you!).

@glasser
Copy link
Contributor Author

glasser commented Apr 6, 2021

CLA fixed. I have used better web apps.

@leebyron
Copy link
Collaborator

leebyron commented Apr 6, 2021

Here's the relevant lines from GraphQL.js

If {result} is {null} (or another internal value similar to {null} such as {undefined}), return {null}.

https://github.com/graphql/graphql-js/blob/main/src/execution/execute.js#L756-L759

Re: my inline comment above and the related #836 which clarifies that NaN should fail to coerce and throw an error, GraphQL.js Float coercion for inputs and outputs does in fact throw for any non-finite value

https://github.com/graphql/graphql-js/blob/main/src/type/scalars.js#L96-L111

@leebyron leebyron merged commit 6dbef35 into graphql:main Apr 7, 2021
@glasser
Copy link
Contributor Author

glasser commented Apr 7, 2021

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✏️ Editorial PR is non-normative or does not influence implementation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Spec should be more explicit about whether NaN and Inf are valid Floats
6 participants