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

Better handle large numbers from external data files #3791

Merged
merged 5 commits into from
Mar 25, 2024
Merged

Conversation

elegaanz
Copy link
Contributor

When deserializing JSON files, a u64 may be produced by serde, that will then be converted to a i64 when using it as a Typst Value. However, some number that can fit in a u64 are too big for a i64.

So far, Typst overflowed when doing the conversion, leading to a negative value where a large positive value was expected. With this change, it will now use a floating point number internally to represent this integer. This comes at the cost of losing precision, but the value will be much closer to the expected one.

Other fixes to this issue were:

  • a saturating cast. It was not satisfying in my opinion because the value you would get would be very different from the one you would expect, making it useless.
  • using a string instead of an actual number. With that solution, you cannot easily do math on your data. It would also be confusing to have a number become of string type when imported in Typst.
  • report an error. I considered that it was better to have imprecise data than being completely blocked because of what the JSON file contains.
  • adding a parameter to the json function to tell whether or large numbers should result in an error, or in imprecisions. In my opinion, this option added quite a lot of complexity for a use case that most people will never encounter.

Fixes #3363

When deserializing JSON files, a `u64` may be
produced by serde, that will then be converted
to a `i64` when using it as a Typst `Value`.
However, some number that can fit in a `u64`
are too big for a `i64`.

So far, Typst overflowed when doing the
conversion, leading to a negative value where a
large positive value was expected. With this
change, it will now use a floating point number
internally to represent this integer. This comes
at the cost of losing precision, but the value
will be much closer to the expected one.

Other fixes to this issue were:
- a saturating cast. It was not satisfying in my
  opinion because the value you would get would be
  very different from the one you would expect,
  making it useless.
- using a string instead of an actual number. With
  that solution, you cannot easily do math on your
  data. It would also be confusing to have a
  number become of string type when imported in
  Typst.
- report an error. I considered that it was better
  to have imprecise data than being completely
  blocked because of what the JSON file contains.
- adding a parameter to the `json` function to
  tell whether or large numbers should result in
  an error, or in imprecisions. In my opinion,
  this option added quite a lot of complexity for
  a use case that most people will never encounter.
@elegaanz
Copy link
Contributor Author

I can maybe add a note about this edge case in the documentation of the json function too. What do you think?

@reknih
Copy link
Member

reknih commented Mar 25, 2024

I can maybe add a note about this edge case in the documentation of the json function too. What do you think?

Sounds good!

Also disable reference image generation for this
test.
Add a note about large numbers

Also make it explicit that values that are not
objects or arrays are supported.
@elegaanz elegaanz changed the title Better handle large numbers from JSON Better handle large numbers from external data files Mar 25, 2024
@reknih reknih enabled auto-merge March 25, 2024 16:48
@reknih reknih added this pull request to the merge queue Mar 25, 2024
Merged via the queue into main with commit 105d715 Mar 25, 2024
12 checks passed
@elegaanz elegaanz deleted the json-large-int branch March 26, 2024 11:30
laurmaedje pushed a commit that referenced this pull request May 16, 2024
Co-authored-by: Martin Haug <mhaug@live.de>
laurmaedje pushed a commit that referenced this pull request May 17, 2024
Co-authored-by: Martin Haug <mhaug@live.de>
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

Successfully merging this pull request may close these issues.

Large numbers in JSON lead to overflow
2 participants