This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.
cos
kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
Input x is assumed to be bounded by ~pi/4 in magnitude.
Input y is the tail of x.
Algorithm
Since cos(-x) = cos(x), we need only to consider positive x.
if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0.
cos(x) is approximated by a polynomial of degree 14 on
[0,pi/4]
4 14
cos(x) ~ 1 - xx/2 + C1x + ... + C6*x
where the remez error is
let r = C1x +C2x +C3x +C4x +C5x +C6x , then
cos(x) ~ 1 - xx/2 + r
since cos(x+y) ~ cos(x) - sin(x)y
~ cos(x) - xy,
a correction term is necessary in cos(x) and hence
cos(x+y) = 1 - (xx/2 - (r - xy))
For better accuracy, rearrange to
cos(x+y) ~ w + (tmp + (r-xy))
where w = 1 - xx/2 and tmp is a tiny correction term
(1 - xx/2 == w + tmp exactly in infinite precision).
The exactness of w + tmp in infinite precision depends on w
and tmp having the same precision as x. If they have extra
precision due to compiler bugs, then the extra precision is
only good provided it is retained in all terms of the final
expression for cos(). Retention happens in all cases tested
under FreeBSD, so don't pessimize things by forcibly clipping
any extra precision in w.