Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cemerick
Created November 22, 2013 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cemerick/7599452 to your computer and use it in GitHub Desktop.
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)
(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