Created
April 13, 2021 17:01
-
-
Save mkitti/37bbe8e4f32d72111c25feeb840fbc2b to your computer and use it in GitHub Desktop.
Convert a decimal representation to a Rational in Julia
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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