Skip to content

Instantly share code, notes, and snippets.

@mkitti
Created April 13, 2021 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkitti/37bbe8e4f32d72111c25feeb840fbc2b to your computer and use it in GitHub Desktop.
Save mkitti/37bbe8e4f32d72111c25feeb840fbc2b to your computer and use it in GitHub Desktop.
Convert a decimal representation to a Rational in Julia
# Via Fredrik Ekre
# https://julialang.zulipchat.com/#narrow/stream/274208-helpdesk-.28published.29/topic/String.20macro.20for.20rationals.3F
function rational_from_string(s)
x, y = split(s, '.')
a = parse(Int, x)
b = parse(Int, y)
return a + b // 10^length(y)
end
macro rational_str(s)
rational_from_string(s)
end
rational"0.29999999999999998"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment