Created
November 22, 2013 12:57
-
-
Save cemerick/7599452 to your computer and use it in GitHub Desktop.
re: https://github.com/reiddraper/simple-check/issues/36, an overly-clever portable double generator (for use with https://github.com/cemerick/double-check, more efficient options are available if you're only targeting the JVM and are using simple-check)
This file contains 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
(def gen-double | |
(gen/such-that | |
identity | |
(gen/fmap | |
(fn [[^long s1 s2 e]] | |
(let [neg? (neg? s1) | |
s1 (str (Math/abs s1)) | |
; this creates odd strings '1.+e5', but JDK and JS parse OK | |
numstr (str (first s1) "." (subs s1 1) | |
(when (not (zero? s2)) s2) | |
"e" e) | |
num #+clj (Double/parseDouble numstr) #+cljs (js/parseFloat numstr)] | |
; TODO use Number.isNaN once we're not using phantomjs for testing :-X | |
(when-not (or #+cljs (js/isNaN num) | |
#+clj (Double/isNaN num) | |
#+cljs (or (== js/Infinity num) (= (- js/Infinity) num)) | |
#+clj (Double/isInfinite num)) | |
num))) | |
(gen/tuple | |
; significand, broken into 2 portions, sign on the left | |
(choose -179769313 179769313) (choose 0 48623157) | |
; exponent range | |
(choose -324 308))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment