The arithmetic underneath

The direction the error leans

The size of one rounding error is set by the precision. How ten thousand of them combine is set by something else entirely — the rounding mode — and the fitted exponents are 0.47 for round-to-nearest and 1.01 for round-toward-infinity, on identical data at identical precision.

Worth reading first: What a float can hold · The order they are added in.

Every figure on this site so far takes the same step and none of them shows it. A computed value falls between two representable numbers; one of them is returned. Which one?

The answer this site has assumed for four phases is the nearer, with ties to even, which is what IEEE 754 does by default and what roundBits did unconditionally. It is one of four answers the standard defines, and there is a fifth in silicon on several current accelerators that the standard does not define at all.

The choice barely changes the size of a single error — every mode is within a factor of two of every other on one operation. What it changes is how the errors of many operations combine, and that is a difference of a whole power of n.

Relative error of a sum against the number of terms, at 24 significand bitsFive curves of relative error against the number of terms, both axes logarithmic. Three rise with unit slope; two rise with about half that.10²10³10⁴10⁻⁸10⁻⁷10⁻⁶10⁻⁵10⁻⁴terms summedrelative error+∞, −∞ 1.01zero 1.00stochastic 0.50nearest 0.47√n against n, fittednearest, fitted exponent0.47stochastic, fitted exponent0.5toward +∞, fitted exponent1twelve seeds averaged at each sizethe slope is the bias, not the precision
Fig. 1 Five modes, identical data, identical precision, and two families of curve. Two rise like the square root of the number of terms and three rise linearly. Drag the precision and every curve moves down together without changing slope — until fourteen bits, where the sum stagnates and the distinction disappears.

The five answers

mode what it returns
nearest round to the nearer, ties to even. The default everywhere.
zero truncate. What a cast to an integer does, and what a great deal of fixed-point hardware does when nobody chose.
up toward +∞.
down toward −∞.
stochastic round up with probability equal to the distance up. Not in IEEE 754; a hardware feature of several recent accelerators and a software trick before that.

The first four are functions of the value. The fifth is a distribution over the same two answers, with a mean equal to the value itself, and it is the next essay’s subject as much as this one’s.

Round-to-nearest and round-toward-zero are the two a programmer meets by accident. The first because it is the default; the second because a truncation is what a cast does, and because integer and fixed-point arithmetic truncate unless told otherwise.

Rounding 1.42 to 4 significand bits, five waysA number line of the representable values in one octave, with the exact value marked between two of them and a row for each rounding mode showing which neighbour it returns.11.131.251.381.51.631.751.882exact 1.42nearest1.375toward zero1.375toward +∞1.5toward −∞1.375stochastic36% upstochastic, measuredposition between neighbours0.36share of draws rounded up0.369 representable values between 1 and 2four functions and one distribution
Fig. 2 One value, four functions and a distribution, on the number line of what four significand bits can hold. The bar at the bottom is the stochastic mode’s split, measured over two thousand draws against the value’s position between its neighbours. Drag the value and watch the three deterministic rows jump between neighbours while the split moves continuously.

Bias, and why it decides the exponent

For round-to-nearest, an inexact result is as likely to be pushed up as down over any reasonable set of inputs, so the errors of n operations partly cancel. Their sum is a random walk and grows like √n.

For a directed mode, every single rounding goes the same way. Nothing cancels; the errors add. The sum grows like n.

That is the whole mechanism and the measurement is a fit. Sum n like-signed values at 24 significand bits, average over twelve seeds so that a random walk is being measured rather than one sample of one, and fit the exponent in error ∝ n^p across three decades of n:

mode fitted exponent
nearest 0.47
stochastic 0.50
toward zero 1.00
toward −∞ 1.00
toward +∞ 1.01

Two families, cleanly separated, on data that differs in nothing. At n = 2,000 the gap between round-to-nearest and round-toward-infinity is a factor of 172.

The data is like-signed on purpose. A sum of values that change sign lets the values cancel, which hides what the errors are doing; the question here is only whether the rounding errors cancel, so the data is arranged not to.

Truncation is biased towards zero, not downwards

Round-toward-zero deserves separating from the other two directed modes, because its bias has a different shape and it is the one most likely to arrive uninvited.

Rounding up is biased upwards on the number line. Rounding down is biased downwards. Truncation is biased towards zero, which means it is biased downwards for positive values and upwards for negative ones — so on a sum of like-signed terms it is a systematic underestimate of the magnitude, and on a sum of mixed signs its errors partly cancel again for a reason that has nothing to do with being unbiased.

Measured at n = 2,000 the signed relative errors are: rounding up +3.98·10⁻⁵, rounding down −4.16·10⁻⁵, truncation −4.16·10⁻⁵ on this all-positive data — identical to rounding down, as it must be — against round-to-nearest’s 2.31·10⁻⁷.

The exponent is the same 1.00 either way. What differs is that the sign of a truncation’s bias depends on the data, so a programmer who checks one test case with positive inputs and finds an underestimate learns nothing about what negative inputs will do.

The one guarantee this site can offer

The directed pair is not only a hazard. Computed together they are the whole of interval arithmetic, and they give something no error estimate on this site can:

Round the entire computation up, and again round the entire computation down. The true answer lies between the two results. Not probably. Not usually.

Every intermediate value in the first run is at least the true one and every intermediate in the second is at most, so the ordering propagates. There is no tolerance in the statement, no constant, and no hypothesis about the data. It is the only claim of that kind anywhere on this site.

And it is loose. The bracket widens by roughly an ulp per operation, so its width grows like nu while the actual error grows like √n·u, and the gap between the guarantee and the thing guaranteed opens by √n. Measured at 24 bits:

terms bracket width actual error ratio
10 4.59·10⁻⁷ 6.72·10⁻¹¹ 6,830
100 4.41·10⁻⁶ 2.29·10⁻⁷ 19
1,000 4.02·10⁻⁵ 4.53·10⁻⁷ 89
10,000 4.42·10⁻⁴ 2.43·10⁻⁶ 182

The containment never fails — that is asserted at every size — and by ten thousand terms the interval is 182 times wider than the error it is bounding. That is the shape of the trade: an interval method is true at every size and useful only while the width stays below what the application can accept, and the width grows faster than the error does.

The remedy is not more precision. Both quantities scale with the unit roundoff, so buying bits moves both curves down together and does not close the gap between them — which the figure’s slider shows directly.

The width of a guaranteed bracket, and the error it guarantees, at 24 bitsTwo rising curves on logarithmic axes. The upper one, the bracket width, rises faster than the lower one, the actual error.10¹10²10³10⁴10⁻¹¹10⁻¹⁰10⁻⁹10⁻⁸10⁻⁷10⁻⁶10⁻⁵10⁻⁴10⁻³terms summedrelative widthbracket widthactual errora bound with no tolerance in itbracket contains the truth7width at 10,000 terms4.4·10⁻⁴error it bounds2.4·10⁻⁶every sum computed twice, up and downtrue at every size, useful at none of the large ones
Fig. 3 The guarantee and the error it guarantees, on the same axes. Both rise, the upper one faster, and the slider moves them down together without changing the distance between them.

What one rounding costs, and why it is the wrong question

It is worth being precise about the quantity the modes do share, because the essay’s claim is a claim about the one they do not.

For a single operation, round-to-nearest has an error of at most half an ulp and every other mode here has an error of at most a whole ulp. So the worst-case error of one rounding differs by exactly a factor of two across the whole set, and every textbook bound on a computation — the nu of a dot product, the backward error of an elimination, the ‖A − QR‖/‖A‖ this site prints on every factorisation figure — inherits that factor of two and nothing else.

Those bounds are therefore almost mode-independent, and they are worst-case bounds: they assume every rounding goes the wrong way, which is exactly what a directed mode does and what round-to-nearest does not. So a directed mode attains the classical bound and round-to-nearest sits far below it, and the distance between them is the √n against n this essay measures.

That reframes the usual complaint that error bounds are pessimistic. The bounds are not pessimistic about arithmetic in general — they are tight for a computation that rounds in one direction, and loose for one that rounds to nearest, by a factor of √n. A reader who has been told that “in practice errors grow like √n rather than like n” has been told a fact about the rounding mode, and it is usually presented as a fact about floating point.

Changing the mode is not a local decision

The measurements above treat the rounding mode as a parameter, because in this site’s arithmetic it is one: arith(bits, { mode }) returns a context, every routine takes a context, and two contexts can coexist in the same program. That is a simulation choice and it is not what the hardware offers.

On a real processor the rounding mode lives in a control register. It is a property of the thread, not of the operation, so setting it changes the behaviour of everything running on that thread — including library code that was written and tested under the default. A library that computes a special function by a polynomial tuned for round-to-nearest, or that relies on a particular splitting identity being exact, may silently return something else when the register changes underneath it. Compilers add a second problem: an optimiser is entitled to reorder or constant-fold arithmetic unless told the rounding mode may vary, and under a directed mode the reordering is no longer harmless.

So interval arithmetic in production is not two runs with a register flipped between them. It is either a library that sets and restores the mode around each operation, at a substantial cost per operation, or one that emulates the directed results from round-to-nearest ones — which is arithmetic on top of arithmetic and slower again.

That is worth knowing before reading the guarantee in the previous section as a cheap one. The bracket costs at least twice the work and usually much more, and its width grows like n. The reason it is used anyway, where it is used, is that no other technique on this site can say the answer is in here without a hypothesis.

The mode nobody chose

There is one more reason to care, and it is the one this phase actually arrived through.

An eight-bit format is not usually the arithmetic. It is the storage, and the operation that matters is the quantisation: taking a value in double or single precision and choosing the nearest representable eight-bit one. That is a rounding, it happens once per stored value rather than once per arithmetic operation, and its mode is decided by whatever the quantisation routine does.

A routine that truncates — because it was written with a cast, or because the fixed-point hardware it targets truncates — introduces a bias towards zero in every stored value at once. The values of a tensor are then all slightly smaller in magnitude than they should be, systematically, and the sums computed from them inherit an error growing like n rather than √n. At 24 bits that is the factor of 172 measured above; at four significand bits the individual errors are 2⁻⁴ rather than 2⁻²⁴, and whatever the factor is, it multiplies something twenty decades larger.

Nothing about that is exotic and nothing warns. The quantised values are plausible, the norms are close to right, and the defect is a systematic shrinkage that a spot check on a few entries will not find. It is the same shape as the truncation bias above and it arrives through a different door — not from an arithmetic loop but from a conversion nobody thought of as arithmetic.

The block-format essay quantises 640 values at a time and rounds to nearest throughout, deliberately, so that the errors it measures are the format’s and not the rounding’s.

Where the distinction stops holding

The separation between the two families has a floor, and the floor is worth knowing because a reader who takes the exponents away as constants will apply them where they are false.

At fourteen significand bits and above, the fits are 0.5 and 1.0 as advertised. Below thirteen they collapse together. At eleven bits a sum of ten thousand terms of size one has a running total near ten thousand, whose ulp at that precision is 8 — so the addends stop arriving entirely, and what is being measured is no longer an accumulation of rounding errors but the size of what was never added.

Round-to-nearest fits at 1.09 there and rounding up at 1.49, both above the linear rate. The distinction this essay is about has not weakened; it has been replaced by a different failure, and the fitted exponent is reporting that failure rather than the one it was set up to measure.

That failure is stagnation, and it is where the fifth mode earns its keep.

What is asserted, and what refuses

Every mode does what its name says, on eight values spanning positive, negative and tiny: rounding up is never below the value, rounding down never above, the bracket is at most one ulp wide, and truncation agrees with rounding-down for positive values and with rounding-up for negative ones — four properties, each of which fails for at least one other mode, so the set distinguishes rather than merely accepting.

The exponents separate: below 0.8 for the two unbiased modes and above 0.85 for the three biased ones, at every precision from fourteen bits up.

