Skip to content

Incorrect float→int conversion #973

Closed
@dhardy

Description

@dhardy
Member

fn to_u64(self) -> Option<u64> {
if self >= 0. && self <= ::core::u64::MAX as f64 {
Some(self as u64)
} else {
None
}
}

This function is wrong; to copy my comment from #958:

This is incorrect for 32-bit platforms. Check this out.

This isn't your fault; to_u64 for f64 makes the same mistake. Hmm, even the TryFrom in Rust 1.34 doesn't handle this conversion. I'm starting to hate this topic; seems simple but isn't.

Activity

dhardy

dhardy commented on Oct 19, 2020

@dhardy
MemberAuthor

The offending code was removed in #987 (including uses of it, so there is no replacement).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @dhardy

        Issue actions

          Incorrect float→int conversion · Issue #973 · rust-random/rand