Re:Implementing Cosine in C from Scratch (2020)
by jhgb on Tuesday March 29, 2022 @02:52PM
(#30846812)
Does it make sense to use a double-double input when you only have double output? Sine is Lispchitz-limited by 1 so I don't see how this makes a meaningful difference.
Re:Implementing Cosine in C from Scratch (2020)
by lifthrasiir on Tuesday March 29, 2022 @03:09PM
(#30847009)
The input might be double but the constant pi is not. Let f64(x) be a function from any real number to double, so that an ordinary expression `a + b` actually computes f64(a + b) and so on. Then in general f64(sin(x)) may differ from f64(sin(f64(x mod 2pi))); since you can't directly compute f64(sin(x mod 2pi)), you necessarily need more precision during argument reduction so that f64(sin(x)) = f64(sin(f64timeswhatever(x mod 2pi))).
Re:Implementing Cosine in C from Scratch (2020)
by jhgb on Tuesday March 29, 2022 @03:28PM
(#30847235)
But am I correct in thinking that is at worst a 0.5 ulp error in this case? The lesser term in double-double can't be more than 0.5 ulp of the greater term and sensitivity of both sine and cosine to an error in the input will not be more than 1.
Also, in case of confusion, I was specifically commenting on the function over the [-pi/4, pi/4] domain in https://github.com/ifduyue/musl/blob/master/src/math/__cos.c , which the comment in https://news.ycombinator.com/item?id=30846546 was presumably about.
Re:Implementing Cosine in C from Scratch (2020)
by lifthrasiir on Tuesday March 29, 2022 @03:36PM
(#30847338)
Yeah, sine and cosine are not as sensitive (but note that many libms target 1 or 1.5 ulp error for them, so a 0.5 ulp error might still be significant). For tangent however you definitely need more accurate range reduction.
Re:Implementing Cosine in C from Scratch (2020)
by adgjlsfhk1 on Tuesday March 29, 2022 @03:31PM
(#30847267)
Double rounding can still bite you. You are forced to incur up to half an ulp of error from your polynomial, so taking another half ulp in your reduction can lead to a total error of about 1 ulp.
[1] https://en.wikipedia.org/wiki/Quadruple-precision_floating-p...
[2] https://github.com/ifduyue/musl/blob/6d8a515796270eb6cec8a27...