Skip to content

Instantly share code, notes, and snippets.

@Marc-B-Reynolds
Last active February 8, 2025 18:36
Show Gist options
  • Save Marc-B-Reynolds/450cec7f3646314c88a353bde50273d1 to your computer and use it in GitHub Desktop.
Save Marc-B-Reynolds/450cec7f3646314c88a353bde50273d1 to your computer and use it in GitHub Desktop.
Using sollya to "repeat" Cecil Hastings' 1955 approximations for atan. Works as an example of using a 'seed' approximation & using sollya to refine.
// Repeating Cecil Hastings atan approximations from 1955 using Sollya.
// Other than "amusement factor" these use Hasting's approximations
// to "seed" the sollya calls to 'fpminimax'.
//
// Current world usage could include using an initial approximation
// from some multiprecision or computer algebra system and then
// Sollya to refine the constants to hardware sized (half,single,double,etc)
//
// Additionally the wide choice of the range would cause Sollya to fail
// to converge if we didn't feed it an initial guess.
F = atan(x);
R = [-1;1];
T = floating;
E = absolute;
// sheet 8:
// H3 is the Hastings approximation
// L3 builds a list of points
//
// this a call that would fail to converge:
// P3 = fpminimax(F, [|1,3,5|], [|24...|], R,T,E);
H3 = horner(.995354*x -.288679*x^3+.079331*x^5);
L3 = dirtyfindzeros(F-H3, R);
P3 = fpminimax(F, [|1,3,5|], [|24...|], L3, E, default, default, H3);
write("hastings "); H3;
write("sollya "); P3;
print("norm = ", accurateinfnorm(F-P3, R, 23));
print("hastings = ", accurateinfnorm(F-H3, R, 23), "\n");
// sheet 9:
H4 = horner(.9992150*x - .3211819*x^3 + 0.1462766*x^5 - 0.0389929*x^7);
L4 = dirtyfindzeros(F-H4, R);
P4 = fpminimax(F, [|1,3,5,7|], [|24...|], L4, E, default, default, H4);
write("hastings "); H4;
write("sollya "); P4;
print("norm = ", accurateinfnorm(F-P4, R, 23));
print("hastings = ", accurateinfnorm(F-H4, R, 23), "\n");
// sheet 10:
H5 = horner(.9998660*x -.3302995*x^3 + .1801410*x^5 - .0851330*x^7 + .0208351*x^9);
L5 = dirtyfindzeros(F-H5, R);
P5 = fpminimax(F, [|1,3,5,7,9|], [|24...|], L5, E, default, default, H5);
write("hastings "); H5;
write("sollya "); P5;
print("norm = ", accurateinfnorm(F-P5, R, 23));
print("hastings = ", accurateinfnorm(F-H5, R, 23), "\n");
hastings x * (0.995354 + x^2 * (-0.288679 + x^2 * 7.9331e-2))
sollya x * (0.995353996753692626953125 + x^2 * (-0.2886789739131927490234375 + x^2 * 7.9330973327159881591796875e-2))
norm = 6.09312788583338260650634765625e-4
hastings = 6.09312322922050952911376953125e-4
hastings x * (0.999215 + x^2 * (-0.3211819 + x^2 * (0.1462766 + x^2 * (-3.89929e-2))))
sollya x * (0.99921500682830810546875 + x^2 * (-0.321181952953338623046875 + x^2 * (0.14627669751644134521484375 + x^2 * (-3.89929525554180145263671875e-2))))
norm = 8.1499878433533012866973876953125e-5
hastings = 8.1499922089278697967529296875e-5
hastings x * (0.999866 + x^2 * (-0.3302995 + x^2 * (0.180141 + x^2 * (-8.5133e-2 + x^2 * 2.08351e-2))))
sollya x * (0.999866008758544921875 + x^2 * (-0.3302995860576629638671875 + x^2 * (0.18014125525951385498046875 + x^2 * (-8.5133291780948638916015625e-2 + x^2 * 2.0835213363170623779296875e-2))))
norm = 1.149161835201084613800048828125e-5
hastings = 1.1491427358123473823070526123046875e-5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment