NumeroLogic: Number Encoding for Enhanced LLMs' Numerical Reasoning

Eli Schwartz, Leshem Choshen, Joseph Shtok, Sivan Doveh, Leonid Karlinsky, Assaf Arbelle

Abstract

Large Language Models (LLMs) frequently struggle with arithmetic and numerical reasoning due to their causal, token-by-token processing, which obscures a number's magnitude until the entire sequence is read. We present NumeroLogic, a formatting technique that prefixes numbers with their digit count (e.g., "2:42"). This provides models with immediate magnitude awareness and an implicit Chain of Thought (CoT) during generation. Our results demonstrate significant improvements in arithmetic accuracy—raising 3-digit addition from 88.37% to 99.96% in small models—and enhanced performance on STEM-related tasks in the MMLU benchmark. This lightweight, training-compatible reformatting requires no architectural changes while significantly boosting the numerical intelligence of transformer-based models.

Introduction

Despite their transformative capabilities in natural language processing, Large Language Models (LLMs) remain notoriously unreliable at basic arithmetic. Even state-of-the-art models like GPT-4 can fail on relatively simple multi-digit multiplication. This research identifies a fundamental structural cause for this failure: the causal nature of transformer architectures.

When a model processes a number like "1,234,567" from left to right, it encounters the digit "1" first. Without knowing the total length of the string, the model cannot determine if that "1" represents one million, one thousand, or a single unit. The true "place value" of leading digits is only revealed once the model reaches the end of the numerical string. This "nearsightedness" prevents the model from building an accurate internal representation of magnitude during the initial processing phase, leading to cascading errors in complex calculations.

The NumeroLogic Method

To address this, we propose NumeroLogic, a simple reformatting technique designed to be integrated into model training. Instead of representing numbers as raw digit strings, NumeroLogic prefixes each number with a metadata tag indicating its scale.

Encoding Scheme

The implementation uses a regex-based pre- and post-processing layer that wraps numbers in specific tags:

  • Integers: A number like "42" is reformatted as <sn>2<mn>42<en>, where <sn> denotes the "start number" and <mn> (mid-number) separates the digit count from the value.
  • Floating Points: A value like "3.14" becomes <sn>1.2<mn>3.14<en>, explicitly stating there is one integer digit and two decimal digits.

Dual Benefits

This approach provides two primary advantages to the transformer:

  1. Magnitude Awareness: The model is informed of the place value of every digit the moment it begins reading the number.
  2. Implicit Chain of Thought (CoT): During the generation phase, the model must predict the number of digits before committing to the specific digits themselves. This forces the model to "reason" about the expected magnitude of a result before performing the granular calculation.

Experimental Setup

We evaluated the efficacy of NumeroLogic across a range of model scales and task complexities:

  • Synthetic Arithmetic: We generated 300,000 random equations covering addition, subtraction, and multiplication (up to 5 digits), alongside 30,000 samples for transcendental functions like sine and square roots.
  • General Pretraining: To test broader applicability, we performed self-supervised continued pretraining on a Llama2-7B model using a subset of the RefinedWeb dataset.
  • Benchmarks: Beyond raw arithmetic, we utilized the MMLU (Massive Multitask Language Understanding) benchmark to observe how improved numerical grounding affects general reasoning in STEM fields.

Key Results and Discussion

Performance Gains

The performance improvements were substantial across all tested architectures. In small-scale models (NanoGPT), accuracy for 3-digit addition rose from 88.37% to 99.96%. Multiplication, a traditionally difficult task for LLMs, saw accuracy more than double, increasing from 13.81% to 28.94%.

In larger models like Llama2-7B, the impact was even more pronounced. For 5-digit subtraction, the NumeroLogic-enhanced model corrected over 80% of the errors produced by the plain-text baseline, achieving a near-perfect 99.93% accuracy.

STEM Generalization

Crucially, the benefits of NumeroLogic extend beyond simple calculator-style tasks. On the MMLU benchmark, the model showed a 1.16% improvement on tasks specifically containing numbers, compared to a negligible 0.14% improvement on non-numerical tasks. This suggests that a better grasp of numerical scale is a prerequisite for broader reasoning in scientific and technical domains.

Ablation: Content vs. Tokens

A common critique of such methods is that they work merely by providing "extra tokens" or "thinking time." To test this, we conducted an ablation study replacing the NumeroLogic prefixes with random white spaces. While the white spaces provided a minor boost (likely by mitigating positional encoding issues), they were significantly outperformed by the actual digit-count prefixes. This confirms that the semantic content of the prefix—the explicit scale information—is the primary driver of performance.

Conclusion and Future Work

NumeroLogic demonstrates that LLMs are not inherently incapable of math; rather, they are hampered by a data format that is poorly suited to their causal processing. By providing an explicit sense of scale, we can significantly enhance their numerical reasoning capabilities without changing the underlying transformer architecture.

Future research will focus on reducing the token overhead associated with these prefixes and exploring the impact of incorporating NumeroLogic during the initial pretraining phase of 70B+ parameter models. Additionally, investigating how this formatting interacts with non-Arabic numerical systems and diverse linguistic contexts remains a priority.

Summary updated

BibTeX
@inproceedings{schwartz2024numerologic,
  author = {Eli Schwartz and Leshem Choshen and Joseph Shtok and Sivan Doveh and Leonid Karlinsky and Assaf Arbelle},
  title = {{NumeroLogic: Number Encoding for Enhanced LLMs' Numerical Reasoning}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.emnlp-main.12},
}
Figure