FAQ
While very much an edge case, it appears the compiler treats ddd and ddd.0
differently for sufficiently large values of ddd. In particular, without
the .0 the compiler will issue and error on a constant that is 2^464 or
larger, even when assigning it to a float64. 2^464 can be perfectly
represented in a 64 bit float, of course. I briefly looked in the spec and
did not see where such a distinction would be made. In particular, given var
a float64:

This works:

a = 476341026354368931790404850737482651634002402140040763986077
416935023763857996463031052566995772090325901326159882602370
52123652332890095616.0

This generates a "constant addition overflow" error:

a = 476341026354368931790404850737482651634002402140040763986077
416935023763857996463031052566995772090325901326159882602370
52123652332890095616

Is this a known issue, or even considered an issue?

Thanks,

     -Paul

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Ian Lance Taylor at Oct 23, 2013 at 3:39 pm

    On Wed, Oct 23, 2013 at 8:33 AM, Paul Borman wrote:
    While very much an edge case, it appears the compiler treats ddd and ddd.0
    differently for sufficiently large values of ddd. In particular, without
    the .0 the compiler will issue and error on a constant that is 2^464 or
    larger, even when assigning it to a float64. 2^464 can be perfectly
    represented in a 64 bit float, of course. I briefly looked in the spec and
    did not see where such a distinction would be made. In particular, given
    var a float64:

    This works:

    a =
    47634102635436893179040485073748265163400240214004076398607741693502376385799646303105256699577209032590132615988260237052123652332890095616.0

    This generates a "constant addition overflow" error:

    a =
    47634102635436893179040485073748265163400240214004076398607741693502376385799646303105256699577209032590132615988260237052123652332890095616

    Is this a known issue, or even considered an issue?
    A Go compiler is permitted to limit the size of constants that it
    supports (http://golang.org/ref/spec#Constants). So there will
    inevitably be cases like this, in which very large floating point and
    integer constants are handled differently. I don't think that's a
    bug, more of an unfortunate condition.

    That said, "constant addition overflow" doesn't seem like the most
    useful possible error message. It's probably worth opening an issue
    for that (http://golang.org/issues/new).

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brendan Tracey at Oct 23, 2013 at 3:58 pm
    I'm confused though, look at
    http://play.golang.org/p/PnCmoloJgT
    or
    http://play.golang.org/p/d8xqDwNlOG

    The spec says "A constant may be given a type explicitly by a constant
    declaration <http://golang.org/ref/spec#Constant_declarations> or conversion<http://golang.org/ref/spec#Conversions>,
    or implicitly when used in a variable declaration<http://golang.org/ref/spec#Variable_declarations> or
    an assignment <http://golang.org/ref/spec#Assignments> or as an operand in
    an expression <http://golang.org/ref/spec#Expressions>. It is an error if
    the constant value cannot be represented as a value of the respective type.
    For instance, 3.0 can be given any integer or any floating-point type,
    while 2147483648.0 (equal to1<<31) can be given the types float32, float64,
    or uint32 but not int32 or string."

    Don't my codes appropriately give the constant a floating point type?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Michael Jones at Oct 23, 2013 at 8:18 pm
    Two years ago I asked to have the one constant that controls this changed
    (~doubled) so that every IEEE double precision floating point number would
    be constructable, and so that IEEE constants could be specified in the
    clear (to me at least) way of:

    var dpmax float64 = (1 + (1 - 2.0 >> 52)) * (2 <<1023) //
    1.7976931348623157e308


    rather than

    var dpmax float64 = 1.7976931348623157e308 // (1 + (1 - 2.0 >> 52)) * (2
    <<1023)


    because in the second case you never know what the decimal-to-binary
    conversion may do to the least significant bits. It was not a persuasive
    argument, being seen more as pedantry than wisdom.

    Michael

    P.S. Of course, since Go is provided as source, one can always have this
    upgrade...
    On Wed, Oct 23, 2013 at 11:58 AM, Brendan Tracey wrote:

    I'm confused though, look at
    http://play.golang.org/p/PnCmoloJgT
    or
    http://play.golang.org/p/d8xqDwNlOG

    The spec says "A constant may be given a type explicitly by a constant
    declaration or conversion, or implicitly when used in a variable
    declaration or an assignment or as an operand in an expression. It is an
    error if the constant value cannot be represented as a value of the
    respective type. For instance, 3.0 can be given any integer or any
    floating-point type, while 2147483648.0 (equal to1<<31) can be given the
    types float32, float64, or uint32 but not int32 or string."
    Don't my codes appropriately give the constant a floating point type?

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.



    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ian Lance Taylor at Oct 23, 2013 at 11:13 pm

    On Wed, Oct 23, 2013 at 8:58 AM, Brendan Tracey wrote:
    I'm confused though, look at
    http://play.golang.org/p/PnCmoloJgT
    or
    http://play.golang.org/p/d8xqDwNlOG

    The spec says "A constant may be given a type explicitly by a constant
    declaration or conversion, or implicitly when used in a variable declaration
    or an assignment or as an operand in an expression. It is an error if the
    constant value cannot be represented as a value of the respective type. For
    instance, 3.0 can be given any integer or any floating-point type, while
    2147483648.0 (equal to1<<31) can be given the types float32, float64, or
    uint32 but not int32 or string."

    Don't my codes appropriately give the constant a floating point type?
    It does give it a floating point type. But the failure happens
    earlier. The compiler gets a numeric string. It turns that numeric
    string into an untyped constant. The .0 in one case means that the
    compiler turns it into an untyped floating point constant. The
    absence of the .o means that the compiler turns that one into an
    untyped integer constant. Once the compiler has an untyped constant,
    it uses the variable assignment to decide to convert the untyped
    constant to float64. Only it doesn't get that far, because it
    overflows trying to represent the untyped integer constant.

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brendan Tracey at Oct 23, 2013 at 11:19 pm
    Ah, I see. I filed an issue on this behalf ( https://code.google.com/p/go/issues/detail?id=6652 ) though based on your response, I'm still not sure I know the difference between a typed constant and an untyped constant. Isn't an integer constant a typed constant?

    On Oct 23, 2013, at 4:13 PM, Ian Lance Taylor wrote:

    On Wed, Oct 23, 2013 at 8:58 AM, Brendan Tracey
    wrote:
    I'm confused though, look at
    http://play.golang.org/p/PnCmoloJgT
    or
    http://play.golang.org/p/d8xqDwNlOG

    The spec says "A constant may be given a type explicitly by a constant
    declaration or conversion, or implicitly when used in a variable declaration
    or an assignment or as an operand in an expression. It is an error if the
    constant value cannot be represented as a value of the respective type. For
    instance, 3.0 can be given any integer or any floating-point type, while
    2147483648.0 (equal to1<<31) can be given the types float32, float64, or
    uint32 but not int32 or string."

    Don't my codes appropriately give the constant a floating point type?
    It does give it a floating point type. But the failure happens
    earlier. The compiler gets a numeric string. It turns that numeric
    string into an untyped constant. The .0 in one case means that the
    compiler turns it into an untyped floating point constant. The
    absence of the .o means that the compiler turns that one into an
    untyped integer constant. Once the compiler has an untyped constant,
    it uses the variable assignment to decide to convert the untyped
    constant to float64. Only it doesn't get that far, because it
    overflows trying to represent the untyped integer constant.

    Ian
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Ian Lance Taylor at Oct 24, 2013 at 12:03 am

    On Wed, Oct 23, 2013 at 4:19 PM, Brendan Tracey wrote:
    Ah, I see. I filed an issue on this behalf (
    https://code.google.com/p/go/issues/detail?id=6652 ) though based on your
    response, I'm still not sure I know the difference between a typed constant
    and an untyped constant. Isn't an integer constant a typed constant?
    It's tricky, I should really avoid the use of the word "type" when
    discussing untyped constants. Perhaps we could say that there are
    different "kinds" of constants. In the appropriate context the
    compiler will interpret the string "1" as an untyped constant of
    integer kind. The string "1.0" is an untyped constant of
    floating-point kind. The kind of a constant applies when it is used
    in a context that has no type (e.g., "var i = 1") and also when
    considering which set of implementation restrictions apply to the
    representation of the value.

    Ian



    On Oct 23, 2013, at 4:13 PM, Ian Lance Taylor wrote:

    On Wed, Oct 23, 2013 at 8:58 AM, Brendan Tracey
    wrote:

    I'm confused though, look at
    http://play.golang.org/p/PnCmoloJgT
    or
    http://play.golang.org/p/d8xqDwNlOG

    The spec says "A constant may be given a type explicitly by a constant
    declaration or conversion, or implicitly when used in a variable declaration
    or an assignment or as an operand in an expression. It is an error if the
    constant value cannot be represented as a value of the respective type. For
    instance, 3.0 can be given any integer or any floating-point type, while
    2147483648.0 (equal to1<<31) can be given the types float32, float64, or
    uint32 but not int32 or string."

    Don't my codes appropriately give the constant a floating point type?


    It does give it a floating point type. But the failure happens
    earlier. The compiler gets a numeric string. It turns that numeric
    string into an untyped constant. The .0 in one case means that the
    compiler turns it into an untyped floating point constant. The
    absence of the .o means that the compiler turns that one into an
    untyped integer constant. Once the compiler has an untyped constant,
    it uses the variable assignment to decide to convert the untyped
    constant to float64. Only it doesn't get that far, because it
    overflows trying to represent the untyped integer constant.

    Ian
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Michael Jones at Oct 24, 2013 at 12:13 am
    BTW, if you increase the value of the constant "Mpprec = 16" at line 87
    of go/src/cmd/gc/go.h, then you'll not have these problems.

    On Wed, Oct 23, 2013 at 8:03 PM, Ian Lance Taylor wrote:

    On Wed, Oct 23, 2013 at 4:19 PM, Brendan Tracey
    wrote:
    Ah, I see. I filed an issue on this behalf (
    https://code.google.com/p/go/issues/detail?id=6652 ) though based on your
    response, I'm still not sure I know the difference between a typed constant
    and an untyped constant. Isn't an integer constant a typed constant?
    It's tricky, I should really avoid the use of the word "type" when
    discussing untyped constants. Perhaps we could say that there are
    different "kinds" of constants. In the appropriate context the
    compiler will interpret the string "1" as an untyped constant of
    integer kind. The string "1.0" is an untyped constant of
    floating-point kind. The kind of a constant applies when it is used
    in a context that has no type (e.g., "var i = 1") and also when
    considering which set of implementation restrictions apply to the
    representation of the value.

    Ian



    On Oct 23, 2013, at 4:13 PM, Ian Lance Taylor wrote:

    On Wed, Oct 23, 2013 at 8:58 AM, Brendan Tracey
    wrote:

    I'm confused though, look at
    http://play.golang.org/p/PnCmoloJgT
    or
    http://play.golang.org/p/d8xqDwNlOG

    The spec says "A constant may be given a type explicitly by a constant
    declaration or conversion, or implicitly when used in a variable
    declaration
    or an assignment or as an operand in an expression. It is an error if the
    constant value cannot be represented as a value of the respective type. For
    instance, 3.0 can be given any integer or any floating-point type, while
    2147483648.0 (equal to1<<31) can be given the types float32, float64, or
    uint32 but not int32 or string."

    Don't my codes appropriately give the constant a floating point type?


    It does give it a floating point type. But the failure happens
    earlier. The compiler gets a numeric string. It turns that numeric
    string into an untyped constant. The .0 in one case means that the
    compiler turns it into an untyped floating point constant. The
    absence of the .o means that the compiler turns that one into an
    untyped integer constant. Once the compiler has an untyped constant,
    it uses the variable assignment to decide to convert the untyped
    constant to float64. Only it doesn't get that far, because it
    overflows trying to represent the untyped integer constant.

    Ian
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Michael Jones at Oct 24, 2013 at 12:14 am
    ...because both "kinds" will be able to support the full implied binary
    size of 64-bit floating point.

    On Wed, Oct 23, 2013 at 8:12 PM, Michael Jones wrote:

    BTW, if you increase the value of the constant "Mpprec = 16" at line 87
    of go/src/cmd/gc/go.h, then you'll not have these problems.

    On Wed, Oct 23, 2013 at 8:03 PM, Ian Lance Taylor wrote:

    On Wed, Oct 23, 2013 at 4:19 PM, Brendan Tracey
    wrote:
    Ah, I see. I filed an issue on this behalf (
    https://code.google.com/p/go/issues/detail?id=6652 ) though based on your
    response, I'm still not sure I know the difference between a typed constant
    and an untyped constant. Isn't an integer constant a typed constant?
    It's tricky, I should really avoid the use of the word "type" when
    discussing untyped constants. Perhaps we could say that there are
    different "kinds" of constants. In the appropriate context the
    compiler will interpret the string "1" as an untyped constant of
    integer kind. The string "1.0" is an untyped constant of
    floating-point kind. The kind of a constant applies when it is used
    in a context that has no type (e.g., "var i = 1") and also when
    considering which set of implementation restrictions apply to the
    representation of the value.

    Ian



    On Oct 23, 2013, at 4:13 PM, Ian Lance Taylor wrote:

    On Wed, Oct 23, 2013 at 8:58 AM, Brendan Tracey
    wrote:

    I'm confused though, look at
    http://play.golang.org/p/PnCmoloJgT
    or
    http://play.golang.org/p/d8xqDwNlOG

    The spec says "A constant may be given a type explicitly by a constant
    declaration or conversion, or implicitly when used in a variable
    declaration
    or an assignment or as an operand in an expression. It is an error if the
    constant value cannot be represented as a value of the respective type. For
    instance, 3.0 can be given any integer or any floating-point type, while
    2147483648.0 (equal to1<<31) can be given the types float32, float64, or
    uint32 but not int32 or string."

    Don't my codes appropriately give the constant a floating point type?


    It does give it a floating point type. But the failure happens
    earlier. The compiler gets a numeric string. It turns that numeric
    string into an untyped constant. The .0 in one case means that the
    compiler turns it into an untyped floating point constant. The
    absence of the .o means that the compiler turns that one into an
    untyped integer constant. Once the compiler has an untyped constant,
    it uses the variable assignment to decide to convert the untyped
    constant to float64. Only it doesn't get that far, because it
    overflows trying to represent the untyped integer constant.

    Ian
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765


    --
    Michael T. Jones | Chief Technology Advocate | mtj@google.com | +1
    650-335-5765

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Benjamin Measures at Oct 23, 2013 at 9:59 pm
    it appears the compiler treats ddd and ddd.0 differently
    They are different- constants are typed. The first is an integer constant, and the second a floating point constant.

    http://golang.org/ref/spec#Constants
    In particular, without the .0 the compiler will issue and error on a constant that is 2^464 or larger
    Integer constants are only required to be represented by at least 256 bits.

    As for this implementation, what made you choose 2^464?
    https://code.google.com/p/go/issues/detail?id=2053

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brendan Tracey at Oct 23, 2013 at 10:34 pm

    On Wednesday, October 23, 2013 2:59:04 PM UTC-7, Benjamin Measures wrote:
    it appears the compiler treats ddd and ddd.0 differently
    They are different- constants are typed. The first is an integer constant,
    and the second a floating point constant.

    http://golang.org/ref/spec#Constants
    Constants can be typed, but it's confusing because the spec says
    "A constant may be given a type explicitly by a constant declaration<http://golang.org/ref/spec#Constant_declarations>
      or conversion <http://golang.org/ref/spec#Conversions>, or implicitly when
    used in a variable declaration<http://golang.org/ref/spec#Variable_declarations> or
    an assignment <http://golang.org/ref/spec#Assignments> or as an operand in
    an expression <http://golang.org/ref/spec#Expressions>. "

    I would have guessed that the type that it is given is decided by the type
    of the variable to which it is being assigned, but apparently that is not
    the case.



    >

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 23, '13 at 3:33p
activeOct 24, '13 at 12:14a
posts11
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase