Is there a string macro to create a Rational?
Why do you need a string macro?
I don't think so. Does 2//3
not work in your case? Or do you propose something that makes a rational from a common literal like 2.568?
I was thinking of something like rational"0.1"
where I would get 1//10
Basically, it's like rationalize(0.1)
So what is wrong with rationalize(0.1)
?
It has to jump through a lot of hoops to go from floating point to Rational. I think there is a direct interpretation of a finite decimal may be possible. For example, I would like these two statements to be equal:
julia> rationalize(0.29999999999999998)
3//10
julia> 29999999999999998 // 100000000000000000
14999999999999999//50000000000000000
function rational_from_string(s)
x, y = split(s, '.')
a = parse(Int, x)
b = parse(Int, y)
return a + b // 10^length(y)
end
Exactly, so
julia> macro rational_str(s)
rational_from_string(s)
end
@rational_str (macro with 1 method)
julia> rational"0.29999999999999998"
14999999999999999//50000000000000000
Should that be in Base?
No.
That's basically my question. I guess the answer is that it is not in Base because it is trivial to implement?
It's more that it's not in base because base doesn't need it
For the most part, I think Base tries to only include things it needs to building itself up and tries to leave stuff like this for packages or whatever. This isn't exactly an ideology that's uniformly shared among the whole developer community though.
I suppose the analogy I see here is to the @big_str
macro as in
julia> big"0.29999999999999998"
0.299999999999999979999999999999999999999999999999999999999999999999999999999999
I'll look into a small RationalDecimals.jl
package. For the moment, I just put this into a gist if you don't mind, @Fredrik Ekre :
https://gist.github.com/mkitti/37bbe8e4f32d72111c25feeb840fbc2b
@big_str
is actually used in the parser for big int literals
The devs have wanted to remove the Big_____
numbers from Base for a while now. The barrier to doing that has been exactly the parser integrations that Simeon mentions
julia> 100000000000000 |> typeof
Int64
julia> 1000000000000000000000000000 |> typeof
Int128
julia> 1000000000000000000000000000000000000000000000000000 |> typeof
BigInt
Last updated: Nov 22 2024 at 04:41 UTC