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

Consider parsing -0 as a double and not as zero #1532

Open
lemire opened this issue Mar 31, 2021 · 7 comments
Open

Consider parsing -0 as a double and not as zero #1532

lemire opened this issue Mar 31, 2021 · 7 comments

Comments

@lemire
Copy link
Member

lemire commented Mar 31, 2021

Currently, we parse -0 as 0 (integer value) and -0.0 as (-0, double value) in the DOM API.

@lemire lemire added this to the 1.0 milestone Mar 31, 2021
@lemire
Copy link
Member Author

lemire commented Mar 31, 2021

Python behaves like simdjson:

>>> import math
>>> import json
>>> math.copysign(1, json.loads("-0"))
1.0
>>> math.copysign(1, json.loads("0"))
1.0
>>> math.copysign(1, -0.0)
-1.0
>>> math.copysign(1, json.loads("-0.0"))
-1.0

@lemire
Copy link
Member Author

lemire commented Mar 31, 2021

I am tempted to close this and leave "as is".

The fix would be simple enough:

is_float |= ((i == 0) && negative);

But any user that really cares about -0 and wants portability would almost surely be careful enough to serialize it as "-0.0".

Meanwhile, it is really subjective what -0 means. It is clear what -0.0 but many people would interpret -0 as zero.

@lemire lemire modified the milestones: 1.0, 2.0 Jul 27, 2021
@jkeiser
Copy link
Member

jkeiser commented Aug 13, 2021

The description indicates DOM already treats -0 as double, is that true? Is this about On Demand? In the On Demand API, you have to ask for either int or double, so I assume the issue is that getDouble() is returning double(+0.0)?

@lemire
Copy link
Member Author

lemire commented Aug 13, 2021

@jkeiser The issue is that integer types in C++ have no notion of -0 but doubles do. So some people would consider -0 to be a double.

We process -0 as the integer 0, and -0.0 as the double value -0.

In On Demand, we no longer distinguish between ints and doubles for the user. Still, if someone encountered -0 and decided to parse it as an integer, they would get 0, if they want it as a double, they would get -0.

so I assume the issue is that getDouble() is returning double(+0.0)?

I think you would get -0 actually.

@jkeiser
Copy link
Member

jkeiser commented Aug 14, 2021

Oh, so the idea is that if you ask for an integer, we treat -0 as INCORRECT_TYPE?

@lemire
Copy link
Member Author

lemire commented Aug 15, 2021

@jkeiser Yes!

But I am not sure I like this idea, but as you can see it is logical.

@jkeiser
Copy link
Member

jkeiser commented Aug 16, 2021

Yeah. I feel sort of positive about the principle that we refuse to parse to a type if it loses information that would be preserved if we parsed to a different type.

A compelling argument could be made that it is obvious which integer to assign this value, but we refuse to parse 1.1 to 1, and it seems like that would apply to integers as well.

On the other hand, we refuse to parse 1.0 as an integer ... that doesn't fit the principle I suggested above. We don't preserve the number of decimal places when we parse doubles with trailing zeroes, so I don't think we can claim we're trying to preserve the number of significant digits ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants