proptest-rs / proptest

Hypothesis-like property testing for Rust
Apache License 2.0
1.69k stars 158 forks source link

Invalid range for float inclusive ranges a..=a #343

Open Andlon opened 1 year ago

Andlon commented 1 year ago

With proptest 1.2.0, the following application

use proptest::prelude::*;

proptest! {
    #[test]
    fn inclusive_floats(x in 0.0..=0.0) {}
}

panics with

thread 'inclusive_floats' panicked at 'invalid range', /home/andreas/.cargo/registry/src/index.crates.io-6f17d22bba15001f/proptest-1.2.0/src/num/float_samplers.rs:463:1

Excerpt from the source:

            fn split_interval([low, high]: [$typ; 2]) -> IntervalCollection {
                    assert!(low.is_finite(), "low finite");
                    assert!(high.is_finite(), "high finite");
                    assert!(high - low > 0., "invalid range");

                    let min_abs = $typ::min(low.abs(), high.abs());
                    let max_abs = $typ::max(low.abs(), high.abs());
                    // .....

Apparently the splitting of the intervals assumes that high > low, but this is not a valid assumption for inclusive ranges.

This example might be contrived, but scenarios like these often happen in practice. For example, if you want to generate numbers $x, y, z \geq 0$ satisfying $x + y + z \leq 0$ by generating one number at a time and sampling the other numbers from a modified range, then you'll run into this case very quickly.

matthew-russo commented 10 months ago

Thanks for the report and sorry for the delay. Triaging issues now and will get this on the backlog