While working on [#5904], Claude discovered that reducing the phi argument by pi causes significant round-off errors. Claude says:
When phi is the double float closest to an odd multiple of %pi/2 (other than %pi/2 itself), the incomplete elliptic integrals can use the wrong number of periods. For m near 1 the result is then wrong from about the 12th digit.
(%i1) fpprintprec:17$
(%i2) elliptic_f(float(3*%pi/2), float(1-2^-30));
(%o2) 35.35050621609541
The exact value at these inputs is 35.350506216083381, so the error is 1693 ulps. The values at the neighbouring floats show the jump:
| phi | Maxima | Exact |
|---|---|---|
float(3*%pi/2)-2^-50 |
35.35050621605427 |
35.350506216054277 |
float(3*%pi/2) |
35.35050621609541 |
35.350506216083381 |
float(3*%pi/2)+2^-50 |
35.35050621611248 |
35.350506216112484 |
The same happens for the other two functions:
(%i3) elliptic_pi(0.5, float(3*%pi/2), float(1-2^-30));
(%o3) 66.96166101939264
/* exact: 66.961661019368576, error 1693 ulps */
(%i4) imagpart(elliptic_e(float(3*%pi/2), float(1+2^-30)));
(%o4) 2.1943771181399678e-9
/* exact: 2.1943771181287562e-9, error 27108 ulps */
float(7*%pi/2) fails in the same way. float(5*%pi/2) and float(201*%pi/2) are correct.
Cause
The functions use F(phi|m) = F(phi - k*%pi|m) + 2*k*K(m) with k =
round(phi/%pi), and the corresponding formulas for the other two.
float(3*%pi/2) is slightly less than 3*%pi/2, so the correct k is
cos(phi)^2, so they cannot3*%pi/2 + d instead of 3*%pi/2 - d, where d is the3*%pi/2.The error is about 2*d times the integrand at %pi/2. For most m that
is below one ulp. It is large when the integrand is large there: m
near 1 for elliptic_f, n or m near 1 for elliptic_pi, and it shows in
the imaginary part of elliptic_e for m just above 1, which is itself
small. elliptic_e for 0 < m < 1 already handles a reduced angle past
%pi/2 and is not affected.
Possible fix
Check k against the sign of the cosine, which is computed
accurately. With s = (-1)^k*sin(phi) and c = (-1)^k*cos(phi), the
reduced angle is within [-%pi/2, %pi/2] exactly when c >= 0. If c < 0,
replace k by k+1 when s > 0 and by k-1 when s < 0. A prototype of this
for elliptic_pi reduced the errors at the affected points to 4 to 6
ulps.
Diff:
Related
Bugs:
#5904