The Maths You Actually NeedLesson 47 of 76

Module 5 · Logarithms, information and the shape of a loss

The log-sum-exp trick, and why exp(1000) is not a number

In this lesson

  • 9 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 9 of 10 in this chapter

This chapter’s goalCompute a cross-entropy and a KL divergence by hand from two small distributions and say which part of a training loss is irreducible, derive the squared-error and cross-entropy losses from a stated noise assumption, compute a softmax and its gradient for a given set of logits, and explain by the overflow limit of float32 why every library subtracts the maximum before exponentiating.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmNot availableNot available
Full audiobookNot availableNot available
Softmax of the logits 1000 and 999, computed two waysStraight from the formulae to the 1000 becomes infe to the 999 becomes inftheir sum is infinf divided by inf is NaNone NaN spreads to every weight it touchesSubtract the largest logit firstthe logits become 0 and minus 1e to the 0 is 1, e to the minus 1 is 0.368their sum is 1.368the answer is 0.731 and 0.269exact to every digit float32 can holdFloat32 overflows at exp of 88.7, so a logit of 89 is already infinite. Subtracting the largest logitchanges nothing about the answer, because softmax cannot see a shift shared by every logit, and it isthe difference between a number and NaN.
Float32 overflows at exp of 88.7, so a logit of 89 is already infinite. Subtracting the largest logit changes nothing about the answer, because softmax cannot see a shift shared by every logit, and it is the difference between a number and NaN.

Figure

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

The overflow that produces NaN

A float32 number cannot exceed about 3.4 × 10^38. The exponential passes that at x = 88.7, so exp(89) in single precision is not a large number: it is inf. Logits of 89 are not exotic; an untrained network with a bad initialisation or a training run whose learning rate is too high produces them routinely.

Apply the naive softmax formula to z = (1000, 999). Both exponentials are inf. The sum is inf. Each ratio is inf / inf, which the floating-point standard defines as NaN. The answer should have been (0.731, 0.269), since the logits differ by exactly 1 and e / (e + 1) = 0.731. The arithmetic was easy; the intermediate values were not representable.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Funke, your AI tutor

How to cite: Addaly (2026) ‘The log-sum-exp trick, and why exp(1000) is not a number’, in The Maths You Actually Need. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/maths-you-need/log-sum-exp-and-stable-softmax (Accessed: 29 November 2026).