(ns same.klipse (:require-macros [same :refer [with-comparator]]) (:require [same :refer [ish? zeroish? not-zeroish? set-comparator!]] [same.compare :refer [compare-ulp]] [same.ish :refer [default-comparator]]))

Click here to make these examples interactive with ClojureScript.

same.core

Main public API namespace.

ish?

(ish? expected & actuals)

Compare one or more values to an expected value, returning true if they are the same-ish. The values can be numbers:

(let [two (Math/pow (Math/sqrt 2) 2)]
  [(== 2 two) (ish? 2 two)])

or data structures:

(ish? {:a 1 :b [1.99999999999999 3]}
      {:a 1.00000000000001 :b [2 3.0]})

you can also compare more than one value to the expected value:

(ish? 1 1.0 0.99999999999999 1.00000000000001 1)

not-zeroish?

(not-zeroish? val & {:keys [scale], :or {scale 1000.0}})

Compare a numeric value to zero, returning true if not close. Equivalent to (not (zeroish? ...)).

(not-zeroish? 3 :scale 1e6)

set-comparator!

(set-comparator! comparator)

Set the default comparator.

(set-comparator! (compare-ulp 2.0 100))
(ish? 0.1 (-> 2 Math/sqrt (Math/pow 2) (- 1.9)))

with-comparator

macro

(with-comparator comparator & body)

Temporarily replace the default comparator.

(with-comparator (compare-ulp 100.0 1e9)
  (ish? 1.0 0.9999999))
(with-comparator ==
  (ish? 1.0 0.9999999999999))

zeroish?

(zeroish? val & {:keys [scale], :or {scale 1000.0}})

Compare a numeric value to zero, returning true if close.

(zeroish? 0.0000000001
          :scale 1e6)