Floats

The limits of floating-point values can be found in the math package. The constant math.MaxFloat32 , the largest float32 , is about 3.4e38 , and math.MaxFloat64 is about 1.8e308 . The smallest positive values are near 1.4e-45 and 4.9e-324 , respectively.

A float32 provides approximately six decimal digits of precision, where as a float64 provides about 15 digits; float64 should be preferred for most purposes because float32 computations accumulate error rapidly unless one is quite careful, and the smallest positive integer that cannot be exactly represented as a float32 is not large:

var f float32 = 16777216 // 1 << 24
fmt.Println(f+1)         // 1.6777216e+07
fmt.Println(f == f+1)    // "true"!

var f float64 = 16777216 // 1 << 24
fmt.Println(f+1)         // 1.6777217e+07
fmt.Println(f == f+1)    // "false"!

Writting literal floats

const e = 2.71828 // (approximately) Digits may be omitted before the decimal point ( .707) or after it ( 1.)

Write very small or very large numbers

const Avogadro = 6.02214129e23
const Planck = 6.62606957e-34

results matching ""

    No results matching ""