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,
- Define a float32 variable named
f1
and assign it a value. - Define another float32 variable named
f2
using type inference and assign it a value. - 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,
- Define two float32 variables named
f1
andf2
. - Perform addition, subtraction, multiplication, and division on these float32 values.
- 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,
- Define a float32 variable named
f
and assign it a value. - Use the
Printf
function from thefmt
package to format the float32 value with two decimal places. - 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,
- Define two float32 variables named
f1
andf2
with different values. - 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