jsonschema-rs icon indicating copy to clipboard operation
jsonschema-rs copied to clipboard

Arbitrary precision numbers

Open Stranger6667 opened this issue 4 years ago • 5 comments

Currently, we can't properly handle some large numbers that e.g. are bigger than u64, but the spec allows it

Stranger6667 avatar Jun 09 '20 20:06 Stranger6667

@Stranger6667 It was an issue that once I've reported to serde-json (I can find the issue eventually) and I was suggesting to allow support for (i|u)128, but the general community was against it as it might lead to poorer performances especially if you don't have hardware supporting it properly. The current suggestion might be to use serde with arbitrary_precision feature (but then the number will be a string and keyword validators relying on math operations will be penalised)

macisamuele avatar Jun 10 '20 22:06 macisamuele

I was trying to play around the concept of perfect precision via rug library. The concept was:

  • Integer can be represented perfectly if we assume unbounded number of bit
  • floating points can be perfectly represented if we consider the rational form (as after all json strings are written on the wire)

The results were very nice from a correctness point of view but it induce a HUGE performance regression. I'll try to formalise a simple PR example for that and we, eventually, might decide to integrate it behind a feature or just decide that it won't be worth the performance penalty caused by it

macisamuele avatar Jun 21 '20 21:06 macisamuele

@macisamuele

Would love to see that PR :)

Stranger6667 avatar Jun 21 '20 22:06 Stranger6667

The branch is currently on https://github.com/macisamuele/jsonschema-rs/tree/maci-perfect-precision It's not into a reviewable state due to the big number of tests commented out (mostly related to error messages)

I'll try to re-arrange it a bit (and remove perfect_precision from the default features) before publishing the PR, but in the meantime you can check the approach ;)

macisamuele avatar Jun 21 '20 22:06 macisamuele

I would love to have something like this. I would like to use this library in the context of validating inputs against a json schema but where the numeric inputs can exceed the precision representable by f64. Without an opt-in change like this I can't use this library for the task. An opt-in to trade some performance for correctness to meet this use case would unblock this!

DanielBauman88 avatar Oct 03 '22 04:10 DanielBauman88