And the collapse below thirteen bits is asserted too, in the other direction: round-to-nearest’s exponent must be above 0.9 there and rounding up’s above it again. A slider that quietly stopped checking at one end would be drawing the wrong claim there.

The bias has a direction, asserted by sign rather than by magnitude, on all three biased modes.

The bracket contains the truth at every size on the sweep, and is more than ten times wider than the error it bounds at the largest.

The claim that precision alone decides how an error accumulates is refused. Two fits at the same 24 bits, in two modes, are fed to assertClose inside rejects() — 0.47 against 1.01, and the assertion must fail.

And the bracket used backwards is refused. Rounding down to get an upper bound and up to get a lower one produces an interval containing nothing, and the assertion that it brackets the truth is fed to rejects().

Rounding 1.94 to 4 significand bits, five waysA number line of the representable values in one octave, with the exact value marked between two of them and a row for each rounding mode showing which neighbour it returns.11.131.251.381.51.631.751.882exact 1.94nearest2toward zero1.875toward +∞2toward −∞1.875stochastic52% upstochastic, measuredposition between neighbours0.52share of draws rounded up0.529 representable values between 1 and 2four functions and one distribution
Fig. 4 The same picture at a value close to the top of its interval, where round-to-nearest and the stochastic mode disagree most often. Four of the five rows land on the upper neighbour; the fifth lands there about nineteen times in twenty, and the twentieth is the whole of what the next essay is about.
Relative error of three summation algorithms in binary32A log–log plot of relative error against the number of terms for naive, pairwise and compensated summation, each measured against the exactly rounded sum.10¹10²10³10⁴10⁵10⁶10⁻⁹10⁻⁷10⁻⁵10⁻³10⁻¹number of terms addedrelative error against the exact sumin orderin a treecompensatedbinary32 · terms are 1/icompensated: 3·10⁻⁸
Fig. 5 The essay this one sits beside. Three summation algorithms at one precision, differing in the order the additions happen — where this essay holds the order fixed and changes what each addition does. Two independent knobs on the same computation, and the site had only been turning one of them.
One entry 64× the rest, at 6-bit block significandsTwo bar charts: how many of 320 entries were rounded to zero, and the median entry's relative error, for the block format in two orderings and for a per-element format.entries rounded to zero, of 320block, as given310block, sorted by size22E4M3, either order0median entry's relative errorblock, as given1block, sorted0.013E4M30.022the same numbers, three waysdeleted, as given310deleted, sorted22deleted, per-element06-bit significands, blocks of 32sorting is free and changes no value
Fig. 6 The quantisation this section is about, in the setting where it does the most damage. Three measurements of the same 320 numbers: stored in the order they arrive, sorted, and in a per-element format. Nothing here is an arithmetic loop — every one of these errors is a single rounding, applied once per value.

What is left

Stochastic rounding, which is the next essay: unbiased like round-to-nearest, and the only mode that escapes the stagnation this one ends on.

Interval arithmetic as a method. What is measured here is a two-run bracket on a sum. A real interval library propagates intervals through every operation, which is tighter in some places and looser in others, and is a subject rather than a comparison.

The rounding of a division or a square root, which this site’s simulated arithmetic performs by computing in double and rounding once — correct for addition and multiplication of narrow operands and not exactly right for the other two, as assertMatchesHardware measures rather than assumes. A directed mode makes that double-rounding gap slightly more visible, and nothing here quantifies it.

Round-half-away-from-zero, the fifth IEEE mode, which differs from round-to-nearest only on exact ties. Ties are a measure-zero event on random data and are common on the decimal fractions a person types, which is why the two modes disagree constantly in a spreadsheet and never in this essay’s experiments. Measuring it would need data chosen to land on ties, which is a different experiment rather than another column in this one.

What links here

Computed from the collection, not written here: the essays that point at this one.

Reads more easily once this is understood

Essays that name this one as worth reading first.

Shares its objects with

Essays that name at least two of the same things, and that neither author linked.

Named objects

A flat tag is an object no other essay names yet.

Directed roundingError accumulationIEEE 754Interval arithmeticRounding modesSummationTruncationUnit roundoff