How to convert any number to a clojure.lang.Ratio type in Clojure?
Asked Answered
B

1

7

In Scheme I can do:

#;> (numerator 1/3)
1
#;> (denominator 1/3)
3

In Clojure I can do something similar:

user=> (numerator 1/3)
1
user=> (denominator 1/3)
3

But in Scheme I can do:

#;> (numerator 0.3)
3.0

and it is not possible in Clojure:

user=> (numerator 0.3)

ClassCastException java.lang.Double cannot be cast to clojure.lang.Ratio  clojure.core/numerator (core.clj:3306)

How can I convert a Double (or actually any kind of number) into a clojure.lang.Ratio?

In Scheme we have also inexact->exact what would be something like "double to ratio" in Clojure, but I can't find anything similar to it.

Ballenger answered 8/8, 2014 at 1:31 Comment(0)
P
12

Ooh, I know this one!

user=> (rationalize 0.3)
3/10
user=> (numerator (rationalize 0.3))
3

But the OP points out that this doesn't work for all numbers:

user=> (numerator (rationalize 1))
ClassCastException java.lang.Long cannot be cast to clojure.lang.Ratio  clojure.core/numerator (core.clj:3306)

see his Java interop workaround in his answer.


[edit] OP here:

Here is a more generic solution:

user=> (numerator (clojure.lang.Numbers/toRatio (rationalize 1)))
1
user=> (numerator (clojure.lang.Numbers/toRatio (rationalize 0.3)))
3
user=> (numerator (clojure.lang.Numbers/toRatio (rationalize 1/3)))
1
Phalanger answered 8/8, 2014 at 1:48 Comment(6)
Unfortunately does not work for every number, see: user=> (numerator (rationalize 1)) ClassCastException java.lang.Long cannot be cast to clojure.lang.Ratio clojure.core/numerator (core.clj:3306)Ballenger
You are right. That seems unfortunate - I wonder if it is intended that (rationalize 1) returns a Long?Phalanger
It seems to be a performance hit, but it is a weird behavior.Ballenger
(rational? 3) returns true, but (numerator 3) blows up. Can't be right.Proscenium
Where is @Rich Hickey? :)Ballenger
Looks like a bug and I've found no evidence of it already being reported. IMO the numerator should be fixed, since integers are also rational numbers by definition of the latter. "Type-safe-me", however, tells me that rationalize should always return an instance of Ratio. Huh.Configurationism

© 2022 - 2024 — McMap. All rights reserved.