skoro v0.0.0

Removable singularities in floating point

The coefficients of the rotations' and the rigid motions' maps are smooth at the identity, but their closed forms divide by a power of the angle there; each is computed by a series below a cutoff and its closed form above, and the cutoff is set by how the error of a sine and a cosine, relative or absolute, is amplified by that division.

Updated 2026-11-25 · 8 min read

The exponential of a rotation, its logarithm and their Jacobians are smooth maps, and their formulas are not: each has a coefficient such as sin⁡(θ/2)/θ\sin(\theta/2)/\theta whose closed form is 0/00/0 at the identity, the point computations visit most. The singularity is removable: the coefficient extends smoothly through θ=0\theta = 0, and its power series says how. This note lists the coefficients, says how each is computed so that its value and its derivatives are right on both sides of the identity, and works out what decides where the series should hand over to the closed form. The answer depends on how accurately the machine computes a sine.

The coefficients#

The coefficients of the rotations and of the rigid motions are each a function of θ2\theta^2, with θ\theta the angle of a rotation vector, or for the logarithm of n2n^2 and ww, with n=∣vec⁡(q)∣n = \lvert \operatorname{vec}(q) \rvert.

coefficientclosed formseriesused by
aacos⁡θ2\cos \tfrac\theta2∑k(−1)kθ2k4k(2k)!=1−θ28+θ4384−⋯\sum_k \frac{(-1)^k \theta^{2k}}{4^k (2k)!} = 1 - \frac{\theta^2}{8} + \frac{\theta^4}{384} - \cdotsthe exponential (6)
bbsin⁡(θ/2)/θ\sin(\theta/2)/\theta∑k(−1)kθ2k22k+1(2k+1)!=12−θ248+θ43840−⋯\sum_k \frac{(-1)^k \theta^{2k}}{2^{2k+1} (2k+1)!} = \frac12 - \frac{\theta^2}{48} + \frac{\theta^4}{3840} - \cdotsthe exponential
AA(1−cos⁡θ)/θ2(1 - \cos \theta)/\theta^2∑k(−1)kθ2k(2k+2)!=12−θ224+θ4720−⋯\sum_k \frac{(-1)^k \theta^{2k}}{(2k+2)!} = \frac12 - \frac{\theta^2}{24} + \frac{\theta^4}{720} - \cdotsthe Jacobians (9)
BB(θ−sin⁡θ)/θ3(\theta - \sin \theta)/\theta^3∑k(−1)kθ2k(2k+3)!=16−θ2120+θ45040−⋯\sum_k \frac{(-1)^k \theta^{2k}}{(2k+3)!} = \frac16 - \frac{\theta^2}{120} + \frac{\theta^4}{5040} - \cdotsthe Jacobians
CC(1−θ2cot⁡θ2)/θ2\bigl(1 - \frac\theta2 \cot \frac\theta2\bigr)/\theta^2112+θ2720+θ430240+⋯\frac1{12} + \frac{\theta^2}{720} + \frac{\theta^4}{30240} + \cdotstheir inverses (10)
LL2atan2⁡(n,w)/n2 \atantwo(n, w)/n2w∑k(−1)kxk2k+1=2w(1−x3+x25−⋯ )\frac2w \sum_k \frac{(-1)^k x^k}{2k+1} = \frac2w \bigl(1 - \frac x3 + \frac{x^2}5 - \cdots\bigr), x=n2/w2x = n^2/w^2the logarithm (7)
DD(θ2+2cos⁡θ−2)/(2θ4)(\theta^2 + 2 \cos \theta - 2)/(2 \theta^4)∑k(−1)kθ2k(2k+4)!=124−θ2720+⋯\sum_k \frac{(-1)^k \theta^{2k}}{(2k+4)!} = \frac1{24} - \frac{\theta^2}{720} + \cdotsthe motions’ Jacobians (6)
EE(2θ−3sin⁡θ+θcos⁡θ)/(2θ5)(2\theta - 3 \sin \theta + \theta \cos \theta)/(2 \theta^5)∑k(−1)k(k+1) θ2k(2k+5)!=1120−θ22520+⋯\sum_k \frac{(-1)^k (k+1)\, \theta^{2k}}{(2k+5)!} = \frac1{120} - \frac{\theta^2}{2520} + \cdotsthe motions’ Jacobians

The series of CC is that of θ2cot⁡θ2=1−θ212−θ4720−θ630240−⋯\tfrac\theta2 \cot \tfrac\theta2 = 1 - \tfrac{\theta^2}{12} - \tfrac{\theta^4}{720} - \tfrac{\theta^6}{30240} - \cdots, and that of LL is atan2⁡(n,w)/n=arctan⁡(n/w)/n\atantwo(n, w) / n = \arctan(n/w)/n for w>0w > 0, expanded in xx. Other spaces have coefficients of the same kind: the volume of the exponential chart of a sphere, for one, is a power of sin⁡r/r\sin r / r, and its logarithm log⁡(sin⁡r/r)=−r26−r4180−r62835−⋯\log(\sin r / r) = -\tfrac{r^2}{6} - \tfrac{r^4}{180} - \tfrac{r^6}{2835} - \cdots is computed the same way.

