json
json copied to clipboard
Impl PartialOrd for Number
It seems possible to implement both of these for Number.
Integer to float comparisons are hard. In particular, (i as f64) < f
is not always equal to i < (f as u64)
. Any ideas for how to implement this?
If #244 were done then perhaps this could be implemented more easily. We also know that the number isn't NaN
so we could implement Ord
.
I guess only PartialOrd would work, then:
given Numbers a and b:
if a and b are both i64
compare as i64
if a and b are both u64
compare as u64
if a is f64 and b is f64
compare as f64
if a is u64 and b is i64
if b < 0
(a>b)
else
compare a vs (b as u64)
if a is i64 and b is u64
invert compare b, a
if a is f64 and b is u64
if b < max-int-representable-by-f64 (2^53?)
compare a, (b as f64)
else if b < floor(a)
(b<a)
else if b > ceil(a)
(a>b)
else
undecidable
if a is f64 and b is i64
if b < 0
invert compare -a, -b
else
compare a, (b as u64)
... assume the rest are defined the same way
Are there any flaws with this?
As discussed in the PR: I am concerned that this would limit our ability to implement arbitrary precision number parsing in the future. In particular, #252 would have wanted to replace Number with struct Number { n: String }
where PartialOrd becomes way more difficult to define.
Let's keep this issue open and follow up after arbitrary-precision numbers have been settled.