Language Models Use Trigonometry to Do Addition

Subhash Kantamneni, Max Tegmark

TL;DR

Large language models represent numbers not as simple scalar magnitudes, but as multi-dimensional generalized helices combining linear trends with periodic circular components (periods 2, 5, 10, and 100). When performing addition ($a + b$), models implement the mechanistic "Clock" algorithm, rotating and manipulating the $a$ and $b$ helices across attention heads and MLPs to construct an answer helix $a + b$, which downstream neurons decode into output logits.

What Problem Does This Paper Address?

While LLMs display strong mathematical and reasoning capabilities, mechanistic interpretability (MI) has struggled to explain the concrete algorithms and internal representations underlying basic arithmetic. Most MI work either maps circuits (which components fire) or isolates linear features (what directions represent concepts). This paper bridges the gap by offering a representation-level mechanistic explanation: reverse-engineering how three mid-sized models (GPT-J 6B, Pythia-6.9B, and Llama3.1-8B) geometrically represent numbers and manipulate those representations to compute integer addition.

Methods

  • Helical Representation Modeling: The authors model the residual stream representations of integers $a$ as a generalized helix combining a linear direction and $k$ Fourier (circular) features: $$B(a) = \left[ a, \cos\left(\frac{2\pi}{T_1}a\right), \sin\left(\frac{2\pi}{T_1}a\right), \dots, \cos\left(\frac{2\pi}{T_k}a\right), \sin\left(\frac{2\pi}{T_k}a\right) \right]$$ Dominant periods identified via Fourier analysis on layer 0 activations are $T \in [2, 5, 10, 100]$.

  • Causal Activation & Path Patching: Rather than relying solely on correlational probes, the authors causally validate whether the model uses these helical forms by replacing clean residual stream activations and individual head/neuron preactivations with synthetic helical fits, measuring logit difference recovery.

  • Circuit Dissection (focused primarily on GPT-J):
  • Attention Heads: Classified into a, b heads (move operand helices to the final token), a + b heads (project answer helix to logits), and rare mixed heads.
  • MLP Layers and Neurons: Analyzed using attribution patching (gradient $\times$ activation) and LogitLens to track how preactivations read helical features and write periodic contributions to output logits.

Datasets

The authors evaluate models on:

  • Synthetic Addition Benchmark: All 10,000 two-digit addition pairs $a + b$ where $a, b \in [0, 99]$ (restricted to ensure single-token input and output representations across models).
  • Secondary Arithmetic Probes: Five arithmetic tasks to test generality: subtraction ($a - 23$), integer division ($a // 5$), decimal multiplication ($a \times 1.5$), modulo arithmetic ($a \pmod 2$), and algebraic subtraction ($x - a = 0$).

Key Insights and Ideas

  • Numbers Live on Multi-Dimensional Helices: Numerical representations in LLMs are not strictly linear. They form a non-linear manifold featuring both a linear trend and orthogonal circular rings reflecting base-10 structure and discrete mathematical properties (e.g., $T=2$ for parity/evenness, $T=5, 10$ for modular decimal units).
  • The "Clock" Algorithm in Real Models: Previously observed in toy one-layer transformers trained on modular arithmetic, the Clock algorithm operates in commercial-scale pretrained models:
  • Input tokens $a$ and $b$ embed into helices.
  • Attention heads in layers 9–14 transport these operand helices to the prompt's final token (=).
  • Intermediate MLPs (layers 14–18) manipulate the inputs to synthesize the answer helix $\text{helix}(a + b)$.
  • Late-stage MLPs (layers 19–27) and late attention heads decode $\text{helix}(a + b)$ into token vocabulary logits.
  • Why Not Use a Line? Error Correction: When performing addition along an optimal linear fit ($R^2 = 0.997$), accuracy plummets below 20% due to accumulated noise. Helical representations serve as an error-correcting code, providing redundant discrete angular constraints analogous to human decimal arithmetic rather than analog slide rules.
  • Decoding Model Mistakes: GPT-J's errors are heavily concentrated at $\pm 10$ (over 70% of numerical mistakes). This is not caused by failed carry operations, but because output neurons and final logits exhibit an inherent periodicity of 10, occasionally confusing adjacent turns of the modular dial.

Key Results

  • Model Addition Accuracies: Llama3.1-8B achieved 98.0%, GPT-J achieved 80.5%, and Pythia-6.9B reached 77.2% on the 10,000 $a + b$ prompts.
  • Helical Causal Faithfulness: A 9-parameter helical fit ($T=[2, 5, 10, 100]$ plus linear) outperforms a 9-dimensional PCA baseline and approaches full layer-patching performance. Ablating the fitted helix subspace degrades accuracy as severely as ablating the entire residual stream.
  • Circuit Sparsity:
  • Patching just 20 attention heads (out of 448 in GPT-J) recovers 83.9% of the total attention effect.
  • Patching 11 MLPs achieves 95% of the full MLP effect.
  • Patching only ~1% of neurons (4,587 neurons) preserves 80% model task accuracy; among them, just ~700 neurons drive 80% of direct logit effects.

Limitations and Future Work

  • Exact Trigonometric Construction Unknown: While the paper proves that MLPs 14–18 synthesize $\text{helix}(a+b)$ from $\text{helix}(a, b)$, the exact low-level arithmetic mechanism (e.g., angle-addition formulas like $\cos(a+b) = \cos a \cos b - \sin a \sin b$ vs. numerical integration) remains unresolved.
  • Model Architectural Differences: The Clock algorithm fit is significantly weaker in Llama3.1-8B, likely because SwiGLU / gated MLPs learn alternative or ensemble algorithms (such as the "Pizza" algorithm).
  • Scope Restrictions: Analysis is restricted to two-digit single-token numbers. Multi-token numbers, multi-digit carry mechanics, and tokenization boundary effects (e.g., Gemma-2's digit-by-digit tokenizer) require further study.

Summary updated

BibTeX
@inproceedings{kantamneni2025language,
  author = {Subhash Kantamneni and Max Tegmark},
  title = {{Language Models Use Trigonometry to Do Addition}},
  booktitle = {BuildingTrust @ ICLR},
  year = {2025},
  url = {https://openreview.net/forum?id=CqViN4dQJk},
}
Figure