Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why clojure clojure.lang.Ratio 3/2 not=1.5

Tags:

math

clojure

I'm lost in clojure ratio. I can't understand it. Why do the equality and inequality tests behave this way?

(= 3/2 1.5)
;; false
(>= 3/2 1.5)
;; true
(> 3/2 1.5)
;;false
(not= 3/2 1.5)
;; true
like image 339
ipaomian Avatar asked Feb 02 '15 07:02

ipaomian


1 Answers

Use == for numerical comparisons where you want to know if two numbers represent the same number regardless of types:

user> (= 3/2 1.5)
false
user> (== 3/2 1.5)
true

Though keep in mind that == is only for numbers and throws if given something not a number.

user> (== :1 :1)
ClassCastException clojure.lang.Keyword cannot be cast to java.lang.Number  clojure.lang.Numbers.equiv (Numbers.java:206)
like image 82
Arthur Ulfeldt Avatar answered Oct 09 '22 15:10

Arthur Ulfeldt