Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Built-in Types

Go float32 and float64 types

Reference for Go floating-point types: float32 versus float64, precision limits, NaN and infinity, comparison with tolerance, formatting and rounding.

Go has two floating-point types that follow the IEEE 754 standard: float32 (about 7 decimal digits of precision) and float64 (about 15 to 16 digits). A literal such as 3.14 that has no other context becomes a float64, and float64 is what the math package uses.

Floats cannot represent most decimal fractions exactly, so 0.1 + 0.2 is not exactly 0.3. They also include special values: positive and negative infinity, and NaN ("not a number").

Go
x := 3.14        // float64
var y float32 = 2.5
z := 1e-9

Try it

Go Precision and special values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Formatting and conversion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Type Size Precision Largest finite value
float32 32 bits about 7 significant digits about 3.4 x 1038 (math.MaxFloat32)
float64 64 bits about 15-16 significant digits about 1.8 x 10308 (math.MaxFloat64)

Zero value: 0.

Literals: 3.5, .5, 1e6, 0x1p-2 (hexadecimal), 1_000.5 (separators). A float literal with no fractional part such as 2. is still a float.

Errors / edge cases:

  • Division of a float by zero produces +Inf, -Inf or NaN; it does not panic (division of constants by zero is a compile error).
  • NaN != NaN: it is not equal to anything, itself included, so use math.IsNaN.
  • Converting an out-of-range or NaN float to an integer gives an implementation-defined result.
  • -0.0 exists; math.Signbit can tell it from 0.

Usage notes

  • Default to float64. It has far better precision, and float32 is only worth it for big arrays, graphics or interop with APIs that require it.
  • Never compare with == after calculations. Use a tolerance: math.Abs(a-b) < 1e-9, scaled to your values.
  • Money is not a float. Use integers of the smallest unit (cents) or a decimal library, and math/big when exactness is required.
  • No implicit conversion. float64 * int is a compile error; convert one side. Constants adapt: 2 * x works.
  • Rounding: see math.Floor(), Ceil(), Round(); format with %.2f for display (it rounds half to even on the exact binary value).
  • Printing: Println uses the shortest representation that round-trips (%v is %g). %f always uses a decimal point; %e scientific notation.
  • Parsing and formatting: strconv.ParseFloat() and strconv.FormatFloat().
  • Complex numbers are built from two floats: see complex().
  • JSON cannot represent NaN or Inf; json.Marshal returns an error for them.