A series below a cutoff#

Each coefficient is computed by its series, truncated, where its argument is below a cutoff, and by its closed form above it. Three things have to be right for the result to be the smooth function it stands for, in its derivatives as well as its value.

The series is a function of the same arguments. A derivative is taken of the formula that was evaluated, so the series must depend on its arguments as the function does. The coefficients in θ\theta are functions of θ2\theta^2 and their series are polynomials in it. The logarithm’s coefficient depends on n2n^2 and on ww, and its series keeps both. On the sphere w=(1−n2)1/2w = (1 - n^2)^{1/2}, and a series in n2n^2 alone would have the right values, but it would have no derivative in ww, and second derivatives along the sphere, which move ww at second order, would be wrong. The logarithm’s series also holds near the identity only, for n2<c w2n^2 < c\, w^2 and w>0w > 0; near q=−1q = -1 the closed form is used, and what it has there is the logarithm’s own cut, which no series removes.

The series keeps enough terms. It keeps at least the terms through θ2\theta^2, so that derivatives of second order at the identity are exact, and beyond that as many as make its truncation error at the cutoff smaller than the rounding of the closed form there.

Neither formula is evaluated where it is undefined. When both formulas are computed and one is then chosen, as a computation over many values at once does, the one not chosen must still be finite, since a reverse pass multiplies its derivative by zero, and zero times an undefined number is undefined. So the closed form is given a harmless argument where the series is chosen, and the series, where it can be undefined, as the logarithm’s 1/w1/w is at w≤0w \le 0, a harmless one where the closed form is.

Relative and absolute error#

A computed value carries an error, the difference between what the machine returns and the exact value at the same, already rounded, input. Write uu for the unit roundoff of the arithmetic, 2−24≈6.0⋅10−82^{-24} \approx 6.0 \cdot 10^{-8} in single precision and 2−532^{-53} in double. A function has relative error η\eta when its computed value is f(x)(1+δ)f(x)(1 + \delta) with ∣δ∣≤η\lvert \delta \rvert \le \eta, and absolute error ε\varepsilon when it is f(x)+δf(x) + \delta with ∣δ∣≤ε\lvert \delta \rvert \le \varepsilon. The two differ only where f(x)f(x) is small, which is exactly where a removable singularity is computed, since sin⁡θ≈θ\sin \theta \approx \theta near 00.

A mathematical library’s sine is accurate relative to its value, to within a few uu. That is not a law of nature. An implementation built for throughput, as a graphics processor’s often is, may promise only an absolute error εs\varepsilon_s for its sine and εc\varepsilon_c for its cosine, each many times uu. Near cos⁡θ≈1\cos \theta \approx 1 the two kinds of error hardly differ. Near sin⁡θ≈0\sin \theta \approx 0 they differ by the factor 1/θ1/\theta.

How the error reaches a closed form#

Every closed form of the table but the logarithm’s is

f(θ)=P(θ,sin⁡θ,cos⁡θ)θk,P(θ,sin⁡θ,cos⁡θ)=O(θk),(1) f(\theta) = \frac{P\bigl(\theta, \sin \theta, \cos \theta\bigr)}{\theta^k}, \qquad P(\theta, \sin \theta, \cos \theta) = O(\theta^k), \tag{1}
(1)

with PP a polynomial whose own partial derivatives in its second and third arguments do not vanish at 00: BB has P=θ−sin⁡θP = \theta - \sin \theta and k=3k = 3, and EE has P=2θ−3sin⁡θ+θcos⁡θP = 2\theta - 3 \sin \theta + \theta \cos \theta and k=5k = 5. The numerator cancels to O(θk)O(\theta^k). The error of the sine and the cosine does not. With the computed s~=sin⁡θ+δs\tilde s = \sin \theta + \delta_s and c~=cos⁡θ+δc\tilde c = \cos \theta + \delta_c, to first order in the errors,

f~−f=∂sP δs+∂cP δcθk, \tilde f - f = \frac{\partial_s P\, \delta_s + \partial_c P\, \delta_c}{\theta^k},

so that under absolute errors ∣f~−f∣≤K ε θ−k\lvert \tilde f - f \rvert \le K\, \varepsilon\, \theta^{-k}, with KK bounded near 00 and ε=max⁡(εs,εc)\varepsilon = \max(\varepsilon_s, \varepsilon_c), while under relative ones, ∣δs∣≤u θ\lvert \delta_s \rvert \le u\, \theta and ∣δc∣≤u\lvert \delta_c \rvert \le u, the sine’s part is smaller by the factor θ\theta.

