Go Tutorials

Go Programs

Go float64


Go float64

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


Understanding the Float64 Data Type in Go

The float64 data type in Go is used to represent 64-bit floating-point numbers. It provides higher precision compared to float32 and is commonly used for scientific calculations and other applications requiring high precision.


Defining a Float64 Variable

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

var f1 float64 = 3.14159265359
f2 := 2.71828182846

Performing Arithmetic Operations

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

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

Formatting Float64 Values

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

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


Defining and using Float64 Variables

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

For example,

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

Go Program

package main
import "fmt"
func main() {
    var f1 float64 = 3.14159265359
    f2 := 2.71828182846
    fmt.Println(f1)
    fmt.Println(f2)
}

Output

3.14159265359
2.71828182846


Performing Arithmetic Operations

We can perform arithmetic operations on float64 values in Go.

For example,

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

Go Program

package main
import "fmt"
func main() {
    f1 := 3.14159265359
    f2 := 2.71828182846
    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.85987448205
Difference: 0.42331082513
Product: 8.53973422267
Quotient: 1.15572734979


Formatting Float64 Values

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

For example,

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

Go Program

package main
import "fmt"
func main() {
    f := 3.14159265359
    fmt.Printf("%.10f\n", f)
}

Output

3.1415926536


Comparing Float64 Values

We can compare float64 values in Go using relational operators.

For example,

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

Go Program

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

Output

true
false
false