Skip to content

NotNaN binary operations can create NaNs #17

@ExpHP

Description

@ExpHP

NotNaN's implementation prevents binary operators from receiving a NaN operand, but this is far from the only way that a NaN may arise;

NaN, the menace that it is, is something that all numeric binary operations are capable of producing!

fn main() {
    let zero = NotNaN::new(0.).unwrap();
    let infinity = NotNaN::new(1./0.).unwrap();
    println!("{:?}", infinity + (-infinity)); // ==> NotNaN(NaN)
    println!("{:?}", infinity - infinity);    // ==> NotNaN(NaN)
    println!("{:?}", zero * infinity);        // ==> NotNaN(NaN)
    println!("{:?}", zero / zero);            // ==> NotNaN(NaN)
    println!("{:?}", zero % zero);            // ==> NotNaN(NaN)
}

Notice that due to unsafe optimizations in NotNaN, this enables undefined behavior in safe code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions