Numeric Constants

Numeric constants are high-precision values.

An untyped constant takes the type needed by its context.

Try printing needInt(Big) too.

(An int can store at maximum a 64-bit integer, and sometimes less.)

numeric-constants.go

  1. package main
  2. import "fmt"
  3. const (
  4. // Create a huge number by shifting a 1 bit left 100 places.
  5. // In other words, the binary number that is 1 followed by 100 zeroes.
  6. Big = 1 << 100
  7. // Shift it right again 99 places, so we end up with 1<<1, or 2.
  8. Small = Big >> 99
  9. )
  10. func needInt(x int) int { return x*10 + 1 }
  11. func needFloat(x float64) float64 {
  12. return x * 0.1
  13. }
  14. func main() {
  15. fmt.Println(needInt(Small))
  16. fmt.Println(needFloat(Small))
  17. fmt.Println(needFloat(Big))
  18. }