Skip to content

Inexact and/or Wrong {From,To}Primitive #10481

@lifthrasiir

Description

@lifthrasiir

Motivating examples:

(100_0000_0000_0000_2345u64).to_f64(); // => Some(1000000000000002244f64)
(42.5f64).to_u8(); // => Some(42u8)
(42.5e80f64).to_u8(); // => Some(0u8)
(42.5e80f64).to_i32(); // => Some(-2147483648i32)

Note that the last two results can change depending on your optimization level. (Is it another issue?) It seems that it's due to the constant propagation across inlined function calls.

Currently we check for such cases in int to int and float to float conversion, but we don't check (and solely rely on casting) in int to float and float to int conversion. This can cause the inexact computation in two ways: unwanted truncation, and failed bound check. Four possible decisions have their pros and cons respectively, but should be decided and documented.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions