comer
comer

Reputation: 321

How to do rational arithmetic in julia?

I am writing some functions in julia and want the results to be represented as rational numbers. That is, if a function returns 1/2, 1/3, 13/2571 etc I want them to be returned as written and not converted to floats. Say the functions compute some coefficients by some iterative process and I want the coefficient values to be shown as rationals. How can I do that in julia?

Upvotes: 1

Views: 464

Answers (1)

one-more-minute
one-more-minute

Reputation: 1406

Rationals in Julia can be written as

1//2

These will work with functions, including user-defined ones, as you would expect:

5//7*3//5 # results in 3//7

f(x) = x^2 - 1

f(3//4) # results in -7//16

There's really not much else to it, but see also the manual section. If there's something in particular that's not working for you, post some example code and I'll take a look.

Upvotes: 6

Related Questions