Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deterministic int to float conversion #62175

Closed
AljoschaMeyer opened this issue Jun 27, 2019 · 6 comments
Closed

Deterministic int to float conversion #62175

AljoschaMeyer opened this issue Jun 27, 2019 · 6 comments
Assignees
Labels
A-FFI Area: Foreign function interface (FFI) C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@AljoschaMeyer
Copy link
Contributor

AljoschaMeyer commented Jun 27, 2019

Casting from an integer type to a floating point type leaves the rounding behavior explicitly unspecified. Is there (or are there plans for) some way of deterministically turning an int into a float regardless of the target architecture? I really don't want 9007199254740993u64 as f64 to be converted to 9007199254740992.0 on one machine and 9007199254740994.0 on another (the example u64 is 2^53 + 1, the smallest natural number not precisely representable as an f64).

Options could be to either use the floating point rounding mode (interpreting the int as real number and then rounding it to the next representable float just like the semantics of all IEEE754 float operations), or supporting explicit rounding modes.

Apologies if such a feature already exists and I just didn't find it.

@jonas-schievink jonas-schievink added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Jun 27, 2019
@jonas-schievink
Copy link
Contributor

This might just be a bug in the reference. rustc probably just emits uitofp, which uses the default rounding mode. In fact, LLVM only supports the default rounding mode AFAIK (which is "round to nearest, ties to even").

@Centril
Copy link
Contributor

Centril commented Jun 27, 2019

Some discussion in rust-lang/reference#607.

@estebank
Copy link
Contributor

estebank commented Jun 27, 2019

Further discussion in https://internals.rust-lang.org/t/tryfrom-for-f64/9793

@nikomatsakis
Copy link
Contributor

Discussed in @rust-lang/lang meeting and @joshtriplett will try to follow up with this as well as rust-lang/reference#607.

@joshtriplett
Copy link
Member

Is this something that would naturally vary between targets? Would specifying this break any existing or anticipated targets?

If the answer to both questions is "no", then I'm fine with specifying this. If we don't currently have consistent behavior on this point across all our existing targets, then I don't think the standard should precede the implementation here.

@jturner314
Copy link
Contributor

This appears to be resolved, so this issue can be closed.

(The reference now specifies the rounding mode. See rust-lang/reference#607.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-FFI Area: Foreign function interface (FFI) C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants