Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure: = vs ==

I'm struggling with understanding why

(= 1 1.0)

evaluates to false whereas

(== 1 1.0)

evaluates to true. According to the docs, == seems to be only working on numbers but other than that there doesn't seem much difference. So, what am I missing?

like image 236
helpermethod Avatar asked Dec 09 '13 20:12

helpermethod


1 Answers

== checks for mathematical equivalence. = with numbers checks for equivalence in a way that is agnostic to size where applicable, but is strict about representation:

user> (= (float 1.0) (double 1.0))
true
user> (= (int 1) (byte 1))
true
user> (= (int 1) (double 1))
false
user> (= 0.5 (/ 1 2))
false
user> (== 0.5 (/ 1 2))
true

assumedly, the reasoning is that the representation of floating point can lose precision, and should not be treated as equivalent to integral or exact representations.

like image 92
noisesmith Avatar answered Sep 22 '22 00:09

noisesmith