Derivatives make it worse. Automatic differentiation evaluates each step’s derivative formula at the computed values, so it differentiates the formula as written, never the rounding, and the mm-th derivative of (1) in θ2\theta^2 is another expression of the same form: a polynomial in θ\theta, s~\tilde s and c~\tilde c over θk+2m\theta^{k + 2m}, since each derivative in θ2\theta^2 is 12θddθ\tfrac{1}{2 \theta} \tfrac{\dd}{\dd \theta} and lowers the order of the numerator’s cancellation by two. Its error is therefore at most

Km ε θ−(k+2m)under absolute error,Km u θ1−(k+2m)for the sine’s part under relative error.(2) K_m\, \varepsilon\, \theta^{-(k + 2m)} \quad \text{under absolute error}, \qquad K_m\, u\, \theta^{1 - (k + 2m)} \quad \text{for the sine's part under relative error}. \tag{2}
(2)

Against a sine of relative accuracy, a sine of absolute accuracy loses the factor εs/(u θ)\varepsilon_s / (u\, \theta) in every closed form whose numerator holds a sine with a coefficient that does not cancel, as BB’s and EE’s do, and nothing in those that hold only a cosine, as AA’s and DD’s do. With εs=2−21\varepsilon_s = 2^{-21} in single precision that factor is 8/θ8 / \theta: about 2525 at θ2=0.1\theta^2 = 0.1, in a second derivative of EE that is already divided by θ9\theta^9.

Three kinds of map are spared. One without a cancelling numerator, such as a rotation’s matrix, its action on a vector, or the exponential’s cos⁡(θ/2)\cos(\theta / 2), carries ε\varepsilon into its value once. The logarithm’s LL divides by nn the angle of a point, and an atan2⁡\atantwo of relative accuracy loses nothing. And the argument itself, θ\theta computed from θ2\theta^2 with a relative error of a few roundings, costs no more than a relatively accurate sine does: where the numerator and the power θk\theta^k are formed from the same computed θ~\tilde\theta, the closed form is evaluated consistently at a nearby argument, and its error is f′(θ)(θ~−θ)f'(\theta)(\tilde\theta - \theta), bounded near 00.

Where to put the cutoff#

The closed form is used above a cutoff θc\theta_c and the series below it. Two conditions decide θc\theta_c, and they pull in opposite directions.

The closed form above the cutoff. Take each order of derivative mm that is computed, and each map that uses the coefficient ff, and let wm(θ)w_m(\theta) be the factor by which the map multiplies ff’s mm-th derivative. Then

Km ε θc−(k+2m) wm(θc)≤τ,(3) K_m\, \varepsilon\, \theta_c^{-(k + 2m)}\, w_m(\theta_c) \le \tau, \tag{3}
(3)

where τ\tau is the error the coefficient may contribute and ε\varepsilon the absolute error of the sine and the cosine on the least accurate machine the computation must run on. The bound decreases in θc\theta_c, so a larger cutoff always meets it, and by (2) a machine with a relatively accurate sine meets it wherever one with the absolute error ε\varepsilon does. How far a cutoff must rise for a sine of absolute error ε\varepsilon to do no worse than a relatively accurate one did at θc\theta_c is, by the same scaling, the factor (ε/(u θc))1/(k+2m)(\varepsilon / (u\, \theta_c))^{1 / (k + 2m)}, which is largest where k+2mk + 2m is smallest. It is a guide only, since it drops KmK_m.

The series below it. The series truncated after NN terms, with remainder RNR_N, must meet the rounding of the most precise arithmetic in use at the cutoff, in its value and its derivatives:

∣dmd(θ2)mRN(θc2)∣≤u ∣f(θc)∣.(4) \Bigl\lvert \tfrac{\dd^m}{\dd (\theta^2)^m} R_N(\theta_c^2) \Bigr\rvert \le u\, \lvert f(\theta_c) \rvert . \tag{4}
(4)

The series of bb, AA, BB, DD and EE are entire, with terms falling like θ2n/(2n+k)!\theta^{2n} / (2n + k)!, so every cutoff can be met with enough terms: at θ2=1\theta^2 = 1 in double precision, about ten. CC’s series converges for θ<2π\theta < 2\pi, with terms falling by (θ/2π)2(\theta / 2\pi)^2, and that of log⁡(sin⁡r/r)\log(\sin r / r) for r<πr < \pi, with terms falling by (r/π)2(r / \pi)^2. Either radius bounds the cutoff away from it, and the number of terms grows as the cutoff nears it. A series costs a product and a sum per term.

One cutoff serves every machine. The first condition is hardest in the least precise arithmetic with the least accurate sine, and the second in the most precise arithmetic. A cutoff that meets the first for the one and the second for the other meets both everywhere else, so a formula need not know which machine it runs on. For the entire series such a cutoff always exists. Raising it costs terms, and nothing else.

Built on by