float32 in Go



In this tutorial, we will learn about the float32 data type in Go. We will cover the basics of defining and using float32 values, including how to perform arithmetic operations and format floating-point numbers.


Understanding the Float32 Data Type in Go

The float32 data type in Go is used to represent 32-bit floating-point numbers. It is suitable for applications where memory usage is a concern, but it provides less precision than float64.


Defining a Float32 Variable

Float32 variables in Go can be defined using the var keyword or by type inference.

var f1 float32 = 3.14
f2 := float32(2.718)

Performing Arithmetic Operations

Arithmetic operations such as addition, subtraction, multiplication, and division can be performed on float32 values.

sum := f1 + f2
difference := f1 - f2
product := f1 * f2
quotient := f1 / f2

Formatting Float32 Values

Float32 values can be formatted for output using the fmt package.

fmt.Printf("%.2f", f1)


Example 1: Defining and using Float32 Variables

We can define and use float32 variables in Go to represent 32-bit floating-point values.

For example,

  1. Define a float32 variable named f1 and assign it a value.
  2. Define another float32 variable named f2 using type inference and assign it a value.
  3. Print the values of both float32 variables to the console.

Go Program

package main
import "fmt"
func main() {
    var f1 float32 = 3.14
    f2 := float32(2.718)
    fmt.Println(f1)
    fmt.Println(f2)
}

Output

3.14
2.718


Example 2: Performing Arithmetic Operations

We can perform arithmetic operations on float32 values in Go.

For example,

  1. Define two float32 variables named f1 and f2.
  2. Perform addition, subtraction, multiplication, and division on these float32 values.
  3. Print the results of these operations to the console.

Go Program

package main
import "fmt"
func main() {
    f1 := float32(3.14)
    f2 := float32(2.718)
    sum := f1 + f2
    difference := f1 - f2
    product := f1 * f2
    quotient := f1 / f2
    fmt.Println("Sum:", sum)
    fmt.Println("Difference:", difference)
    fmt.Println("Product:", product)
    fmt.Println("Quotient:", quotient)
}

Output

Sum: 5.858
Difference: 0.422
Product: 8.53452
Quotient: 1.1553134


Example 3: Formatting Float32 Values

We can format float32 values for output in Go using the fmt package.

For example,

  1. Define a float32 variable named f and assign it a value.
  2. Use the Printf function from the fmt package to format the float32 value with two decimal places.
  3. Print the formatted value to the console.

Go Program

package main
import "fmt"
func main() {
    f := float32(3.14159)
    fmt.Printf("%.2f\n", f)
}

Output

3.14


Example 4: Comparing Float32 Values

We can compare float32 values in Go using relational operators.

For example,

  1. Define two float32 variables named f1 and f2 with different values.
  2. Use relational operators to compare the float32 values and print the results to the console.

Go Program

package main
import "fmt"
func main() {
    f1 := float32(3.14)
    f2 := float32(2.718)
    fmt.Println(f1 > f2)  // true
    fmt.Println(f1 < f2)  // false
    fmt.Println(f1 == f2) // false
}

Output

true
false
false