Terminology (7)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- binary64
- The 64-bit IEEE 754 format: one sign bit, 11 exponent bits and 52 fraction bits. Normal values have 53 significant binary digits.
- Representation error
- The difference between a decimal you write and the nearest binary64 value actually stored.
- Rounding error
- The change introduced when an exact arithmetic result is rounded to a binary64 value, for the separately rounded operations discussed here.
- Shortest round-trip printing
- Printing the fewest decimal digits that read back as the same stored value.
- Tolerance
- An allowed difference for treating two values as equal, chosen for their units and size.
- NaN
- “Not a number”: the result of an undefined operation. It compares unequal to every value, itself included.
- Infinity
- A signed nonfinite value beyond finite range; the examples obtain it through the language’s named infinity API.
Precision, range and displayed digits
Floating-point represents numbers using a finite significant part scaled by an exponent. Binary64 is the format used for the runtime values in these examples. Its finite precision leaves gaps between representable values, so storing a decimal or performing arithmetic can require rounding.
Formatting chooses a decimal description of a stored value; it does not increase that value’s precision. An equality decision and a rounding policy must therefore be judged against the actual representation. Finite amounts, infinities and NaN also require different handling when a calculation feeds a real decision.
01Representation
Most decimal fractions are stored approximately
A receipt tool must print a total, decide whether the customer paid the right amount and apply a percentage discount with a stated cent-rounding rule. A displayed 0.30 can conceal a value that fails the payment comparison, and a decimal half-cent can move below the boundary before rounding. Track the stored value through each receipt decision.
Before totaling prices, inspect what parsing a decimal can retain. Normal binary64 values have 53 significant binary digits, scaled by a power of two; precision and exponent range both limit exact representation. The shown literals use the nearest representable values under the default rounding profile.
The program displays the stored values of 0.1 and 0.375 rounded to twenty decimal places. Which original decimal was stored exactly?
console.log((0.1).toFixed(20));
console.log((0.375).toFixed(20));
npx tsc --noEmit --strict main.ts && node main.ts
Use the selected language’s setup from the first reading and replace the file for each experiment. The examples use binary64 number, float64 and f64; the C++ track target uses binary64 double. Predictions describe the shown separate operations under the track’s default rounding and formatting profile.
02Arithmetic
The shown binary64 additions round before comparison
The receipt’s 0.10 and 0.20 prices have already been approximated. In the shown separate binary64 addition, their exact sum is rounded to a representable value. The program compares 0.1 + 0.2 with 0.3, first with literals and then with variables, and prints the sum with the default formatting. Predict all three lines.
console.log prints the shortest decimal that reads back as the same number.
console.log(0.1 + 0.2 === 0.3);
const a = 0.1;
const b = 0.2;
console.log(a + b === 0.3);
console.log(a + b);
03NaN and infinity
Special values break the usual comparison rules
Payment classification assumes finite inputs. This reduced preflight probe explains why callers need that acceptance condition: binary64 also has infinities and NaN, “not a number”, which can arise from calculations such as infinity minus infinity. Can the ordinary comparison branches classify these special values as usable receipt amounts?
Predict the first line: whether NaN equals itself, and whether NaN is either below 1 or at least 1.
Number.NaN and Number.POSITIVE_INFINITY name the special values.
const missing = Number.NaN;
const huge = Number.POSITIVE_INFINITY;
console.log(missing === missing, missing < 1 || missing >= 1);
console.log(huge, -huge);
console.log(Number.isNaN(huge - huge));
04Rounding and display
Rounding works on the stored value, not the decimal you typed
A half-price discount on 2.01 has an intended decimal result of 1.005, exactly at a half-cent. This reduced probe assigns 1.005 directly, rounds it to cents in two ways and prints the stored value in between. Predict the first line.
toFixed(digits) rounds to that many decimals and returns text.
const price = 1.005;
console.log(price.toFixed(2));
console.log(price.toFixed(20));
console.log(Math.round(price * 100) / 100);
05Repair the receipt
Print, compare, then round to the cent
Repair the receipt’s printed amounts, payment verdict and percentage discount. Display precision alone cannot settle a cent comparison; the half-up discount must also preserve the intended cent value before its percentage calculation.
This terminal supports finite nonnegative amounts with exactly two decimal digits. Each receipt has at most 100 prices; prices, the total and the amount paid are at most 99999.99. Discounts use whole percentages from 0 through 100. The lab receives data within those bounds; input validation is outside these repairs.
The lab opens in your reading language and has its own language control. Each file contains its imports, reader and complete main. Later levels supply the earlier correct helpers.
Repair the receipt
Display, comparison and exact decimal rounding, each on amounts read as binary floating point.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?