The arithmetic underneath

The division the norm did not need

Dividing by the largest entry is the standard repair for a Euclidean norm that overflows, and it was said to make about the same rounding errors as the expression it replaces. It is about a third worse on short vectors, up to 1.74 times, and it misses 801 of 4,518 integer norms the plain expression returns exactly, because a power of two buys the same range with no rounding at all. But the scale was never the main liability. One entry of size one and 4,095 entries of two to the minus twelve lose 2,047 units of roundoff in every route alike, from a sum that throws each small square away on arrival.

Worth reading first: A norm that overflows before it is a norm · What a float can hold · The order they are added in.

A norm that overflows before it is a norm set two expressions for the Euclidean norm side by side. One is the square root of the sum of the squares. The other divides every entry by the largest first and multiplies the answer back at the end, and it returned a correct number at every normal exponent of three formats where the first managed barely over half. On the way past, it said what the repair costs: both expressions “make the same number of rounding errors to within a small constant”, and the second “costs one extra pass over the data and one division per entry”.

That sentence was an aside in a measurement of range, and nothing had measured it. Measured, it fails in two different ways, and the second has nothing to do with scaling at all.

The repair is more expensive than it needs to be, by a factor large enough to see, and the excess buys nothing, because the range can be had for an operation that does not round. And when the error of a norm is actually large — hundreds or thousands of units of roundoff, on a vector with no entry anywhere near an overflow — no scale of any kind has anything to do with it.

Five routes, one reference

The measurement takes five ways of computing one number in simulated binary32 and fp16. The first is the plain expression. The second is the division by the largest entry that a textbook hypot uses. The third finds the largest entry too but multiplies everything by the power of two nearest its reciprocal, and undoes that at the end. Multiplying by a power of two only changes an exponent, so inside the range it is exact: the third route rounds in exactly the places the first one does. The fourth is the incremental form of the classic nrm2, which keeps a running scale and a running sum and divides by whichever is larger at each entry, so it never needs a first pass. The fifth is Blue’s method of 1978: three accumulators, one each for entries whose squares would underflow, for entries whose squares are safe and for entries whose squares would overflow, the first and last scaled by constants that are powers of two fixed in advance for the format. It divides by nothing except at the very end, and it is what the reference BLAS moved to when Anderson’s reexamination of these routines was adopted.

The true value comes from binary64, which holds every entry of these formats exactly and each square and sum to more digits than either format can return. Errors are reported in units of the format’s roundoff, u=2−24u = 2^{-24} for binary32. Three kinds of vector are used: even entries between a half and one, one large entry above many a hundredth its size, and entries graded over two decades. Each carries a random sign and is rounded into the format first, so the input is exactly what the routine sees.

A third worse where the vector is short

The root mean square relative error of a binary32 Euclidean norm against the length of the vector, by the undivided route and by division by the largest entry, on even entriesOn even entries, in units of the roundoff: at length 2 the undivided route 0.54 and the divided 0.71, at length 64 1.10 and 1.18, at length 1024 3.90 and 4.25.10¹10²10³110¹length of the vectorerror in units of the roundoffdividing by the largestpower of two, or nonebinary32 · 1500 draws to 64, 400 abovethe gap closes as the sum takes over
Fig. 1 The root mean square error of a binary32 norm against the length of the vector, by the undivided route (blue) and by division by the largest entry (red). The dial sets the kind of vector.

On a pair of even entries the undivided routes have a root mean square error of 0.54 units and the divided one 0.71; the largest errors over 1,500 draws are 1.52 and 2.12. The plain expression, the power of two and Blue’s accumulators are the same curve to every digit printed at every length, as they should be: in this part of the range they perform identical roundings. So do the divided route and the incremental one, to within the small differences their different update orders make.

The reason is a count. Each square in the plain sum has one rounding, relative size at most uu, and in the norm that is halved by the square root. With a division in front of it, the entry being squared already carries one rounding from the division, which squaring doubles. And the whole sum is then multiplied back by the largest entry, a further rounding that sits outside the square root where nothing halves it. The undivided route’s final multiplication is by a power of two and costs nothing. A worst-case count on a pair therefore moves from about two units to about four, and the measurement is well inside both but moves the same way.

Worth noting what the dial does when turned. It moves both curves, because the shape of a vector moves how much of its rounding is concentrated in a few entries, and the gap between them survives every setting up to a length of a few dozen. Above that both curves climb together and the difference is hard to find.

The price is per entry, and the sum outgrows it

