How could I define trigonometric functions that take arguments in degrees instead of the usual radians, and compute correctly rounded results for these arguments?
Mu
The only rationals q
for which cosdeg(360q)
is rational have 1, 2, 3, 4, or 6 as the denominator. This paper by Joerg Jahnel contains a short and beautiful proof using field theory in section 6. (Indeed, the author characterises the degree of the algebraic number cosdeg(360q)
using Euler's totient function.) So there is no floating-point q
such that cosdeg(360q)
is halfway between two adjacent floating-point numbers.
So I guess the answer is "about the same way you implement sin
and friends for radians," though @gnasher729 makes the excellent point that argument reduction for degrees is much, much nicer.
Well, this is a difficult question. Let me clarify some points:
I basically recommend to use MPFR if you are obliged to use inputs only in degrees. Let me remind you that any argument in degrees, when it is multiplied by (Pi/180), it produces a transcendental number. However, What is passed to the trigonometric function is the floating point representation rounded, preferably rounded to nearest integer, to the working precision.
I recommend you to do as follows:
"Elementary functions", by Muller shows statistically that most, NOT ALL, of the hard cases are correctly rounded if the working precision is slightly larger than twice the target precision. But in your case, as the input is theoretically transcendental, to be safe, at the expense of the performance, make the working precision much higher than the target. Actually 10x is totally sufficient for almost 100% of cases, if you require up to double precision final result.
If you need a low precision, i.e. single precision or lower, it is feasible to do exhaustive test to decide on the lowest working precision which produces all cases correctly rounded.
It's difficult. On the positive side, you can reduce the argument to +/- 45 degrees exactly. So you need correctly rounded results between +/- 45 degrees. For very small x, sin (x) is about x * (pi / 180) which is hard enough to get rounded exactly.
To get mostly correctly rounded results for the sine function for example, take -45 <= x <= 45. Split x into xhi = round (512 x) / 512 and xlo = x - xhi. Let sin (x degrees) ≈ ax - bx^3. Round a and b so that s (x) a*xhi - b * (xhi^3) is calculated exactly. Calculate the remainder sin (x degrees) - s (x) carefully; the rounding error should be quite small because the result is small. Add to s (x), this will most of the time give the correctly rounded result.
You first need to detect the exact cases, and this has already been answered. Now, for the other cases, there's the well-known problem of the table maker's dilemma. If your arithmetic has a fixed (and small) precision and you want a certified bound on the intermediate precision that might be needed, there are two known solutions: