Skip to content

Instantly share code, notes, and snippets.

@lnostdal
Last active July 19, 2018 14:05
Show Gist options
  • Select an option

  • Save lnostdal/6b7f362ad469305edd614efd172d4934 to your computer and use it in GitHub Desktop.

Select an option

Save lnostdal/6b7f362ad469305edd614efd172d4934 to your computer and use it in GitHub Desktop.
fuzzy comparing floating point numbers
;; This turns out to be very, very useful when working with floating point numbers in some cases.
;; E.g. if you switch some of your input from ^long to ^double your calculations might actually shift ever so slightly
;; which will make comparing old vs. new results tricky.
;; You'll need this in your project.clj file:
[com.google.guava/guava "LATEST"]
;; ..and this somewhere useful:
(import 'com.google.common.math.DoubleMath)
;; Now instead of comparing using =, you can do something like:
quantataraxia.core> (let [md (oneb-get-ohlc-with-spreads "EURUSD" (tp-seconds (time/hours 1)))]
(dorun (map #(try (assert (DoubleMath/fuzzyEquals %1 %2 0.000000000000001))
(catch Throwable e
(println [%1 %2])
#_(throw e)))
(old-wma (double-array (map pclose md)) 15)
(wma (double-array (map pclose md)) 15))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment