Better handle large numbers from external data files#3791
Merged
Conversation
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.
Member
Author
|
I can maybe add a note about this edge case in the documentation of the |
reknih
reviewed
Mar 25, 2024
Member
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
commented
Mar 25, 2024
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When deserializing JSON files, a
u64may be produced by serde, that will then be converted to ai64when using it as a TypstValue. However, some number that can fit in au64are too big for ai64.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:
jsonfunction 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