What dividing by the largest entry costs a binary32 norm, as its root mean square error over the undivided route's, against the length of the vector, on three kinds of vectoreven entries: 1.32 at 2, 1.27 at 8, 1.08 at 64, 1.09 at 1024; one large, the rest a hundredth: 1.74 at 2, 1.50 at 8, 1.41 at 64, 1.20 at 1024; graded over two decades: 1.45 at 2, 1.22 at 8, 1.03 at 64, 0.98 at 1024.10¹10²10³0.811.21.41.61.82length of the vectordivided error over undividedeven entriesone large, the rest a hundredthgraded over two decadesbinary32 · one is no price at alla per-entry cost under a growing sum
Fig. 2 The divided route’s root mean square error over the undivided route’s against the length, for three kinds of vector. A ratio of one would be no price at all.

As a ratio the cost is 1.32 at a length of two on even entries, 1.45 on graded ones and 1.74 on a vector with one large entry. By a length of 64 it is 1.08 and 1.03 on the first two, and at 1,024 it is around one. The third is the slowest to come down, 1.41 at 64 and 1.20 at 1,024, because a vector with a single dominant entry is the case where the multiplication back by that entry accounts for the largest part of what is left.

The shape is what the count predicts. The division’s cost is a fixed number of extra roundings per entry, averaged over the vector, plus one at the end. Everything else about a long vector’s error comes from adding: a running sum that has grown large rounds each new term against its own size, and that error climbs, on even entries, from 0.54 units at length two to 3.9 at length 1,024. Three walks and one bound measured that walk for a plain sum, and the climb here is the same kind of thing, near a square root of the length. So on a long vector the scale’s contribution disappears beneath the sum’s, and on a two-entry hypot, which is called more often than anything else in this family, it does not.

The answers it can no longer return

A factor of 1.3 could be dismissed. A root mean square is an average over inputs that nobody picks. The other measurement here is about inputs somebody does pick.

How many pairs of integers with an integer norm each route returns exactly: 4518 pairs in binary32 and 18 in fp16Binary32: square root of the sum 4518, divide by the largest 3717, power of two 4518, incremental scale and sum 3717, three accumulators 4518 of 4518. Fp16: square root of the sum 18, divide by the largest 17, power of two 18, incremental scale and sum 17, three accumulators 12 of 18.0255075100binary32, 4518 pairssquare root of the sum100.0%divide by the largest82.3%power of two100.0%incremental scale and sum82.3%three accumulators100.0%fp16, 18 pairssquare root of the sum100.0%divide by the largest94.4%power of two100.0%incremental scale and sum94.4%three accumulators66.7%per cent of the pairs returned exactlyevery square representable exactlya division cannot return what it rounded
Fig. 3 Of the pairs of integers whose norm is an integer and whose squares the format holds exactly, the share each route returns exactly, in binary32 and fp16.

There are 4,518 pairs of integers below 4,000 in which the norm is an integer and whose squares and sum binary32 can hold exactly. For those the plain expression is not merely accurate: every operation in it is exact, and the correctly rounded square root of a perfect square is its root, so the answer is exactly right. The power of two inherits that. The divided route returns 3,717 of them and misses the other 801, about one in six. The first pair it misses in binary32 is (13, 84), whose norm is 85 and which comes back as 85.00000763, one unit in the last place high. The pair (3, 4) survives because 3/43/4 is exact, and (5, 12) survives by luck, since 5/125/12 is rounded and the rounding is absorbed further on. Every one of the 801 misses is a single unit in the last place.

Nobody computes the norm of (13, 84) for its own sake. But a test does, and a person looking at a test does, and a routine with a property like “the norm of an integer vector with an integer norm is that integer” is easier to reason about than one without. The same point can be put as a general one: a route that performs a rounding in a place where the plain expression had none has turned exact cases inexact, and it takes no probability model to see them. What a float can hold makes a related observation about formats, that a value being representable is a property worth keeping.

What the range actually required

On how many of a format's normal exponents each route returns a norm correct to one per cent, for one vector of length eight scaled through them allBinary32, of 254: square root of the sum 135, divide by the largest 254, power of two 254, incremental scale and sum 254, three accumulators 254. Fp16, of 30: square root of the sum 18, divide by the largest 30, power of two 30, incremental scale and sum 30, three accumulators 30.0255075100binary32, 254 exponentssquare root of the sum53%divide by the largest100%power of two100%incremental scale and sum100%three accumulators100%fp16, 30 exponentssquare root of the sum60%divide by the largest100%power of two100%incremental scale and sum100%three accumulators100%per cent of the exponents with a correct normlength eight · one vector, every exponentany scale at all buys the range
Fig. 4 On how many of a format’s normal exponents each route returns a correct norm, for one vector of length eight scaled through them all, in binary32 and fp16.

