Skip to content

Commit 0d7eac8

Browse files
committed
Remove implicit upper+lower bound from QCheck+QCheck2 float generator
1 parent 0f48fc2 commit 0d7eac8

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/core/QCheck.ml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,16 @@ module Gen = struct
165165

166166
let bool st = RS.bool st
167167

168-
let float st =
169-
exp (RS.float st 15. *. (if RS.float st 1. < 0.5 then 1. else -1.))
170-
*. (if RS.float st 1. < 0.5 then 1. else -1.)
168+
let float st = (* switch to [bits64] once lower bound reaches 4.14 *)
169+
(* Technically we could write [15] but this is clearer *)
170+
let four_bits_mask = 0b1111 in
171+
(* Top 4 bits *)
172+
let left = Int64.(shift_left (of_int (RS.bits st land four_bits_mask)) 60) in
173+
(* Middle 30 bits *)
174+
let middle = Int64.(shift_left (of_int (RS.bits st)) 30) in
175+
(* Bottom 30 bits *)
176+
let right = Int64.of_int (RS.bits st) in
177+
Int64.(float_of_bits (logor left (logor middle right)))
171178

172179
let pfloat st = abs_float (float st)
173180
let nfloat st = -.(pfloat st)

src/core/QCheck2.ml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,16 @@ module Gen = struct
397397
then Tree.Tree (true, Seq.return false_gen)
398398
else false_gen
399399

400-
let float : float t = fun st ->
401-
let x = exp (RS.float st 15. *. (if RS.bool st then 1. else -1.))
402-
*. (if RS.bool st then 1. else -1.)
403-
in
400+
let float : float t = fun st -> (* switch to [bits64] once lower bound reaches 4.14 *)
401+
(* Technically we could write [15] but this is clearer *)
402+
let four_bits_mask = 0b1111 in
403+
(* Top 4 bits *)
404+
let left = Int64.(shift_left (of_int (RS.bits st land four_bits_mask)) 60) in
405+
(* Middle 30 bits *)
406+
let middle = Int64.(shift_left (of_int (RS.bits st)) 30) in
407+
(* Bottom 30 bits *)
408+
let right = Int64.of_int (RS.bits st) in
409+
let x = Int64.(float_of_bits (logor left (logor middle right))) in
404410
let shrink a = fun () -> Shrink.float_towards 0. a () in
405411
Tree.make_primitive shrink x
406412

0 commit comments

Comments
 (0)