
(FPCore (x) :precision binary64 (- (/ PI 2.0) (* 2.0 (asin (sqrt (/ (- 1.0 x) 2.0))))))
double code(double x) {
return (((double) M_PI) / 2.0) - (2.0 * asin(sqrt(((1.0 - x) / 2.0))));
}
public static double code(double x) {
return (Math.PI / 2.0) - (2.0 * Math.asin(Math.sqrt(((1.0 - x) / 2.0))));
}
def code(x): return (math.pi / 2.0) - (2.0 * math.asin(math.sqrt(((1.0 - x) / 2.0))))
function code(x) return Float64(Float64(pi / 2.0) - Float64(2.0 * asin(sqrt(Float64(Float64(1.0 - x) / 2.0))))) end
function tmp = code(x) tmp = (pi / 2.0) - (2.0 * asin(sqrt(((1.0 - x) / 2.0)))); end
code[x_] := N[(N[(Pi / 2.0), $MachinePrecision] - N[(2.0 * N[ArcSin[N[Sqrt[N[(N[(1.0 - x), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\pi}{2} - 2 \cdot \sin^{-1} \left(\sqrt{\frac{1 - x}{2}}\right)
\end{array}
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (/ PI 2.0) (* 2.0 (asin (sqrt (/ (- 1.0 x) 2.0))))))
double code(double x) {
return (((double) M_PI) / 2.0) - (2.0 * asin(sqrt(((1.0 - x) / 2.0))));
}
public static double code(double x) {
return (Math.PI / 2.0) - (2.0 * Math.asin(Math.sqrt(((1.0 - x) / 2.0))));
}
def code(x): return (math.pi / 2.0) - (2.0 * math.asin(math.sqrt(((1.0 - x) / 2.0))))
function code(x) return Float64(Float64(pi / 2.0) - Float64(2.0 * asin(sqrt(Float64(Float64(1.0 - x) / 2.0))))) end
function tmp = code(x) tmp = (pi / 2.0) - (2.0 * asin(sqrt(((1.0 - x) / 2.0)))); end
code[x_] := N[(N[(Pi / 2.0), $MachinePrecision] - N[(2.0 * N[ArcSin[N[Sqrt[N[(N[(1.0 - x), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\pi}{2} - 2 \cdot \sin^{-1} \left(\sqrt{\frac{1 - x}{2}}\right)
\end{array}
(FPCore (x) :precision binary64 (if (<= x -1.66e-162) (fma -2.0 (asin (sqrt (fma -0.5 x 0.5))) (* 0.5 PI)) (fma 0.5 PI (* -2.0 (- (* 0.5 PI) (acos (sqrt 0.5)))))))
double code(double x) {
double tmp;
if (x <= -1.66e-162) {
tmp = fma(-2.0, asin(sqrt(fma(-0.5, x, 0.5))), (0.5 * ((double) M_PI)));
} else {
tmp = fma(0.5, ((double) M_PI), (-2.0 * ((0.5 * ((double) M_PI)) - acos(sqrt(0.5)))));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= -1.66e-162) tmp = fma(-2.0, asin(sqrt(fma(-0.5, x, 0.5))), Float64(0.5 * pi)); else tmp = fma(0.5, pi, Float64(-2.0 * Float64(Float64(0.5 * pi) - acos(sqrt(0.5))))); end return tmp end
code[x_] := If[LessEqual[x, -1.66e-162], N[(-2.0 * N[ArcSin[N[Sqrt[N[(-0.5 * x + 0.5), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] + N[(0.5 * Pi), $MachinePrecision]), $MachinePrecision], N[(0.5 * Pi + N[(-2.0 * N[(N[(0.5 * Pi), $MachinePrecision] - N[ArcCos[N[Sqrt[0.5], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.66 \cdot 10^{-162}:\\
\;\;\;\;\mathsf{fma}\left(-2, \sin^{-1} \left(\sqrt{\mathsf{fma}\left(-0.5, x, 0.5\right)}\right), 0.5 \cdot \pi\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(0.5, \pi, -2 \cdot \left(0.5 \cdot \pi - \cos^{-1} \left(\sqrt{0.5}\right)\right)\right)\\
\end{array}
\end{array}
if x < -1.66e-162Initial program 13.3%
Taylor expanded in x around 0
negate-subN/A
+-commutativeN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
Applied rewrites13.3%
if -1.66e-162 < x Initial program 5.0%
lift-asin.f64N/A
lift-sqrt.f64N/A
lift--.f64N/A
lift-/.f64N/A
*-lft-identityN/A
metadata-evalN/A
distribute-lft-out--N/A
associate-*l/N/A
metadata-evalN/A
sqrt-unprodN/A
asin-acosN/A
lift-/.f64N/A
lift-PI.f64N/A
lower--.f64N/A
sqrt-unprodN/A
negate-subN/A
mul-1-negN/A
metadata-evalN/A
mul-1-negN/A
negate-subN/A
Applied rewrites8.0%
Taylor expanded in x around 0
fp-cancel-sub-sign-invN/A
lift-PI.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
lower-fma.f64N/A
metadata-evalN/A
lower-*.f64N/A
Applied rewrites8.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-sqrt.f645.9
Applied rewrites5.9%
(FPCore (x) :precision binary64 (/ (- (* (* PI 0.5) (* PI 0.5)) (pow (* (- (* PI 0.5) (acos (sqrt (fma x -0.5 0.5)))) 2.0) 2.0)) (fma PI 0.5 (* (- (* PI 0.5) (acos (/ 1.0 (pow (fma x -0.5 0.5) -0.5)))) 2.0))))
double code(double x) {
return (((((double) M_PI) * 0.5) * (((double) M_PI) * 0.5)) - pow((((((double) M_PI) * 0.5) - acos(sqrt(fma(x, -0.5, 0.5)))) * 2.0), 2.0)) / fma(((double) M_PI), 0.5, (((((double) M_PI) * 0.5) - acos((1.0 / pow(fma(x, -0.5, 0.5), -0.5)))) * 2.0));
}
function code(x) return Float64(Float64(Float64(Float64(pi * 0.5) * Float64(pi * 0.5)) - (Float64(Float64(Float64(pi * 0.5) - acos(sqrt(fma(x, -0.5, 0.5)))) * 2.0) ^ 2.0)) / fma(pi, 0.5, Float64(Float64(Float64(pi * 0.5) - acos(Float64(1.0 / (fma(x, -0.5, 0.5) ^ -0.5)))) * 2.0))) end
code[x_] := N[(N[(N[(N[(Pi * 0.5), $MachinePrecision] * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision] - N[Power[N[(N[(N[(Pi * 0.5), $MachinePrecision] - N[ArcCos[N[Sqrt[N[(x * -0.5 + 0.5), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * 2.0), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision] / N[(Pi * 0.5 + N[(N[(N[(Pi * 0.5), $MachinePrecision] - N[ArcCos[N[(1.0 / N[Power[N[(x * -0.5 + 0.5), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left(\pi \cdot 0.5\right) \cdot \left(\pi \cdot 0.5\right) - {\left(\left(\pi \cdot 0.5 - \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, -0.5, 0.5\right)}\right)\right) \cdot 2\right)}^{2}}{\mathsf{fma}\left(\pi, 0.5, \left(\pi \cdot 0.5 - \cos^{-1} \left(\frac{1}{{\left(\mathsf{fma}\left(x, -0.5, 0.5\right)\right)}^{-0.5}}\right)\right) \cdot 2\right)}
\end{array}
Initial program 7.3%
lift-asin.f64N/A
lift-sqrt.f64N/A
lift--.f64N/A
lift-/.f64N/A
*-lft-identityN/A
metadata-evalN/A
distribute-lft-out--N/A
associate-*l/N/A
metadata-evalN/A
sqrt-unprodN/A
asin-acosN/A
lift-/.f64N/A
lift-PI.f64N/A
lower--.f64N/A
sqrt-unprodN/A
negate-subN/A
mul-1-negN/A
metadata-evalN/A
mul-1-negN/A
negate-subN/A
Applied rewrites8.7%
Taylor expanded in x around 0
fp-cancel-sub-sign-invN/A
lift-PI.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
lower-fma.f64N/A
metadata-evalN/A
lower-*.f64N/A
Applied rewrites8.7%
Applied rewrites8.7%
lift-fma.f64N/A
lift-sqrt.f64N/A
pow1/2N/A
*-commutativeN/A
metadata-evalN/A
pow-negN/A
lower-/.f64N/A
lower-pow.f64N/A
*-commutativeN/A
lift-fma.f648.7
Applied rewrites8.7%
(FPCore (x) :precision binary64 (let* ((t_0 (- (* 0.5 PI) (acos (sqrt (fma x -0.5 0.5)))))) (/ (fma (* PI PI) 0.25 (* -4.0 (pow t_0 2.0))) (fma t_0 2.0 (* 0.5 PI)))))
double code(double x) {
double t_0 = (0.5 * ((double) M_PI)) - acos(sqrt(fma(x, -0.5, 0.5)));
return fma((((double) M_PI) * ((double) M_PI)), 0.25, (-4.0 * pow(t_0, 2.0))) / fma(t_0, 2.0, (0.5 * ((double) M_PI)));
}
function code(x) t_0 = Float64(Float64(0.5 * pi) - acos(sqrt(fma(x, -0.5, 0.5)))) return Float64(fma(Float64(pi * pi), 0.25, Float64(-4.0 * (t_0 ^ 2.0))) / fma(t_0, 2.0, Float64(0.5 * pi))) end
code[x_] := Block[{t$95$0 = N[(N[(0.5 * Pi), $MachinePrecision] - N[ArcCos[N[Sqrt[N[(x * -0.5 + 0.5), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(N[(N[(Pi * Pi), $MachinePrecision] * 0.25 + N[(-4.0 * N[Power[t$95$0, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(t$95$0 * 2.0 + N[(0.5 * Pi), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \pi - \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, -0.5, 0.5\right)}\right)\\
\frac{\mathsf{fma}\left(\pi \cdot \pi, 0.25, -4 \cdot {t\_0}^{2}\right)}{\mathsf{fma}\left(t\_0, 2, 0.5 \cdot \pi\right)}
\end{array}
\end{array}
Initial program 7.3%
lift-asin.f64N/A
lift-sqrt.f64N/A
lift--.f64N/A
lift-/.f64N/A
*-lft-identityN/A
metadata-evalN/A
distribute-lft-out--N/A
associate-*l/N/A
metadata-evalN/A
sqrt-unprodN/A
asin-acosN/A
lift-/.f64N/A
lift-PI.f64N/A
lower--.f64N/A
sqrt-unprodN/A
negate-subN/A
mul-1-negN/A
metadata-evalN/A
mul-1-negN/A
negate-subN/A
Applied rewrites8.7%
Taylor expanded in x around 0
fp-cancel-sub-sign-invN/A
lift-PI.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
lower-fma.f64N/A
metadata-evalN/A
lower-*.f64N/A
Applied rewrites8.7%
Applied rewrites8.7%
Applied rewrites8.7%
(FPCore (x) :precision binary64 (fma 0.5 PI (* -2.0 (- (* 0.5 PI) (acos (sqrt (fma x -0.5 0.5)))))))
double code(double x) {
return fma(0.5, ((double) M_PI), (-2.0 * ((0.5 * ((double) M_PI)) - acos(sqrt(fma(x, -0.5, 0.5))))));
}
function code(x) return fma(0.5, pi, Float64(-2.0 * Float64(Float64(0.5 * pi) - acos(sqrt(fma(x, -0.5, 0.5)))))) end
code[x_] := N[(0.5 * Pi + N[(-2.0 * N[(N[(0.5 * Pi), $MachinePrecision] - N[ArcCos[N[Sqrt[N[(x * -0.5 + 0.5), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(0.5, \pi, -2 \cdot \left(0.5 \cdot \pi - \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, -0.5, 0.5\right)}\right)\right)\right)
\end{array}
Initial program 7.3%
lift-asin.f64N/A
lift-sqrt.f64N/A
lift--.f64N/A
lift-/.f64N/A
*-lft-identityN/A
metadata-evalN/A
distribute-lft-out--N/A
associate-*l/N/A
metadata-evalN/A
sqrt-unprodN/A
asin-acosN/A
lift-/.f64N/A
lift-PI.f64N/A
lower--.f64N/A
sqrt-unprodN/A
negate-subN/A
mul-1-negN/A
metadata-evalN/A
mul-1-negN/A
negate-subN/A
Applied rewrites8.7%
Taylor expanded in x around 0
fp-cancel-sub-sign-invN/A
lift-PI.f64N/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
lower-fma.f64N/A
metadata-evalN/A
lower-*.f64N/A
Applied rewrites8.7%
(FPCore (x) :precision binary64 (if (<= x -5e-310) (fma -2.0 (asin (sqrt 0.5)) (* 0.5 PI)) (- (/ PI 2.0) (* 2.0 (asin (/ 1.0 (sqrt 2.0)))))))
double code(double x) {
double tmp;
if (x <= -5e-310) {
tmp = fma(-2.0, asin(sqrt(0.5)), (0.5 * ((double) M_PI)));
} else {
tmp = (((double) M_PI) / 2.0) - (2.0 * asin((1.0 / sqrt(2.0))));
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= -5e-310) tmp = fma(-2.0, asin(sqrt(0.5)), Float64(0.5 * pi)); else tmp = Float64(Float64(pi / 2.0) - Float64(2.0 * asin(Float64(1.0 / sqrt(2.0))))); end return tmp end
code[x_] := If[LessEqual[x, -5e-310], N[(-2.0 * N[ArcSin[N[Sqrt[0.5], $MachinePrecision]], $MachinePrecision] + N[(0.5 * Pi), $MachinePrecision]), $MachinePrecision], N[(N[(Pi / 2.0), $MachinePrecision] - N[(2.0 * N[ArcSin[N[(1.0 / N[Sqrt[2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\mathsf{fma}\left(-2, \sin^{-1} \left(\sqrt{0.5}\right), 0.5 \cdot \pi\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\pi}{2} - 2 \cdot \sin^{-1} \left(\frac{1}{\sqrt{2}}\right)\\
\end{array}
\end{array}
if x < -4.999999999999985e-310Initial program 9.0%
Taylor expanded in x around 0
negate-subN/A
+-commutativeN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
Applied rewrites9.0%
Taylor expanded in x around 0
Applied rewrites6.0%
if -4.999999999999985e-310 < x Initial program 5.5%
lift-sqrt.f64N/A
lift--.f64N/A
lift-/.f64N/A
sqrt-divN/A
lower-/.f64N/A
lower-sqrt.f64N/A
lift--.f64N/A
lower-sqrt.f648.4
Applied rewrites8.4%
Taylor expanded in x around 0
Applied rewrites5.8%
(FPCore (x) :precision binary64 (fma -2.0 (asin (sqrt (fma -0.5 x 0.5))) (* 0.5 PI)))
double code(double x) {
return fma(-2.0, asin(sqrt(fma(-0.5, x, 0.5))), (0.5 * ((double) M_PI)));
}
function code(x) return fma(-2.0, asin(sqrt(fma(-0.5, x, 0.5))), Float64(0.5 * pi)) end
code[x_] := N[(-2.0 * N[ArcSin[N[Sqrt[N[(-0.5 * x + 0.5), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] + N[(0.5 * Pi), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-2, \sin^{-1} \left(\sqrt{\mathsf{fma}\left(-0.5, x, 0.5\right)}\right), 0.5 \cdot \pi\right)
\end{array}
Initial program 7.3%
Taylor expanded in x around 0
negate-subN/A
+-commutativeN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
Applied rewrites7.3%
(FPCore (x) :precision binary64 (fma -2.0 (asin (sqrt 0.5)) (* 0.5 PI)))
double code(double x) {
return fma(-2.0, asin(sqrt(0.5)), (0.5 * ((double) M_PI)));
}
function code(x) return fma(-2.0, asin(sqrt(0.5)), Float64(0.5 * pi)) end
code[x_] := N[(-2.0 * N[ArcSin[N[Sqrt[0.5], $MachinePrecision]], $MachinePrecision] + N[(0.5 * Pi), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(-2, \sin^{-1} \left(\sqrt{0.5}\right), 0.5 \cdot \pi\right)
\end{array}
Initial program 7.3%
Taylor expanded in x around 0
negate-subN/A
+-commutativeN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
Applied rewrites7.3%
Taylor expanded in x around 0
Applied rewrites4.2%
herbie shell --seed 2025118
(FPCore (x)
:name "Ian Simplification"
:precision binary64
(- (/ PI 2.0) (* 2.0 (asin (sqrt (/ (- 1.0 x) 2.0))))))