Stream: helpdesk (published)

Topic: String macro for rationals?


view this post on Zulip Mark Kittisopikul (Apr 13 2021 at 14:35):

Is there a string macro to create a Rational?

view this post on Zulip Fredrik Ekre (Apr 13 2021 at 16:00):

Why do you need a string macro?

view this post on Zulip Florian Große (Apr 13 2021 at 16:01):

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?

view this post on Zulip Mark Kittisopikul (Apr 13 2021 at 16:08):

I was thinking of something like rational"0.1" where I would get 1//10

view this post on Zulip Mark Kittisopikul (Apr 13 2021 at 16:08):

Basically, it's like rationalize(0.1)

view this post on Zulip Fredrik Ekre (Apr 13 2021 at 16:11):

So what is wrong with rationalize(0.1)?

view this post on Zulip Mark Kittisopikul (Apr 13 2021 at 16:20):

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

view this post on Zulip Fredrik Ekre (Apr 13 2021 at 16:25):

function rational_from_string(s)
    x, y = split(s, '.')
    a = parse(Int, x)
    b = parse(Int, y)
    return a + b // 10^length(y)
end

view this post on Zulip Mark Kittisopikul (Apr 13 2021 at 16:26):

Exactly, so

julia> macro rational_str(s)
           rational_from_string(s)
       end
@rational_str (macro with 1 method)

julia> rational"0.29999999999999998"
14999999999999999//50000000000000000

view this post on Zulip Mark Kittisopikul (Apr 13 2021 at 16:27):

Should that be in Base?

view this post on Zulip Fredrik Ekre (Apr 13 2021 at 16:28):

No.

view this post on Zulip Mark Kittisopikul (Apr 13 2021 at 16:58):

That's basically my question. I guess the answer is that it is not in Base because it is trivial to implement?

view this post on Zulip Mason Protter (Apr 13 2021 at 17:34):

It's more that it's not in base because base doesn't need it

view this post on Zulip Mason Protter (Apr 13 2021 at 17:36):

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.

view this post on Zulip Mark Kittisopikul (Apr 13 2021 at 19:45):

I suppose the analogy I see here is to the @big_str macro as in

julia> big"0.29999999999999998"
0.299999999999999979999999999999999999999999999999999999999999999999999999999999

view this post on Zulip Mark Kittisopikul (Apr 13 2021 at 19:46):

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

view this post on Zulip Simeon Schaub (Apr 13 2021 at 20:08):

@big_str is actually used in the parser for big int literals

view this post on Zulip Mason Protter (Apr 13 2021 at 20:25):

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

view this post on Zulip Mason Protter (Apr 13 2021 at 20:25):

julia> 100000000000000 |> typeof
Int64

julia> 1000000000000000000000000000 |> typeof
Int128

julia> 1000000000000000000000000000000000000000000000000000 |> typeof
BigInt

Last updated: Nov 06 2024 at 04:40 UTC