Last active
July 19, 2018 14:05
-
-
Save lnostdal/6b7f362ad469305edd614efd172d4934 to your computer and use it in GitHub Desktop.
fuzzy comparing floating point numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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