rationalize

added
1.0

ns
clojure.core

type
function

(rationalize num)

returns the rational value of num

                <pre>\r
user=> (rationalize 1.5)\r
3/2\r
</pre>
            
                user=> (rationalize Math/PI)
3141592653589793/1000000000000000

user=> (rationalize (Math/sqrt 2))
14142135623730951/10000000000000000
            
                (rationalize 2/4)
;; => 1/2

(rationalize 4/2)
;; => 2

(rationalize 2)
;; => 2

(rationalize 2.0)
;; => 2N
            
                ;; To quickly convert a mixed number to an improper fraction, 
;; multiply the denominator
;; by the whole number and add to the numerator

(= (+ 20 3/4) (rationalize (/ (+ (* 20 4) 3) 4)))
;; => true