M2
M2 copied to clipboard
Real to rational promotion
Is this just the reality of how mpfr numbers work?
i1 : (1.2)_QQ
5404319552844595
o1 = ----------------
4503599627370496
o1 : QQ
i2 : (1.5)_QQ
3
o2 = -
2
o2 : QQ
In other words, is there something subtly flawed about doing it naively?
i1 : f = r -> 1 + position(try precision r else defaultPrecision,
e -> r * 10 - floor(r = r * 10) < 1e-15);
i2 : g = r -> (r * 10^(e := f(r)))_QQ / 10^e;
i3 : g(1.2)
6
o3 = -
5
o3 : QQ
i4 : g(12.3456)
7716
o4 = ----
625
o4 : QQ
i5 : g(pi)
3141592653589793
o5 = ----------------
1000000000000000
o5 : QQ
Probably the < 1e-15 part needs more thinking, is this the subtlety?
Techincally, RR objects are always going to be rational numbers whose denominators are a power of 2 since they're represented using binary. That's where the 4503599627370496 is coming from -- it's $2^{52}$. But I agree that 6/5 makes a lot more sense in this case.
I'm guessing instead of 1e-15, maybe it should be something like 10^(-floor(prec*log 2/log 10)) to account for precisions other than 53.
So would it make sense to change something just so things like (1.2)_QQ look reasonable, or is it not a bad idea?
I think it makes sense.