None of this costs the repair its reason for existing. The plain expression is right on 135 of binary32’s 254 normal exponents and 18 of fp16’s 30, about the same shortfall the range measurement reported. All four of the others are right on every exponent, the power of two included. So the range needed a scale, and it never needed that scale to be the exact value of the largest entry. A number within a factor of two of it will do, and the nearest power of two is such a number and is the only kind whose use leaves no trace.

The same logic applies to the change of units that overflows before the answer: rescaling a problem by a power of two is exact in the arithmetic and loses nothing, where rescaling by the measured norm rounds at every entry for no gain. The first of those two essays calls the division “what hypot does, what every BLAS nrm2 does”. That was fair as a description of a textbook and of the incremental form, which does divide, at every entry, and which is the fourth curve here. It is less fair of current reference code, which is why the fifth curve exists.

The accumulators do have a limit, and fp16 is where it shows. In binary32 they match the plain expression on every pair. In fp16 they return only 12 of the 18 integer norms, because the safe middle window spans just nine octaves and an entry as small as 5 is already above it and gets scaled down with bits falling off. And their scaled-up accumulator overflows outright on 4,095 entries of two to the minus nine, returning not-a-number, since the method’s constants are chosen to give a length limit that a wide format never approaches and a narrow one reaches easily. The design assumes a format with range to spare.

Where the error actually is

The largest errors in all of this are still only a few units. To find a large one, the vector should be chosen for what a sum does badly rather than for what a scale does badly: one entry of size one, and a long tail of equal small entries.

The relative error of a binary32 norm of one entry of size one and 4,095 entries of two to the minus k, against k: the scaled route, the divided route, and the scaled route with its sum compensated or sortedAt k = 11 the scaled route's error is 0.00 units of the roundoff, at k = 12 it is 2047 and the divided route's 2047, at k = 13 it is 512, at k = 16 it is 8. Compensated, the error at k = 12 is 0.62; sorted, 0.62.units of the roundoffat k = 110.002at k = 122047compensated at 120.620481216202410⁻³10⁻²10⁻¹110¹10²10³10⁴k, the small entries are two to the minus kerror in units of the roundoffeither scale, largest firstsum compensatedbinary32 · length 4,096 · floored at 10⁻³a square half an ulp below the running sum
Fig. 5 The error of a binary32 norm of one entry of size one and 4,095 entries of two to the minus kk, against kk, by either scale with the large entry first (red), and with the sum compensated (blue).

At k=11k = 11 the error is too small to print. At k=12k = 12 it is 2,047 units, by the divided route and the power-of-two route alike, to within two units of each other. At 13 it is 512, at 14 it is 128, and it goes on falling by four per step until it reaches rounding at 18. The curve is a cliff on one side and a straight line on the other, and it is entirely a property of the sum.

The arithmetic can be done by hand. Binary32’s spacing just above one is 2−232^{-23}. A running sum that starts at one and takes a term of 2−242^{-24} is exactly halfway between two neighbours, and round-to-even returns one. So each of the 4,095 squares at k=12k = 12 vanishes on arrival, and the sum ends where it began. The true squared norm is 1+4095⋅2−241 + 4095 \cdot 2^{-24}, the square root halves the relative error, and 4095/24095/2 units of 2−242^{-24} is 2,047.5 units. That is the measured number. At k=11k = 11 every square is 2−222^{-22}, which is two whole spacings and is added exactly; that is the side of the cliff with nothing on it. Past 12 the squares still vanish but are worth a quarter as much each per step, so the straight line has slope four.

No entry in this vector is near the top or the bottom of the range. Its norm is 1.000122. Every route here is protected against overflow, every one returns exactly 1, and binary32 could have held three more digits. A scale does nothing because scaling by any power of two moves all the terms together, which leaves their ratio to the running sum where it was.

The length decides how far, and half a format decides where

The worst error of a scaled norm over every depth of the small entries, at five lengths, in binary32 and in fp16, in units of each format's roundoffBinary32: length 16, 7 at k = 12; length 64, 31 at k = 12; length 256, 127 at k = 12; length 1024, 511 at k = 12; length 4096, 2047 at k = 12. Fp16: length 16, 4 at k = 6; length 64, 16 at k = 6; length 256, 61 at k = 6; length 1024, 216 at k = 6; length 4096, 753 at k = 5.1101001000binary32length 16, worst at k = 127length 64, worst at k = 1231length 256, worst at k = 12127length 1024, worst at k = 12511length 4096, worst at k = 122047fp16length 16, worst at k = 64length 64, worst at k = 616length 256, worst at k = 661length 1024, worst at k = 6216length 4096, worst at k = 5753worst error in units of the roundoffthe depth is half the format's significandhalf the length, in units
Fig. 6 The worst error over every depth of the small entries, at five lengths, in binary32 and in fp16, in units of each format’s roundoff.

The worst case sits at k=12k = 12 in binary32 at every length measured, and it is n/2n/2 units on nn small entries: 7 on 15, 127 on 255, 2,047 on 4,095. In fp16 it sits at k=6k = 6 and is smaller in units, 4, 16, 61 and 216 on lengths up to 1,023, since there a square of 2−122^{-12} is a quarter of a spacing below one and more of the tail is needed before it would have mattered. At a length of 4,096 the worst moves to k=5k = 5 and 753 units, and there the error is no longer small in any sense: each square is one spacing above one and is added, until the running sum reaches two, where the spacing has doubled and every later square is lost. The sum stalls at two where the truth is five, and the norm is 37 per cent wrong on a vector whose largest entry is one. The depth is half the bits of the significand, the point where a square is half a spacing. That is the same halving of the exponent range the first essay found at overflow: a squaring spends half of whatever it touches, and the significand is no exception.

So the liability scales with the length and is reached by entries a factor of only 4,096 smaller than the largest. A vector whose largest entry is 1 and whose other four thousand entries are all about 0.00024 is not exotic. It is a residual with one bad row in it, or a unit impulse with a little noise, in single precision.

The repair is to the sum

The same plot’s lower curve is the same scaled norm with Kahan’s compensated summation in place of the plain running sum, at the price of four additions an entry rather than one. It has no cliff. Its error at k=12k = 12 is 0.62 units and it stays under one at every depth in binary32, a factor of more than three thousand. Adding the squares smallest first does the same thing a different way: the tail is summed against itself until it is large enough to count, and this gives exactly the same 0.62, at the price of a sort rather than a few extra additions.

Both of these are known remedies for a known effect: the order they are added in is the general form, and the vector that hides it shows how a sum’s own arithmetic can stay out of sight of the tests a code usually runs. What is particular to a norm is how quickly the condition is met, since the terms are squares. An entry that is a four-thousandth of the largest is a sixteen-millionth once squared, and that is all binary32 has. A gap of sixteen million between terms is what people have in mind when they reach for compensation, and here it is a gap of four thousand between entries.

None of the reference routines compensates, and for most vectors they are right. On even entries the plain sum’s error is under four units at a length of a thousand, and four additions in place of one is a lot to pay for that. The trouble is purely that nothing flags the other case: the routine has already found the largest entry and has no reason to look at the rest.

What this settles and what it does not

Everything here is a norm of a vector taken in one pass, measured in two simulated formats whose rounding has been checked against hardware separately. Nothing measured the speed of a division against a multiplication, which on most hardware is a real further cost and was left out because it varies by machine and would obscure the digits. The kinds of vector are three, chosen to be different from each other rather than typical. The count of integer norms is a count of exact cases, and a single unit in the last place is, for most purposes, a difference no calculation downstream will ever register.

The two claims that do stand are narrower and firmer. A route with a division in it cannot return some exact answers the route without one returns, and it has no compensating advantage, because the range it protects can be protected by a scale that does not round. And the size of a norm’s error is decided by the sum’s arithmetic on the small entries. The direction the error leans measures how a rounding mode tilts that same running error, and the cliff above is the limiting case of the same thing, in which each small term rounds the same way every time.

Still open: how often the cliff is met, and a test that reads for it

A tail with some spread in it. The cliff is sharp because every small entry is the same size. If the entries are spread over even a factor of two around 2−122^{-12}, some of their squares survive and some do not. The prediction with a sign is that at a length of 4,096, drawing the tail log-uniformly over a factor of four around that depth keeps the error above 500 units and below 1,500, so that the spread takes off at most two thirds of the peak rather than removing it. A wider spread, over a factor of a hundred, is predicted to bring it below 100.

A test that reads for it. The routine has already found the largest entry on its first pass, and that pass could as cheaply count the entries below half the significand’s depth. Since the cliff’s height is half the number of entries lost, the prediction is that compensating only when that count exceeds eight keeps the worst error over every vector measured here under eight units, while running the extra additions on none of the even and graded vectors at all. The test would be one comparison an entry, and it could be folded into the first pass of any scaled route. Whether the threshold has a clean form in fp16, whose half-significand depth falls between two integers, is the part most likely to be wrong.

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 summationFp16hypotOverflowScaled normUnit roundoff