Decimal Literals
jmaessen@alum.mit.edu
jmaessen@alum.mit.edu
Fri, 11 Jul 2003 16:27:38 -0400
Ketil Z. Malde <ketil@ii.uib.no> writes:
> I.e. for 3.14, allow any rational number in [3.14,3.15), or
> perhaps (3.135,3.145]? Or restrict it to just the rational number in
> range with the least total digits?
We can get this effect using a version of (%) adapted specifically for
converting decimal floating-point constants. Thus:
Data.Ratio> 31415926536%1000000000
3926990817 % 125000000
Data.Ratio> let n %% d = approxRational (n%d) (1%(2*d)) in
31415926536 %% 10000000000
312689 % 99532
We can imagine the compiler replacing floating constants by
fromRational (n%%d) rather than fromRational (n%d).
Of course, then trailing zeros suddenly become significant:
Data.Ratio> let n %% d = approxRational (n%d) (1%(2*d)) in 314 %% 100
22 % 7
Data.Ratio> let n %% d = approxRational (n%d) (1%(2*d)) in 3140 %% 1000
135 % 43
This might be good or bad, depending on your perspective.
-Jan-Willem Maessen
jmaessen@alum.mit.edu