LLM Decimal Comparison Errors: Causes and Engineering Mitigations
Large language models can misinterpret numeric strings such as 9.9 and 9.11 because they learn text distributions, rely on tokenization, face format ambiguity, and decode probabilistically. This article explains those causes and outlines engineering practices for reliable numeric handling, including deterministic tools, explicit prompts, structured validation, and evaluation sets.
1. The Starting Point: One String of Digits, Two Readings
“9.9” and “9.11” look simple, but the answer depends on context.
- As decimals: 9.9 equals 9.90, which is greater than 9.11.
- As version numbers: The minor version in 9.11 is greater than that in 9.9, so 9.11 is newer.
Without explicit semantic constraints, a model may waver between the two readings and produce a conclusion that does not match the expected answer. This shows that the core of many numeric problems is often not the calculation itself, but the judgment of semantics.
2. Why LLMs Make Mistakes in Numeric Comparisons
2.1 Language Models Learn Text Distributions, Not Arithmetic Rules
The training objective of a language model is to predict the next token. It learns from large text corpora which words tend to appear alongside “9.9,” but it does not automatically acquire strict ordering relationships. When the training corpus contains decimals, version numbers, section numbers, and dates at the same time, the model’s interpretation of the same notation is influenced by the corpus distribution.
2.2 Tokenization Affects Digit Alignment
Before text enters the model, it is split into tokens. The same number may be split into different fragments: the integer part and fractional part may be separated, or multiple digits may be merged into a single token. Digit alignment is natural in human mental arithmetic, but it does not directly exist in a token sequence. The model must reconstruct place-value relationships from fragments, and this step is error-prone.
2.3 Format Ambiguity Is Everywhere
The following notations have different meanings in different scenarios:
| Notation | Possible meaning | Comparison rule |
|---|---|---|
| 9.9 and 9.11 | Decimal or version number | Decimals by numeric value; version numbers segment by segment |
| 2026-11-25 | Date | Chronological order |
| Section 3.10 | Section number | Hierarchical order |
If the input does not state the format, the model can only infer it from common patterns, and the inference result does not always match the user’s intent.
2.4 Probabilistic Nature of Generative Decoding
The model generates tokens one by one, selecting each result from a probability distribution. Even if the correct answer has a higher probability, decoding can still take another path. For numeric tasks, this randomness can amplify bias.
3. Boundaries of Architecture and Capability
Some LLMs use a mixture-of-experts (MoE) architecture, in which multiple subnetworks work with a routing mechanism to perform computation. This architecture can improve model capacity at a certain scale, but it does not automatically guarantee consistency in numeric comparison. Routing choices, training data coverage, and fine-tuning strategies all affect the model’s performance on specific tasks.
Therefore, capability changes brought by architecture upgrades need to be verified with the same evaluation set, rather than relying only on a single case or promotional description.
4. Engineering Mitigations
4.1 Delegate Deterministic Computation to Tools
For tasks such as comparison, summation, and unit conversion, the most reliable approach is to have the model call deterministic tools, such as a code execution environment or a calculation function. The model is responsible for understanding the problem and generating parameters, while the tool provides the result.
from decimal import Decimaldef compare_decimal(a: str, b: str) -> str:"""Compare two string-form numbers as decimal values."""x, y = Decimal(a), Decimal(b)if x > y:return f"{a} > {b}"if x < y:return f"{a} < {b}"return f"{a} = {b}"print(compare_decimal("9.9", "9.11")) # Output: 9.9 > 9.11
Using Decimal instead of floating point avoids precision issues caused by binary floating-point representation.
4.2 Make Semantics Explicit in the Prompt
Specifying the type of number in the input can reduce ambiguity. For example:
- “The following numbers are decimal fractions. Compare them by numeric value.”
- “The following are software version numbers. Compare them segment by segment by major version, minor version, and revision number.”
4.3 Structurally Validate Outputs
Require the model to return results in a fixed structure, such as JSON fields, and then have a program check whether the fields comply with the rules. If validation fails, the system can retry, fall back to tool-based calculation, or hand the case over for manual handling.
4.4 Build a Numeric Evaluation Set
The evaluation set should cover the following types:
- Decimals with different numbers of digits, such as 3.14 and 3.5;
- Scenarios that mix version numbers and decimals;
- Negative numbers, zero, and scientific notation;
- Values with units and currency symbols.
cases = [("9.9", "9.11", ">"),("3.14", "3.5", "<"),("0.10", "0.1", "="),]def run_eval(model_fn):passed = 0for a, b, expected in cases:answer = model_fn(a, b)if expected in answer:passed += 1return passed / len(cases)
Evaluation results should be reported by question type so that weak areas can be identified.
5. Pre-Launch Checklist
- Define the semantic rules for each type of numeric task and document them.
- Use deterministic tools wherever precise computation is involved.
- Set structured output formats and programmatic validation.
- Rerun the numeric evaluation set after each model or prompt change.
- Record failure samples and continuously add evaluation cases.
Numeric errors may be a minor issue in everyday conversation, but in finance, statistics, version management, and similar scenarios they directly affect results. Combining semantic constraints, tool-based computation, and automated evaluation allows a system to use the model’s language capabilities within a controlled scope.
