Language Models Use Trigonometry to Do Addition

Subhash Kantamneni, Max Tegmark

TL;DR

Researchers have discovered that mid-sized LLMs like GPT-J and Llama 3.1 represent numbers using a "generalized helix"—a geometric structure combining linear and periodic (trigonometric) components. To perform addition, these models use a "Clock" algorithm, rotating these internal helices much like the hands of a clock to arrive at a sum.

What Problem Does This Paper Address?

While Large Language Models (LLMs) show an impressive aptitude for mathematics, the internal "black box" of how they actually compute a sum has remained largely mysterious. Do they simply memorize addition tables? Do they use a "slide rule" linear approach? Or is there a more robust internal algorithm at play?

Previous research identified a "Clock" algorithm for modular addition in tiny, one-layer transformers, but it wasn't clear if this scaled to the multi-billion parameter models we use today for standard integer addition. This paper aims to provide a "representation-level" explanation of mathematical reasoning in production-grade LLMs.

Methods

The authors reverse-engineered the addition process in three models: GPT-J (6B), Pythia-6.9B, and Llama 3.1-8B. Their approach combined several Mechanistic Interpretability (MI) techniques:

  1. Fourier Analysis: They applied Fourier transforms to the model's internal hidden states (the residual stream) to detect periodic patterns.
  2. Helix Fitting: They modeled number representations as a "generalized helix," which includes a linear component (the value of the number) and several circular components (trigonometric sine/cosine terms) corresponding to specific periods like $T=2, 5, 10, 100$.
  3. Activation Patching: To prove the model actually uses these helices, they performed causal interventions. They replaced the model's real activations with their simplified "helical fit." If the model still produced the correct answer, it proved the helix captured the essential information used for the calculation.
  4. Path Patching: They traced the flow of information to identify which specific layers and neurons were responsible for "rotating" the helices.

Datasets

The primary dataset consisted of 10,000 addition problems ($a + b$) where $a$ and $b$ are integers between 0 and 99. To test the generality of the helical representation, the authors also evaluated the models on:

  • Subtraction ($a - 23$)
  • Integer division ($a // 5$)
  • Multiplication ($a * 1.5$)
  • Modular arithmetic ($a \pmod 2$)

Key Insights and Ideas

  • Numbers as Helices: LLMs don't just see "73" as a point on a line. They represent it as a position on a helix. This structure captures both the magnitude (linear) and the "circular" nature of our decimal system (e.g., the units digit repeats every 10).
  • The Clock Algorithm: Addition is performed by rotation. To solve $3 + 63$, the model takes the helix for $3$ and "rotates" it by the amount represented by the helix for $63$. This is visualized in Figure 1.
Figure 1. Illustrating the Clock algorithm. We find that LLMs represent numbers on a helix. When computing the addition prob- lem a + b, LLMs rotate the a and b helices, as if on a clock, to create the a + b helix and read out the final answer.
  • Why Trigonometry? The authors conjecture that LLMs use this helical representation because it acts as a "built-in redundancy" or error-correcting code. Purely linear representations are noisy; by mapping numbers to circles (like the $T=10$ units digit), the model can be much more precise.
  • Layer Specialization: The "Clock" algorithm is a multi-stage process. Early layers (9-14) move the number representations to the final token. Middle layers (MLPs 14-18) perform the actual "rotation" to create the $a+b$ helix. Finally, late layers (MLPs 19-27) "read" this helix to predict the text of the answer.

Key Results

  • Causal Evidence: The helical fit was highly effective. In GPT-J, patching in the helical representation (using only 9 parameters) often outperformed a 27-dimensional PCA baseline, as shown in Figure 4. This suggests the helix is a "truer" description of the model's internal variables than simple linear directions.
Figure 4. Helix causal intervention results. We use activation patching to causally determine if our fits preserve the information the model uses to compute a + b. We find that our helical and circular fits are strongly causally implicated, often outperforming the PCA baseline.
  • Periodic Neurons: The researchers found individual neurons whose activations were perfectly periodic. For example, some neurons fire based on whether the sum $a+b$ is even or odd ($T=2$), while others track the units digit ($T=10$).
  • Predictable Errors: When GPT-J makes a mistake, it is most often off by exactly $\pm 10$ (over 70% of numeric errors). This is a "smoking gun" for the Clock algorithm; it's the equivalent of a human misreading a clock face by one full position.

Limitations and Future Work

While the paper proves that models use these helices, the exact low-level circuit—the specific "gears" of the clock—remains partially hidden. The authors hypothesize that MLPs use trigonometric identities (like $\cos(a+b) = \cos a \cos b - \sin a \sin b$) to perform the rotation, but they haven't yet isolated the specific neurons performing that exact multiplication.

Additionally, the study focuses on single-token numbers (0-99). Future work is needed to see how models handle larger numbers where tokenization splits digits (e.g., "1,234"), which likely requires more complex algorithms to "carry" values between tokens.

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