Chapter 18: Float

Float

Floating-point numbers are the ones that can represent decimal / fractional values (3.14, -0.001, 1.7976931348623157e+308, etc.).

In Go, floating-point support is very simple and clean compared to many other languages: only two types exist, and one of them is overwhelmingly preferred.

Let’s go through everything step by step like we’re sitting together with VS Code open and a notebook.

1. The Two Floating-Point Types in Go

Type Bits Approx. decimal digits of precision Approx. range (positive values) Zero value Recommendation / Reality in 2026
float32 32 ~6–7 digits ±1.18×10⁻³⁸ to ±3.40×10³⁸ 0.0 Use only when memory is extremely tight
float64 64 ~15–16 digits ±2.23×10⁻³⁰⁸ to ±1.80×10³⁰⁸ 0.0 Default and almost always the correct choice

Key takeaway most people learn the hard way:

In modern Go code (2024–2026), you should almost never use float32 unless you have a very specific reason (GPU memory, legacy file formats, very large arrays of floats where every 4 bytes matters).

The default floating-point literal type is float64.

2. Declaration & Literal Examples

Go

Typical output:

text

3. Literal Styles You’ll See

Style Example literal Result type Notes
Decimal 3.14 float64 Most common
With exponent (scientific) 1.23e-4 or 9.8E+2 float64 e or E both ok
Hexadecimal float (Go 1.13+) 0x1.8p1 float64 Rare — used in low-level math / tests
No decimal point 42. or .5 float64 Both valid

4. Important Characteristics & Gotchas (Must Know)

a) Precision is not infinite

Go

→ Classic floating-point imprecision (IEEE 754 standard). → Never compare floats with == for equality unless you really understand what you’re doing.

b) Safe comparison pattern (use small epsilon)

Go

c) No automatic promotion from float32 → float64 in most cases

Go

5. Real-World Use Cases (2026 style)

Use case Recommended type Why
Scientific calculations float64 Needs high precision
Money / finance none — use decimal package float64 can give rounding surprises
Machine learning weights/biases float32 or float64 float32 often used to save GPU memory
Game physics / 3D coordinates float32 or float64 Depends on engine (Unity → float32, many Go engines → float64)
JSON / config values float64 json.Unmarshal defaults to float64
Very large arrays of floats float32 50% memory saving
GPS coordinates, distances float64 Needs enough precision

6. Quick Practice Exercise (Try Right Now)

Create float_practice.go:

Go

Run it → observe the tiny difference between float32 and float64.

Any part still confusing?

  • Floating point precision & rounding deeper?
  • math package helpers (math.Abs, math.Round, etc.)?
  • float32 vs float64 in JSON marshaling/unmarshaling?
  • Or next: string type in detail? or composite types (slice/struct/map)?

You’re progressing beautifully — keep running these small examples, they build intuition very fast. 💻🇮🇳 Let’s keep going! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *