Skip to content

gen_range(f64::MIN..f64::MAX) should panic instead of silently overflow #1090

@nlopes

Description

@nlopes

A change request is considered a (small) Request-For-Comment, and requires a concrete proposal and motivation.

Summary

Although we can find a mention in the book of how to use f64 it's not clear that doing rand::thread_rng().gen_range(f64::MIN..f64::MAX); will not give the results intended.

Details

I think potentially guarding the range when doing gen_range on floats is a good idea. The above will not complain but won't (assume a normal distribution) give floats in the range specified (they will all be < 0).

Motivation

Make it easier for newcomers to the rand library to not shoot themselves in the foot. Be explicit programmatically in what a function accepts is better than leaving it to documentation (explicit is better than implicit).

Alternatives

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