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").
x := 3.14 // float64 var y float32 = 2.5 z := 1e-9
Try it
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.
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,-InforNaN; it does not panic (division of constants by zero is a compile error). NaN != NaN: it is not equal to anything, itself included, so usemath.IsNaN.- Converting an out-of-range or NaN float to an integer gives an implementation-defined result.
-0.0exists;math.Signbitcan tell it from0.
Usage notes
- Default to
float64. It has far better precision, andfloat32is 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/bigwhen exactness is required. - No implicit conversion.
float64 * intis a compile error; convert one side. Constants adapt:2 * xworks. - Rounding: see
math.Floor(),Ceil(),Round(); format with%.2ffor display (it rounds half to even on the exact binary value). - Printing:
Printlnuses the shortest representation that round-trips (%vis%g).%falways uses a decimal point;%escientific notation. - Parsing and formatting:
strconv.ParseFloat()andstrconv.FormatFloat(). - Complex numbers are built from two floats: see
complex(). - JSON cannot represent NaN or Inf;
json.Marshalreturns an error for them.
Related
math,math.Sqrt()andPow(),int,complex(), Type conversions- Lessons: Numbers, Basic types