Skip to content

Range is odd when used with floating point types #17010

Closed
@Veedrac

Description

@Veedrac

For one,

fn main() {
    // The .take is needed to make this terminate
    let mut bad_range = range(0f32, 1e8).take(1000_000_000);

    let (min, max) = bad_range.size_hint();
    let max = max.unwrap();

    println!(
        "min: {:e} ≤ length: {:e} ≤ max: {:e}",
        min               .to_f64().unwrap(),
        bad_range.count() .to_f64().unwrap(),
        max               .to_f64().unwrap()
    );
}

outputs min: 1e8 ≤ length: 1e9 ≤ max: 1e8.

There's no real decision about what to do when rounding errors means r.next() == r.next(), such as with:

fn main() {
    let mut bad_range = range(1e8f32, 1e8+8.0);
    println!("{}", bad_range.next() == bad_range.next());
}

which currently results in an infinite iterator.

Personally I don't see a big problem with it, but unless floating ranges are banned it should at least avoid breaking size_hint.

Edit:

Further, rounding currently means that the size hints can be wrong. Both the upper bound:

fn main() {
    println!("{}", range(0f32, 1.5f32).count()); // 2
    println!("{}", range(0f32, 1.5f32).size_hint()); // (1, Some(1))
}

and the lower bound:

fn main() {
    println!("{}", range(4194303.8f32, 4194305f32).count()); // 1
    println!("{}", range(4194303.8f32, 4194305f32).size_hint()); // (2, Some(2))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions