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

spec: clarify imprecise conversions of non-floating-point constant to floating point type #13782

Open
momchil-velikov opened this issue Dec 30, 2015 · 2 comments

Comments

@momchil-velikov
Copy link
Contributor

... or perhaps the spec is does not convey the intention.

The spec says (https://golang.org/ref/spec#Conversions)

A constant value x can be converted to type T in any of these cases:

  • x is representable by a value of type T.

The Go compiler allows conversion in certain cases, where an integer constant value is not representable as a value of type float32 as in:

package main

import "fmt"

func main() {
    // bad
    const A = 0x01ffffff
    const B = float32(A)
    fmt.Printf("%d\n%f\n", A, B)

    // ok
    const C = 0x01fffffe
    const D = float32(C)
    fmt.Printf("%d\n%f\n", C, D)

    // ok
    const E = 0x00ffffff
    const F = float32(E)
    fmt.Printf("%d\n%f\n", E, F)
}

I suppose there are similar cases when converting an integer constant into float64, complex64, or complex128 type.

@ianlancetaylor ianlancetaylor changed the title cmd/compile: Incorrect constant conversion spec: does not permit imprecise conversion of non-floating-point constant to floating point type Dec 31, 2015
@ianlancetaylor
Copy link
Contributor

Personally I think the spec should be modified so that the next line permits integer constants as well as floating-point constants to be rounded.

@griesemer
Copy link
Contributor

Agreeing with @ianlancetaylor. This is a grey area that should be spec-ed out more precisely. (Some) rounding is acceptable when converting to floats.

@griesemer griesemer changed the title spec: does not permit imprecise conversion of non-floating-point constant to floating point type spec: clarify imprecise conversions of non-floating-point constant to floating point type Jan 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants