wired value

45 views
Skip to first unread message

belrhalm...@gmail.com

unread,
Jul 28, 2016, 11:48:55 PM7/28/16
to golang-nuts
Hello everybody, can someone explain me this weird value :

this return me this value: -9223372036854775808

fmt.Println(int(math.Pow(float64(9200), float64(5))))


Normally 9200^5 musnt give this value even if i put a value bigger then 9200 i receive the same value please can someone explain me why in details please?











Chris Manghane

unread,
Jul 29, 2016, 12:52:46 AM7/29/16
to belrhalm...@gmail.com, golang-nuts
A simple example: https://play.golang.org/p/RNgW-Ya5BS

"For the conversion of non-constant numeric values, the following rules apply:
  1. When converting between integer types, if the value is a signed integer, it is sign extended to implicit infinite precision; otherwise it is zero extended. It is then truncated to fit in the result type's size.
    For example, if v := uint16(0x10F0), then uint32(int8(v)) == 0xFFFFFFF0. The conversion always yields a valid value; there is no indication of overflow.
  2. When converting a floating-point number to an integer, the fraction is discarded (truncation towards zero)."

The float result of math.Pow is larger than the largest signed integer on your machine. Converting it to a signed integer loses precision and gets turned into the smallest signed integer on your machine. This is why using larger bases for math.Pow render the same result. 

--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages