The arithmetic underneath

Every equal tail has a cliff

A scaled binary32 norm of one entry of size one and 4,095 entries of two to the minus twelve is wrong by 2,047 units of roundoff, and that was read as a depth: a square half a spacing below the running sum is lost. It is not a depth. An equal tail at 0.003, eight bits shallower, loses 1,965 units, and 88 per cent of all depths between four and twelve lose more than a hundred. The error is one addition's rounding repeated once per entry, so a test that counts small entries misses nearly all of it, and a spread tail does not average it away. Adding the same squares pairwise, with the same number of additions, leaves under five units on everything measured.

Worth reading first: A norm that overflows before it is a norm · The order they are added in · The same program, twice.

The division the norm did not need ended on a vector that was built to be awkward. One entry of size one, then 4,095 entries of 2−122^{-12}, in binary32, with every route protected against the overflow that breaks a norm before it is a norm. Each of them returned exactly 1, where the truth is 1.000122, and that is an error of 2,047 units of the format’s roundoff. The explanation was arithmetic that could be done by hand: binary32’s spacing just above one is 2−232^{-23}, the square of 2−122^{-12} is 2−242^{-24}, exactly half a spacing, and round-to-even returns the running sum unchanged. Every square vanished on arrival.

That essay read the result as a depth. The squares are lost because the entries sit at half the significand’s depth below the largest, and on a sweep over integer depths the error was a single cliff at k=12k = 12, falling by four a step on the deeper side and absent on the shallower. Two predictions with a sign followed from that reading. A tail drawn log-uniformly over a factor of four around the depth would keep between 500 and 1,500 units, and over a factor of a hundred would fall below 100. And a routine that counted the entries below half the depth on its first pass, which it already makes to find the largest, could compensate only when more than eight were found and keep the worst error under eight units everywhere.

Both predictions are wrong, in instructive directions, and for one reason. The cliff was never a depth.

The same sweep, at a fiftieth of a step

The earlier sweep took kk at whole numbers, and a whole-number kk is the most special depth there is. The entry 2−k2^{-k} has a one-bit significand, its square is a power of two, and a power of two above 2−242^{-24} is added to a sum near one exactly. That is why depths 4 to 11 showed nothing. Taking kk in steps of a fiftieth instead gives entries with full significands.

The error of a binary32 norm of one entry of size one and 4,095 equal entries of two to the minus k, against k in steps of a fiftieth, with the squares added in order and added pairwiseAdded in order, the error reaches 2047 units of the roundoff, and 88 per cent of the depths between 4 and 12 lose more than 102 units; at the integer depths 4 to 11 it is under one. Added pairwise, the worst at any depth is 2.0 units.4681012141610⁻²10⁻¹110¹10²10³10⁴k, the tail's entries are two to the minus kerror in units of the roundoffsquares added in ordersquares added pairwisebinary32 · length 4,096 · floored at a hundredtha cliff at almost every depth
Fig. 1 The error of a binary32 norm of one and an equal tail, against the tail’s depth in steps of a fiftieth, with the squares added in order (red) and added pairwise (blue). The dial sets the tail’s length.

At 4,095 entries the red curve is a forest. The cliff at twelve is still there, at 2,047 units, and it is the tallest tree by a little. Next to it, at k=11.9k = 11.9, the error is 1,742 units in the other direction: the norm comes out too large. At 0.003, which is about 2−8.42^{-8.4} and so eight bits shallower than the cliff, it is 1,965. At 0.01 it is 405. Of the 400 depths from 4 up to 12, 88 per cent have an error above 102 units, a fortieth of the length, and a third of them are above a thousand. The whole-number depths are the exceptions, each a narrow notch in the forest, and the earlier sweep happened to sample nothing but notches.

The dial moves the length, and the picture scales without changing shape. At 255 entries the worst is 127 units; at 1,023 it is 508; at 4,095 it is 2,047. The forest is the same forest, with every tree grown in proportion to the number of entries.

The blue curve is the same scaled norm with the squares added as a tree rather than a line: pairs, then pairs of pairs, until one number remains. It uses the same n−1n - 1 additions, the same scaling and the same square root, and its worst error at any depth and any length on the dial is 2.0 units.

One rounding, repeated

A sum of a large term and many equal small ones behaves in a way that is easy to state once seen. The running sum starts at one and stays between one and two, where the spacing is a fixed h=2−23h = 2^{-23}. Each square ss arrives and the result is rounded to the grid, so it moves by ss rounded to a multiple of hh. The rounding error of one addition is δ=round(s/h) h−s\delta = \mathrm{round}(s/h)\,h - s, and because every addend is the same and the grid is the same, so is δ\delta. After nn additions the squared norm is off by nδn\delta exactly, and the norm, being its square root, by half that relative to its size. That is a prediction with no free parameter in it: compute one addition in binary32, multiply by the length.

The measured error of a binary32 norm of one and 1,023 equal entries, between depths seven and nine, against the error predicted from one addition's rounding multiplied by the number of entriesOver 94 depths between seven and nine with a predicted error above one unit, the measured and predicted errors differ by at most 1.0 units, on errors up to 505.77.588.5910⁻¹110¹10²10³k, the tail's entries are two to the minus kerror in units of the roundoffpredicted: n times one roundingmeasuredbinary32 · length 1,024 · every second depth dottedone rounding, the same every time
Fig. 2 The measured error of a binary32 norm of one and 1,023 equal entries, between depths seven and nine, against one addition’s rounding multiplied by the number of entries (grey), with every second measured depth dotted (red).

Between depths seven and nine at 1,023 entries the two agree to within one unit on every depth with a predicted error above one, and across the whole sweep at all three lengths they agree to within one unit and one per cent. The tree heights are therefore not a statistic. They are where the fraction s/hs/h happens to land: a square that is a whole number of spacings is added exactly and leaves a notch, and one that is a whole number and a half is rounded by half a spacing, the most rounding allows, in a direction fixed by round-to-even and the sum’s last bit. Between those, the error moves through every value in proportion to how far the fraction is from a whole number.

The cliff at twelve is not a different phenomenon. It is the particular case in which s/hs/h is exactly one half and the sum is even, so round-to-even returns the sum and δ=−s\delta = -s: the square is lost entirely rather than rounded. That is a striking way to lose it, and the first essay drew attention to it rightly, but in units it is no worse than its neighbours. At 0.003 the square is 75.5 spacings, the rounding is again half a spacing, and the 4,095 roundings add up to as much as 4,095 lost squares did, from entries that are a three-hundredth of the largest and nowhere near any threshold.

This is the shape a repeated rotation drifts in a straight line found for a product of one rotation with itself: the same factor’s rounding defect, applied a million times, accumulated linearly at 622,127 units where fresh factors walked like a square root. An equal tail is the additive version. A random sequence of roundings partly cancels and grows like n\sqrt{n}, which is what three walks and one bound found measured sums actually doing against a bound of nn; one rounding repeated cannot cancel against itself and grows like nn. The direction the error leans is the general setting, where a rounding mode with a bias drifts in proportion to the number of operations, and an equal tail is round-to-nearest given one input and so one bias.

The count reads the one depth it was told about

The test predicted to catch the cliff counts the entries at or below 2−122^{-12} times the largest and compensates when it finds more than eight. On the tail sweep it does exactly what it was designed to do, and that turns out to be very little.

The error of the binary32 norm that counts the entries below two to the minus twelve of the largest and compensates when more than eight are found, against the depth of an equal tail of 4,095, beside the pairwise sumThe count fires at 201 of 601 depths, all of them deeper than twelve, where the compensated error is at most 1.50 units. At the 400 it does not fire on, the error is as before, up to 2031 units. The pairwise sum's worst is 2.0.4681012141610⁻²10⁻¹110¹10²10³10⁴k, the tail's entries are two to the minus kerror in units of the roundoffcount, then compensatesquares added pairwisebinary32 · length 4,096 · the count's threshold markedit reads the one depth it was told about
Fig. 3 The error of the norm that counts small entries and compensates when it finds more than eight (red), against the depth of an equal tail of 4,095, beside the pairwise sum (blue). The count’s threshold is marked.

It fires at 201 of the 601 depths, every one of them at or deeper than twelve, and there the compensated sum’s error is at most 1.5 units. At the other 400 depths it has nothing to count, and the error is the uncompensated one, up to 2,031 units at k=10.84k = 10.84. The test’s premise is that a small entry is dangerous because of its depth, and so it reads depth. The danger is in the repetition, which a depth cannot see: every entry of the tail at 0.003 is perfectly ordinary in size, and only the fact that all 4,095 of them round the same way makes them a problem.

A test that did read for repetition is easy to imagine and hard to want. It would have to notice that the squares fall on the same fraction of a spacing, which is a question about the whole vector’s values rather than about any one entry. The alternative is to remove the condition rather than detect it, and the pairwise tree does that for free.

Why the tree does not care

Pairwise addition adds each square first to another square of its own size. Two copies of ss make 2s2s exactly, since doubling is exact in binary floating point; four make 4s4s; and the tail sums itself to 4,095s4{,}095s with almost no rounding, before ever meeting the large entry. The large entry and the tail’s total then meet once, in one addition with one rounding, and that single rounding is the half unit the result carries.

The line, in contrast, meets the large entry with every square. Each addition puts a small term against a large one, which is exactly the arrangement in which rounding is largest relative to what is added. The order they are added in made this argument for sums in general, and its bound for pairwise addition grows with log⁡2n\log_2 n rather than with nn. What an equal tail adds to the general argument is that the line’s nn is not a worst case approached by unlucky data but an exact value attained by the most regular data there is.

The error of a binary32 norm of one entry of size one and n equal entries, against n, at three values of the entries, added in order, and the worst of the three added pairwiseentries of two to the minus 12: 7 at 15, 31 at 63, 127 at 255, 511 at 1023, 2047 at 4095, 8186 at 16383; entries of 0.003: 8 at 15, 32 at 63, 127 at 255, 505 at 1023, 1965 at 4095, 7104 at 16383; entries of 0.01: 2 at 15, 9 at 63, 35 at 255, 129 at 1023, 405 at 4095, 1556 at 16383. Pairwise, the worst of the three at any length is 1.5 units.10¹10²10³10⁴10⁻¹110¹10²10³10⁴n, the number of equal entrieserror in units of the roundoffentries of two to the minus 12entries of 0.003entries of 0.01pairwise, worst of the threebinary32 · slope one in orderthe tree's error does not grow with n
Fig. 4 The error of a binary32 norm of one and nn equal entries, against nn, at three entry values, added in order, with the worst of the three added pairwise (blue).

Against the length the three lines added in order are straight with slope one, through four decades: entries of 2−122^{-12} lose 7 units at 15 entries and 8,186 at 16,383, entries of 0.003 lose 8 and 7,104, entries of 0.01 lose 2 and 1,556. The pairwise curve does not rise at all; its worst over the three values and six lengths is under two units, which is less than the log⁡2n\log_2 n the bound would allow. At 16,383 entries of 0.003 the line’s error is 7,104 units, a relative error of 4⋅10−44 \cdot 10^{-4} in a format whose roundoff is 6⋅10−86 \cdot 10^{-8}, and the tree’s is 0.07.

The two repairs in the earlier essay fare differently on the forest. Kahan’s compensation keeps an equal tail’s error under 1.5 units at every one of the 601 depths, since it carries each addition’s rounding forward into the next and so cannot let δ\delta accumulate. Sorting the squares smallest first, which removed the cliff at twelve just as well, does not: its worst over the same depths is 383 units. A sorted equal tail is still summed in a line, against its own growing partial sum instead of against the large entry, and a line of equal terms repeats its roundings in runs for as long as the partial sum stays in one binade. The tree is the arrangement in which no addend ever meets a sum much larger than itself. Compensation costs four additions an entry in place of one; the tree costs a stack of log⁡2n\log_2 n partial sums, or in a blocked implementation almost nothing, since summing blocks and then summing the block totals is already pairwise at one level.

Spreading the tail does not average it

The second prediction concerned a tail that is not exactly equal: entries drawn log-uniformly over a factor FF around 2−122^{-12}. It said a factor of four would keep 500 to 1,500 units of the cliff and a factor of a hundred would leave under 100. If the cliff were a depth, those numbers would follow from the share of the tail still near it. Since the cliff is a repeated rounding, the question is what happens to the repetition when the entries are allowed to differ.

The error of a binary32 norm of one entry of size one and 4,095 entries drawn log-uniformly over a factor F around two to the minus twelve, against F: median and worst of forty draws in order, and worst pairwisefactor 1: median 2047, worst 2047, pairwise worst 1.4; factor 1.1: median 24, worst 95, pairwise worst 3.2; factor 1.25: median 20, worst 67, pairwise worst 2.7; factor 1.5: median 54, worst 121, pairwise worst 3.6; factor 2: median 172, worst 212, pairwise worst 3.2; factor 4: median 288, worst 340, pairwise worst 2.0; factor 10: median 255, worst 297, pairwise worst 3.1; factor 100: median 153, worst 185, pairwise worst 3.2; factor 1000: median 102, worst 130, pairwise worst 4.0, in units of the roundoff.110¹10²10³10⁻¹110¹10²10³10⁴F, the factor the tail is spread overerror in units of the roundoffin order, worstin order, medianpairwise, worstbinary32 · forty draws a factordown by a hundred, then up again
Fig. 5 The error of a binary32 norm of one and 4,095 entries spread log-uniformly over a factor around two to the minus twelve, against the factor: worst (red) and median (green) of forty draws added in order, and the worst added pairwise (blue).

The first tenth of spread destroys it. At F=1.1F = 1.1 the median error falls from 2,047 to 24 units and the worst of forty draws is 95, because the squares now land at every fraction of a spacing and their roundings point both ways. Then, as the spread widens, the error comes back: 54 units at 1.5, 172 at 2, 288 at 4, and then slowly down again, 255 at 10, 153 at 100, 102 at a thousand. Neither predicted number holds. A factor of four keeps 288 units, well under the predicted lower limit of 500, and a factor of a hundred keeps 153, half again above the predicted ceiling.

The reason the curve rises is that a spread around the critical depth always puts half the tail below it, and below it the squares are not rounded but lost. In one draw at F=1.1F = 1.1 the lost squares account for 964 units of error downwards, and the squares just above the depth, rounded up to a whole spacing, for 986 upwards. The two halves cancel to within the noise, because a narrow spread gives them the same size. At F=4F = 4 the lost half sits further below and the half above is no longer rounded up as much: 547 units down, 277 up, and a net of 270. At F=100F = 100 the losses are smaller, since the lost squares are smaller, and so are the gains: 218 down and 72 up. The error a spread tail keeps is the imbalance between what it loses entirely below the depth and what it gains just above it, and that imbalance has a maximum near a factor of four.

So the depth matters after all, in the one way the earlier reading did not predict. It is not where the error is; it is the place where rounding to nearest changes from rounding into rounding away, and a spread that straddles it turns an unbiased rounding into a biased one. Pairwise addition removes that too. Its worst over all nine spreads and forty draws is 4.0 units, at a factor of a thousand.

Where real data repeats

None of this would matter much if equal tails were rare, and in floating point that is assumed rather than known. Data from a sensor, an image or an audio file is quantised before it is ever a float: its values are integers times a step. A unit impulse with a little noise, which the earlier essay offered as an ordinary vector to meet the cliff on, is the natural place to look.

The error of a binary32 norm of a unit impulse and 4,095 samples of noise at a hundredth, quantised to six steps: median and worst of thirty draws added in order, and the worst added pairwisestep of two to the minus 7, a median of 6 distinct magnitudes: in order median 0.4 and worst 0.7, pairwise worst 0.7; step of two to the minus 10, a median of 37 distinct magnitudes: in order median 0.5 and worst 0.8, pairwise worst 0.8; step of two to the minus 13, a median of 238 distinct magnitudes: in order median 298.1 and worst 327.6, pairwise worst 2.4; step of two to the minus 16, a median of 1309 distinct magnitudes: in order median 30.1 and worst 63.6, pairwise worst 2.0; step of two to the minus 19, a median of 3330 distinct magnitudes: in order median 9.1 and worst 40.0, pairwise worst 2.2; step of two to the minus 22, a median of 3989 distinct magnitudes: in order median 10.6 and worst 36.2, pairwise worst 1.5, in units of the roundoff.0.11101001000added in order: median, with the range to the worststep two to the minus 7, 6 values0 to 1step two to the minus 10, 37 values1 to 1step two to the minus 13, 238 values298 to 328step two to the minus 16, 1,309 values30 to 64step two to the minus 19, 3,330 values9 to 40step two to the minus 22, 3,989 values11 to 36added pairwise: worststep two to the minus 7, 6 values0.7step two to the minus 10, 37 values0.8step two to the minus 13, 238 values2.4step two to the minus 16, 1,309 values2.0step two to the minus 19, 3,330 values2.2step two to the minus 22, 3,989 values1.5error in units of the roundoffbinary32 · thirty draws a stepthe middle steps repeat values
Fig. 6 The error of a binary32 norm of a unit impulse and 4,095 samples of noise at a hundredth, quantised to six steps: the median and the range to the worst of thirty draws added in order (red), and the worst added pairwise (blue).

The answer has three regimes, and the middle one is the trouble. At a coarse step of 2−72^{-7} the noise takes six distinct magnitudes and every square is a short binary fraction that the sum holds exactly, so the error is under a unit. At a fine step of 2−192^{-19} or below the samples take three to four thousand distinct values and their roundings are as good as random, with medians of nine to eleven units. Between them, at a step of 2−132^{-13}, the noise takes 238 distinct magnitudes, each repeated about seventeen times, and the median error is 298 units, with a worst of 328. That is a vector with nothing special in its range or its sizes, quantised the way thirteen-bit data is, losing about as much as the spread tail at its worst.

Added pairwise, every one of the six steps is under 2.4 units.

What this does not settle

The measurements are of one norm in simulated binary32, with the reference computed in binary64, on vectors constructed to isolate one effect and then on one family of quantised noise. The predicted law was checked only where the running sum stays in its first binade; a tail large enough to push the sum past two, such as 16,383 entries of 0.01, changes the spacing midway, and the law then needs two pieces, which were not derived. Fp16 shows the same forest at a smaller scale, a worst of 375 units on 1,023 entries, and was not taken further.

The pairwise tree is measured as a plain recursive pairing. Real reductions are usually blocked, summing a block of some dozens in order and then the block totals; a block of 64 equal squares added in order would carry 64 of the repeated roundings rather than 4,095, and the error would be bounded by the block length rather than the vector’s. That is a smaller cliff rather than none, and how blocked kernels in practice compare was not measured. Where the disagreement comes from traced run-to-run variation to exactly that blocking, a rounding committed at each partial sum’s own scale, and an equal tail is the input on which a block’s length would show in the answer. Nor was speed: the tree’s additions are the same in number but not in dependency, and on most hardware a reduction with independent partial sums is faster rather than slower, which is why libraries already use one.

What is settled is narrower. A norm’s large error on these vectors is the number of entries times one addition’s rounding, to a unit and a per cent; it is attained by any equal tail at almost any depth and by quantised data with moderate resolution; it is invisible to a test that looks at entry sizes; and the order of the same additions removes it. The vector that hides it made the point that a sum’s error hides from tests that look at the answer. Here it hides from a test that looks at the inputs, as long as the test asks about size.

Still open: the blocked reduction, and stochastic rounding against repetition

A block of equal squares. A blocked norm adds 32 or 64 squares in order and then sums the block totals. The prediction with a sign is that on an equal tail its worst error over all depths is the block length times half a unit, within ten per cent, at every vector length from 1,024 to 16,384, so that a blocked reduction caps the forest at the block’s height rather than removing it, and that a block of eight is already under the five units pairwise reaches. If so, the block length is the one number a library’s documentation should state for its norm.

A coin flip against one bias. A coin flip that fixes the average showed stochastic rounding removing the drift of a biased sum. An equal tail is the extreme case of a bias, the same rounding every time, and stochastic rounding turns it into a random walk. The prediction is that a scaled norm with stochastic rounding of the additions has an error of about n/2\sqrt{n}/2 units on every equal tail, 32 at 4,095 entries, at every depth, so that it removes the forest’s dependence on depth entirely but leaves an error sixteen times larger than the pairwise tree’s at that length, and that the two together, a tree with stochastic rounding, do no better than the tree alone.

What links here

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

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.

Binary32Compensated summationPairwise summationQuantisationScaled normSummationUnit roundoff