Floating Point & Decimal Logic
Relevant source files
The following files were used as context for generating this wiki page:
The Floating Point & Decimal Logic module manages the numerical precision, formatting, and arithmetic consistency of the application. It ensures that user inputs, such as decimal points and large digits, are handled within specific constraints while maintaining accuracy during arithmetic operations using IEEE 754 floating-point standards mediated by JavaScript’s Number type.
This system is responsible for mitigating common floating-point errors (e.g., binary representation artifacts), enforcing digit limits for the user interface, and formatting results for human readability.
Precision Control and Rounding
The calculator implements a strict precision policy to prevent long floating-point tails that occur during binary arithmetic. The core of this logic resides in a dedicated rounding function that limits results to 12 significant digits.
Rounding Mechanism
The round(n) function utilizes toPrecision(12) to standardize numerical output. This approach ensures that operations like 0.1 + 0.2 do not result in 0.30000000000000004 on the display.
The diagram above illustrates the pipeline for ensuring numerical precision after any arithmetic operation.
Decimal Input Handling
The logic for entering decimal values is designed to prevent invalid numerical formats, such as multiple decimal points in a single operand.
Input Constraints
- State Check: If the system is in a
shouldResetstate (after an operation), the decimal button initializes a new value of0.. - Existence Check: The system checks if
currentalready contains a period. If it does, additional decimal inputs are ignored.
| Logic Step | Action | Condition |
|---|---|---|
| Reset State | current = '0.' | shouldReset === true |
| Validation | Append . | !current.includes('.') |
| Prevention | No Action | current.includes('.') |
Sources: calculator.html:408-412
Numerical Formatting and Display
To maintain a clean user interface, the system formats raw floating-point numbers into localized strings and handles scientific notation for extreme values.
Formatting Rules
- Large/Small Numbers: Numbers with an absolute value $\ge 10^{12}$ or $< 10^{-6}$ (excluding zero) are converted to exponential notation with 4 decimal places.
- Localization: Integers and the integer part of decimals are formatted with thousands separators using
toLocaleString(). - Digit Limits: User input is capped at 14 characters to prevent display overflow.
This flow defines how internal state values are transformed into display-ready strings.
Arithmetic Operation Logic
Arithmetic is processed through the compute(a, b, op) function, which maps UI operators to mathematical logic.
Supported Operations
- Addition (+): Standard floating-point sum followed by a
round(). - Subtraction (−): Standard floating-point difference followed by a
round(). - Multiplication (×): Standard floating-point product followed by a
round(). - Division (÷): Includes a safety check for division by zero, returning "Error" if the divisor is
0.
function compute(a, b, op) {
switch (op) {
case '+': return round(a + b);
case '−': return round(a - b);
case '×': return round(a * b);
case '÷': return b === 0 ? 'Error' : round(a / b);
default: return b;
}
}
Sources: calculator.html:428-435
Functional Transformations
Beyond basic arithmetic, the logic supports unary transformations that modify the current floating-point value.
| Function | Logic | Implementation |
|---|---|---|
| Toggle Sign | $n \times -1$ | parseFloat(current) * -1 |
| Percentage | $n / 100$ | parseFloat(current) / 100 |
Sources: calculator.html:458-466
Summary
The Floating Point & Decimal Logic provides a robust layer of abstraction over native JavaScript math. By enforcing a 12-digit precision limit and a 14-digit input limit, it balances mathematical accuracy with UI constraints. The system effectively manages state transitions between input, operation, and result display while protecting against common errors like division by zero and decimal duplication.