ClojureDocs

导航

命名空间

rationalize

clojure.core

自 1.0 起可用 (源代码)
  • (rationalize num)
returns the rational value of num
4 Examples
<pre>

user=> (rationalize 1.5)

3/2

</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
See Also
No see-alsos for clojure.core/rationalize
1 Note
    By , created 11.3 years ago

    Remember that for irrational numbers, like sqrt 2, this is only an estimate (pretty good one).