In this article
  1. Before you start
  2. Why interviewers ask this
  3. Floating point number from first principles
  4. Worked example
  5. Trade-offs and edge cases
  6. How to answer it in an interview
  7. Common mistakes
  8. Follow-up questions
  9. Sources

Before you start

This page is one stop in the coding interview guide, and it needs only three ideas.

Why interviewers ask this

Floating point appears in coding rounds wherever a problem averages, divides, measures a distance or compares two computed values for equality, so the question is a quick check on whether you know what a number in code really is.

A weak answer says "computers are bad at decimals." A strong answer says the stored value is the nearest representable binary fraction, that the error is deterministic rounding, and that you compare with a tolerance. A Staff-level answer adds that the gap between neighboring floats grows with the size of the number, that integers are exact only up to a limit, and when to avoid floats entirely, such as for money. The same habits matter in Python interview questions, where 0.1 + 0.2 is a classic.

Floating point number from first principles

Floating point number
A floating point number is a number stored as a sign, an exponent and a significand in a fixed number of bits, so the position of the binary point moves with the exponent. It covers a very wide range of sizes but keeps only a fixed number of significant digits.
Significand
A significand is the string of significant digits of a floating point number, the part that is multiplied by a power of two. It is also called the mantissa, and a stored fraction field holds its digits after the leading one.
Machine epsilon
Machine epsilon is the gap between 1.0 and the next larger float in a given format. It measures the precision of the format; some texts instead define it as the largest relative rounding error, which is half that gap.

In plain words

Picture a ruler with marks only at certain places. You measure something and read the nearest mark. Near zero the marks are packed close together. Far from zero they are spread wide apart. A computer's number line works the same way. Every real number is replaced by the nearest mark.

In one precise sentence, a floating point representation stores a number as plus or minus a significand times a base raised to an exponent, which Goldberg (1991) writes as ± d.dd...d × β^e, where β is the base, 2 in binary. Squeezing infinitely many real numbers into finitely many bits forces an approximate representation, as the same paper puts it.

Step 1: A float is split into sign, exponent and fraction

A floating point number is stored in three fields: one sign bit, an exponent field and a fraction field. In the 32-bit format (binary32) the fields have 1, 8 and 23 bits; in the 64-bit format (binary64) they have 1, 11 and 52 bits. Goldberg's Table D-1 gives a precision of 24 and 53 bits and an exponent width of 8 and 11 bits, and the fraction field holds one bit fewer than the precision because the leading bit is implied.

The exponent bias is a fixed number added to the true exponent so the exponent field can be stored as an unsigned number. It is 127 for binary32 and 1,023 for binary64, equal to the largest exponent (+127 and +1,023) in the same table; the formula 2^(w−1) − 1 for an exponent width w is computed in the data script.1

You now know where the bits go. Which order the bytes of those bits take in memory is a separate matter, covered in little-endian byte order.

Step 2: The three fields decode into one value

A normal float decodes as x=(−1)s×1.f×2E−b, where s is the sign bit, f the fraction bits, E the exponent field and b the bias. The leading digit can be left out of storage because a normalized number has a nonzero leading digit, and in binary that digit can only be one (Goldberg, 1991).

Take 6.5 in binary32. It equals 1.101 in binary times 2 squared, so the sign bit is 0, the exponent field is 129 (10000001 in binary) and the fraction field starts 101 followed by zeros.1 For -0.15625 the sign bit is 1, the exponent field is 124 and the fraction field starts 01 followed by zeros, because the value is -1.25 times 2 to the power of minus 3.1

Step 3: Rounding to the nearest value makes 0.1 inexact

Most decimal fractions have no finite binary form, so the computer stores the nearest float instead. Goldberg (1991) shows that with a 24-bit significand, 0.1 becomes about 1.10011001100110011001101 × 2^-4; in binary32 its exact value is 0.100000001490116119384765625.1 In binary64, the Python tutorial shows 0.1 stored as 3602879701896397 / 2^55, which is 0.1000000000000000055511151231257827 to 34 decimal places, about 5.55 × 10^-18 above one tenth.1

This is not randomness. It is rounding to the nearest representable value, the same every run.

The unit in the last place is the gap between a float and the next larger float; it is the size of the smallest step at that magnitude, and Goldberg (1991) uses it to measure error. Because the gap grows with the number, relative error, not absolute error, is the fair yardstick. Rounding errors then add up through every operation, which sets up the equality trap you meet next. Comparisons use a tolerance, not equality, for exactly this reason.

Step 4: Comparing floating point numbers needs a tolerance

Floating point numbers that look equal on paper can differ after arithmetic. In the Python tutorial, math.isclose is the function suggested for comparing inexact values; the math module documentation lists it with rel_tol and abs_tol parameters for a relative and an absolute tolerance.

A relative tolerance scales with the numbers: two values pass if their difference is small compared with the larger value. An absolute tolerance is a fixed gap. Use a relative tolerance for numbers far from zero. It fails when one value is zero, because the allowed gap shrinks to nothing, so add an absolute tolerance there.1 This is why math.isclose defaults to abs_tol=0.0: isclose(x, 0.0) is false for any nonzero x until you pass abs_tol yourself.1

The tolerance of 1e-9 used in this page's examples is illustrative; the right size depends on your data. Your own code should state it, not hide it.

A float is split, decoded, rounded and then compared with a toleranceColumn flow of four steps: a float is split into sign, exponent and fraction, decoded into one value, rounded to the nearest representable value, and finally compared with a tolerance instead of equality.Split into three fieldssign, exponent, fractionDecode the valuesign, 1.fraction, power of twoRound to nearest value0.1 lands on a nearby floatCompare with a tolerancenot with equalityDiagram: Interview Clozo, CC BY 4.0 ·interview.clozo.ai/blog/floating-point-number.html

A float is a rounded value, so the last step of any float calculation is a tolerance comparison, not an equality test.CC BY 4.0 — source: What is a floating point number, and why do computers round it?
Step-by-step description
  1. Sign, exponent and fraction share the bits.
  2. The fields combine into one value.
  3. Most decimals land on a nearby float.
  4. Use a tolerance, not equality.
  • A float is stored as a sign, an exponent and a fraction.
  • The stored value is the nearest representable number.
  • Comparisons use a tolerance, not equality.

What 32-bit and 64-bit floats can hold

A 32-bit floating point number holds about 7 significant decimal digits and a 64-bit one about 16. The reason is that 24 and 53 bits of precision are worth 24 × log10(2) ≈ 7.2 and 53 × log10(2) ≈ 16.0 decimal digits.1 The largest finite binary32 value is 3.4028234663852886e+38 and its smallest normal value is 1.1754943508222875e-38; for binary64 they are 1.7976931348623157e+308 and 2.2250738585072014e-308.1 Subnormal numbers fill the gap below the smallest normal value, down to 1.401298464324817e-45 in binary32 and 5e-324 in binary64.1

The reserved exponent fields hold the special values (Goldberg, 1991). Zero has every bit clear, and a negative zero has only the sign bit set and compares equal to it, so -0.0 == 0.0 is true.1 An all-ones exponent with a zero fraction is infinity, and with a nonzero fraction it is NaN.

Knowing it is half the job. Saying it is the other half.

Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.

Start Free Trial →

Worked example

Add 0.1 and 0.2 in binary641 and compare the sum with 0.3.1 The stored 0.1 is 3602879701896397 / 2^55, and the stored 0.2 is exactly twice that, 3602879701896397 / 2^54.1 Their exact sum is 10808639105689191 / 2^55, which needs 54 bits, one more than the 53 available. It lies exactly halfway between two neighbors, 5404319552844595 / 2^54 and 5404319552844596 / 2^54, so the default rule, round half to even, picks the one whose last bit is 0: 5404319552844596.1

The stored 0.3 is 5404319552844595 / 2^54, one unit lower.1 So the sum prints as 0.30000000000000004, it differs from the stored 0.3 by about 5.55 × 10^-17, and == returns false.1 Adding 0.1 ten times in a loop gives 0.9999999999999999, not 1.0.1

With a relative tolerance of 1e-9 (illustrative), the sum and 0.3 compare as close. Compare the difference with zero using that same relative tolerance and the test fails; an absolute tolerance of 1e-9 (illustrative) passes.1

The sum of 0.1 and 0.2 rounds one unit away from 0.3Working in binary64: stored 0.1 is 3602879701896397 over 2 to the 55, stored 0.2 is twice that, their exact sum needs 54 bits and is rounded to a neighbor, and stored 0.3 is one unit lower.0.1 -> 3602879701896397 / 2^55Stored 0.1 in binary64.0.2 -> 3602879701896397 / 2^54Stored 0.2 is twice stored 0.1.sum -> 10808639105689191 / 2^55Needs 54 bits, so it must be rounded.round -> 5404319552844596 / 2^54A tie: round half to even picks …596.0.3 -> 5404319552844595 / 2^54One unit apart, so equality is false.Diagram: Interview Clozo, CC BY 4.0 ·interview.clozo.ai/blog/floating-point-number.html
The sum of the stored 0.1 and 0.2 rounds to a value one unit above the stored 0.3, so equality fails.CC BY 4.0 — source: What is a floating point number, and why do computers round it?
Text description of the diagram
  • The stored 0.1 is 3602879701896397 / 2^55.
  • The stored 0.2 is exactly twice the stored 0.1.
  • The sum rounds to 5404319552844596 / 2^54 and the stored 0.3 is 5404319552844595 / 2^54.
Neighboring 64-bit floats near 1 are 2.2 × 10^-16 apart, but the gap widens to 2.0 by ten quadrillion, where it exceeds one.
QuantityValue (computed)
Stored 0.1, 32-bit0.100000001490116119384765625
Stored 0.1, 64-bit0.1000000000000000055511151231257827 (first 34 places)
Machine epsilon, 32-bit1.1920928955078125e-07
Machine epsilon, 64-bit2.220446049250313e-16
Gap between floats near 1,000,0001.1641532182693481e-10
Gap between floats near 10,000,000,000,000,0002.0
0.1 + 0.20.30000000000000004
2^53 + 1 stored as a floatequals 2^53, which is 9007199254740992
2^53 + 3 stored as a floatrounds up to 9007199254740996

Trade-offs and edge cases

Floating point trades exactness for range. A float gives a wide range in a fixed size. The Python tutorial, read October 2026 says almost all platforms map Python floats to the Institute of Electrical and Electronics Engineers (IEEE) 754 binary64 "double precision" format, which holds 53 bits of precision: the 52 stored fraction bits plus one implied leading bit. Because the gap grows with size, integers above 2^53 are no longer all representable: adding one to 2^53 returns 2^53 again.1 A 32-bit float has a machine epsilon 2^29 times larger than a 64-bit one, about 5.4 × 10^8 or roughly nine orders of magnitude, so it suits graphics or machine learning and not careful accounting.1

Choose the number comparison by the size of the numbersTable of four ways to compare numbers in code: equality, absolute tolerance, relative tolerance and integer cents, each with when it works and when it fails.MethodWorks whenFails whenEqualityvalues areidenticalany roundinghappenedAbsolute tolerancescale is knownvalues huge ortinyRelative tolerancefar from zeroa value is zeroInteger centswhole unitssufficefractions areneededDiagram: Interview Clozo, CC BY 4.0 ·interview.clozo.ai/blog/floating-point-number.html
No single float comparison works everywhere, so choose it by the size of the numbers.CC BY 4.0 — source: What is a floating point number, and why do computers round it?
Text description of the diagram
  • Relative tolerance fails when one value is zero.
  • Absolute tolerance fails when values are huge or tiny.
  • Whole cents in integers are exact.

Edge cases follow from the table. Relative tolerance fails when one value is zero; absolute tolerance fails when values are huge or tiny. NaN (not a number) is a special value that IEEE 754 adds for undefined results, alongside infinity and denormalized numbers; it compares unequal to itself, so test it with math.isnan.1 For money, a strong answer keeps whole cents in integers, which are exact.

In a k closest points problem, compare squared distances rather than taking square roots; with whole-number coordinates the squared distances are exact integers. Checking whether two matrices are equal needs a tolerance when the entries are floats. A prefix-sum and hash-map solution relies on exact equality, so it needs integer sums.

How to answer it in an interview

Interviewer asks

What is a floating point number, and why does a computer get decimals slightly wrong?

A strong answer

A floating point number is a sign, an exponent and a fraction packed into a fixed number of bits, so it covers a huge range with limited precision. Most decimals have no finite binary form, so the stored value is the nearest representable one. That error is deterministic rounding, not randomness. It is why a tenth plus two tenths misses three tenths by a hair. So I never test computed floats with equality. I use a relative tolerance, add an absolute one near zero, and keep money in integers.

The spoken structure: define the three fields, explain that most decimals are rounded, show the equality failure, then give the tolerance and the integer fallback.

Common mistakes

  • "Floating point is random or buggy." (wrong) The result is deterministic rounding to the nearest representable value (Goldberg, 1991). (right) "The error is repeatable and predictable."
  • "0.1 is stored exactly." (wrong) It is stored as a nearby binary fraction.1 (right) "The stored value is the nearest float to 0.1."
  • "I check a == b after arithmetic." (wrong) The sum of 0.1 and 0.2 is not equal to 0.3.1 (right) "I use a tolerance."
  • "A relative tolerance works for everything." (wrong) It fails against zero. (right) "I pair it with an absolute tolerance."

Follow-up questions

Floating point: how do you compare?

Use a tolerance: math.isclose in Python, or an absolute difference checked against a bound you choose. Pick relative for large values and absolute near zero; Step 4 and the diagram above show why.

What is machine epsilon, and is it the same as the rounding error?

Machine epsilon is the gap between 1.0 and the next float, 2.220446049250313e-16 in binary64.1 The largest relative rounding error is half that gap, so some texts, including Goldberg's, use the smaller value; state which convention you mean.

How do you compare a floating point number with zero?

Use an absolute tolerance, because a relative one scales with the value and shrinks to nothing. The difference of 0.1 plus 0.2 and 0.3 passes an absolute tolerance of 1e-9 (illustrative) and fails the relative one.1

Why does adding one to a very large float change nothing?

From 2^53 up, the gap between neighboring binary64 floats is at least 2, so x + 1 is not representable. Between 2^53 and 2^54 the result is a tie that rounds to the even neighbor, so 2^53 + 1 gives 2^53 but 2^53 + 3 gives 2^53 + 4. From 2^54 up, the gap is 4 or more and adding one always changes nothing.1

Should you use floating point numbers for money?

A strong answer keeps money in integer units such as cents, or uses an exact decimal type, because rounding errors add up and tenths are inexact in binary.

Is NaN equal to NaN?

No. A NaN compares unequal to everything, itself included, so math.isnan is the test.1

Key takeaways
  • A floating point number is a sign, an exponent and a fraction stored in fixed bits.
  • Most decimal fractions are stored as the nearest representable binary value.
  • The rounding is deterministic, and the gap between floats grows with the number's size.
  • Compare computed floats with a tolerance, relative for large values and absolute near zero.
  • Keep money and exact counts in integers.

Sources

  1. What Every Computer Scientist Should Know About Floating-Point Arithmetic — Oracle docs, reprint of Goldberg, ACM Computing Surveys; read 2026-10-26, 1 Mar 1991
  2. Floating-Point Arithmetic: Issues and Limitations (Python tutorial) — Python Software Foundation; accessed 2026-10-26, 26 Oct 2026
  3. math: Mathematical functions (Python documentation) — Python Software Foundation; accessed 2026-10-26, 26 Oct 2026