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

Small numbers are rounded to zero on calculation #2685

Closed
itchyny opened this issue Jul 9, 2023 · 7 comments · Fixed by #2804
Closed

Small numbers are rounded to zero on calculation #2685

itchyny opened this issue Jul 9, 2023 · 7 comments · Fixed by #2804
Labels
Milestone

Comments

@itchyny
Copy link
Contributor

itchyny commented Jul 9, 2023

Describe the bug
In the latest version of jq, jq -n '1 / 1e-17' exits with Division by zero error, but should emit 1e+17.

To Reproduce
Run jq -n '1 / 1e-17'.

Expected behavior
Since 1e-17 is not small enough to be rounded to zero, 1e+17 is expected, just like jq 1.6.

Environment (please complete the following information):

  • Both macOS, Ubuntu (on Docker).
  • jq version: jq-1.6-250-gce3701f

Additional context
Likely a regression of cf4b48c.

@itchyny itchyny added the bug label Jul 9, 2023
@itchyny itchyny added this to the 1.7 release milestone Jul 9, 2023
@itchyny
Copy link
Contributor Author

itchyny commented Jul 10, 2023

Caused by jvp_literal_number_to_double, where it reduces the number literal even if it has exponent.

@itchyny
Copy link
Contributor Author

itchyny commented Jul 11, 2023

Another test case is 1 / 1.00000000000000000000000000001e-17. This really needs to be reduced, but should not be truncated to zero.

@itchyny itchyny changed the title 1e-17 is not zero Small numbers are rounded to zero on calculation Jul 28, 2023
@nicowilliams
Copy link
Contributor

I'm insufficiently familiar with decNumber. @leonid-s-usov, do you know what the fix would be here?

@itchyny
Copy link
Contributor Author

itchyny commented Aug 1, 2023

I was thinking of using decimal64/decimal64ToString but feel some difficulties in DECNUMDIGITS. Opened #2804.

@leonid-s-usov
Copy link
Contributor

I've contemplated whether to enable decnum division in the original pull request, but that seemed too big of a change since it may greatly affect the performance. For now, any math attempted on numbers will "resolve" it down to the internal floating-point representation, which is reflected in the docs:

Math
jq currently only has IEEE754 double-precision (64-bit) floating point number support.

@nicowilliams
Copy link
Contributor

@leonid-s-usov this issue comes up just in conversion of literals to doubles. We don't need to consider making arithmetic operations work with decNumber yet to deal with this issue.

@leonid-s-usov
Copy link
Contributor

@nicowilliams yes, correct. With @itchyny 's help we found the issue with rounding a decNumber to floating point precision and it's passing the test now

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

Successfully merging a pull request may close this issue.

3 participants