Go Programming Interview Questions - Floating Point Types in Go

Опубликовано: 23 Август 2026
на канале: Reza Mirjahan
43
2

Go Programming Interview Questions - Go provides two floating point types: float32 and float64. In this video, you will learn the difference between them, how they are stored in memory using the IEEE 754 standard, and why float64 is the default type when you write decimal numbers like 3.14.

00:00 Go Floating-Point Types Overview
00:11 float32 vs float64 and Default Type
00:58 IEEE 754 Memory Structure (Sign, Exponent, Mantissa)
01:45 Floating-Point Literals (Decimal, Scientific, Hex)
03:14 Special Values: Inf, NaN, Negative Zero
04:18 Why NaN as Map Key Is Dangerous
04:53 Why Not Use == with Floats
05:42 Safe Float Comparison with Epsilon
06:13 Relative Epsilon for Large Numbers
06:56 Float Conversions and Truncation
07:41 Rounding Functions (Floor, Ceil, Round, RoundToEven)
08:23 Formatting Floats with fmt.Printf
09:04 String ↔ Float Conversion (strconv)
10:26 Important math Package Functions
11:18 math.Mod vs math.Remainder
12:03 When to Use math/big.Float
12:33 NaN and Infinity Problems in JSON
13:15 Sorting and Comparing Floats Safely
13:49 Why Floats Are Dangerous as Map Keys
14:33 Subnormal Numbers and Gradual Underflow
15:02 Why Not Use Floats for Money
15:25 Accumulation Error (Adding 0.1 Repeatedly)
15:51 Kahan Summation Algorithm
16:20 Integer Division Mistake When Expecting Float
16:49 Float Overflow to Infinity
17:17 float32 to float64 Precision Myth
17:49 When to Use float32 for Performance
18:26 complex64 vs complex128
18:58 Unsafe Bit Manipulation with Floats
19:36 Atomic Operations with float64
20:18 Float Comparison in Tests
20:43 Floats vs DECIMAL in Databases

We explain the internal structure of float32 and float64, including sign, exponent, and mantissa bits, and show how to inspect raw binary values using functions like math.Float64bits and math.Float64frombits.

You will see all the ways to write floating point literals in Go: normal decimals, scientific notation, hexadecimal floating point literals like 0x1p10, and readable numbers with underscores. We also explain how hex floats use powers of 2 instead of powers of 10.

Special values such as positive infinity, negative infinity, NaN, and negative zero are covered in detail. You will learn why NaN is not equal to itself, why using NaN as a map key is dangerous, and how infinity behaves in arithmetic operations. We also explain why floats should not be used as map keys in production code.

We demonstrate why comparing floats with double equals is unsafe, how precision errors happen with values like 0.1 plus 0.2, and how to use epsilon and relative epsilon for safe comparisons. You will also learn why tests should use tolerance-based comparisons instead of direct equality.

The video covers float conversion rules, including precision loss when converting float64 to float32, truncation when converting float to int, and overflow behavior that silently produces infinity. We also explain why converting float32 to float64 does not increase accuracy.

You will learn about rounding functions such as math.Floor, math.Ceil, math.Trunc, math.Round, and math.RoundToEven, and how to properly format floats using fmt.Printf with verbs like percent f, percent e, and percent g.

We show how to convert strings to floats using strconv.ParseFloat and convert floats to strings using strconv.FormatFloat, including handling scientific notation and special values.

Important math package functions such as Abs, Sqrt, Pow, Log, Exp, Sin, Cos, Tan, Mod, and Remainder are explained, including the difference between math.Mod and math.Remainder.

Advanced topics include subnormal numbers, accumulation error, the Kahan summation algorithm, integer division mistakes, float overflow, atomic operations with float64 using uint64 and math.Float64bits, unsafe bit manipulation, and sorting floats with special values like NaN.

We also explain why floats should not be used for money, why DECIMAL or NUMERIC database columns should not be scanned into float types, and when to use big.Float or big.Rat for high precision work.

Finally, we compare float32 and float64 performance tradeoffs, explain when float32 makes sense for memory or graphics workloads, and introduce complex64 and complex128 types, including how to access real and imaginary parts.

#Go #Golang #Float32 #Float64 #IEEE754 #Precision #NaN #Infinity #Epsilon #KahanSum #GoTutorial #BackendDev #Programming #GoLangDev #FloatMath