Floating-Point Numbers in GoLang

Go supports the IEEE-754 32-bit and 64-bit floating-point numbers extensively. Let’s see how to use it.

package main

import (
	"fmt"
)

func main() {
	var f float32 
	var f2 float64 
	f = 12.34567890123456
	f2 = 12.34567890123456
	fmt.Println(f, f2)  // prints "12.345679 12.34567890123456"
}

Loss of Precision with Floating Point Numbers

Loss of precision will occur when a 64-bit floating-point number is converted to 32-bit float.

package main

import (
	"fmt"
)

func main() {
	var f1 float32
	var f2 float64
	
	f2 = 1.234567890123
	f1 = float32(f2) 
	
	fmt.Println(f1)	        // prints "1.2345679"
}

Complex numbers

Floating-point numbers are used in complex numbers as well. The real and imaginary parts are floats. Complex numbers will be discussed thoroughly in another article.