-
-
Notifications
You must be signed in to change notification settings - Fork 456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tweak Open01 and Closed01 #237
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -269,9 +269,38 @@ mod tests { | |
|
||
#[test] | ||
fn floating_point_edge_cases() { | ||
// the test for exact equality is correct here. | ||
assert!(ConstantRng(0xffff_ffff).gen::<f32>() != 1.0); | ||
assert!(ConstantRng(0xffff_ffff_ffff_ffff).gen::<f64>() != 1.0); | ||
const EPSILON32: f32 = 1.0 / (1u32 << 23) as f32; | ||
const EPSILON64: f64 = 1.0 / (1u64 << 52) as f64; | ||
|
||
let mut zeros = ConstantRng(0); | ||
let mut ones = ConstantRng(!0); | ||
|
||
let zero32 = zeros.gen::<f32>(); | ||
let zero64 = zeros.gen::<f64>(); | ||
let one32 = ones.gen::<f32>(); | ||
let one64 = ones.gen::<f64>(); | ||
assert_eq!(zero32, 0.0); | ||
assert_eq!(zero64, 0.0); | ||
assert!(1.0 - EPSILON32 <= one32 && one32 < 1.0); | ||
assert!(1.0 - EPSILON64 <= one64 && one64 < 1.0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As noted above, I believe this should be exactly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, |
||
|
||
let Closed01(closed_zero32) = zeros.gen::<Closed01<f32>>(); | ||
let Closed01(closed_zero64) = zeros.gen::<Closed01<f64>>(); | ||
let Closed01(closed_one32) = ones.gen::<Closed01<f32>>(); | ||
let Closed01(closed_one64) = ones.gen::<Closed01<f64>>(); | ||
assert_eq!(closed_zero32, 0.0); | ||
assert_eq!(closed_zero64, 0.0); | ||
assert_eq!(closed_one32, 1.0); | ||
assert_eq!(closed_one64, 1.0); | ||
|
||
let Open01(open_zero32) = zeros.gen::<Open01<f32>>(); | ||
let Open01(open_zero64) = zeros.gen::<Open01<f64>>(); | ||
let Open01(open_one32) = ones.gen::<Open01<f32>>(); | ||
let Open01(open_one64) = ones.gen::<Open01<f64>>(); | ||
assert!(0.0 < open_zero32 && open_zero32 <= EPSILON32); | ||
assert!(0.0 < open_zero64 && open_zero64 <= EPSILON64); | ||
assert!(1.0 - EPSILON32 <= open_one32 && open_one32 < 1.0); | ||
assert!(1.0 - EPSILON64 <= open_one64 && open_one64 < 1.0); | ||
} | ||
|
||
#[test] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use
core::f32::EPSILON
?ε is defined as the difference between 1.0 and the next largest representable number; since all numbers output (excepting
Closed01
) are strictly less than 1.0 the exponent will be 1 less than for 1.0, hence the largest representable number less than 1 should be1 - ε / 2
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I first used
std::f32::EPSILON
but that failed the continuous integration tests and I forgot aboutcore
, so I'll change this.