Last active
August 29, 2015 14:06
-
-
Save AdamClements/f53cbce895e470d1935c to your computer and use it in GitHub Desktop.
Generate floats in a range for test.check generators
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 float | |
"Generates a floating point number which shrinks towards zero" | |
(fmap core/float ratio)) | |
(defn float-in-range | |
"Generates a floating point number with a given start/end | |
point (inclusive)" | |
[range-start range-end] | |
(let [gen-float-0-to-1 (sized (fn [size] | |
;; We can't have 0 as our bound, would give NaN | |
(let [bound (max size 1)] | |
(fmap (fn [x] (/ x (core/float bound))) | |
(choose 0 bound))))) | |
range-width (- range-end range-start)] | |
(fmap (fn [x] (+ range-start (* x range-width))) | |
gen-float-0-to-1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi