Ian Simplification

Percentage Accurate: 7.0% → 8.5%
Time: 7.0s
Alternatives: 2
Speedup: 1.1×

Specification

?
\[\begin{array}{l} \\ \frac{\pi}{2} - 2 \cdot \sin^{-1} \left(\sqrt{\frac{1 - x}{2}}\right) \end{array} \]
(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}

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 2 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 7.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\pi}{2} - 2 \cdot \sin^{-1} \left(\sqrt{\frac{1 - x}{2}}\right) \end{array} \]
(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}

Alternative 1: 8.5% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\cos^{-1} \left(\sqrt{\mathsf{fma}\left(-0.5, x, 0.5\right)}\right), 2, -0.5 \cdot \pi\right) \end{array} \]
(FPCore (x)
 :precision binary64
 (fma (acos (sqrt (fma -0.5 x 0.5))) 2.0 (* -0.5 PI)))
double code(double x) {
	return fma(acos(sqrt(fma(-0.5, x, 0.5))), 2.0, (-0.5 * ((double) M_PI)));
}
function code(x)
	return fma(acos(sqrt(fma(-0.5, x, 0.5))), 2.0, Float64(-0.5 * pi))
end
code[x_] := N[(N[ArcCos[N[Sqrt[N[(-0.5 * x + 0.5), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * 2.0 + N[(-0.5 * Pi), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(\cos^{-1} \left(\sqrt{\mathsf{fma}\left(-0.5, x, 0.5\right)}\right), 2, -0.5 \cdot \pi\right)
\end{array}
Derivation
  1. Initial program 7.0%

    \[\frac{\pi}{2} - 2 \cdot \sin^{-1} \left(\sqrt{\frac{1 - x}{2}}\right) \]
  2. Applied rewrites8.5%

    \[\leadsto \frac{\pi}{2} - \color{blue}{\left(\pi + \left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, -0.5, 0.5\right)}\right)\right) \cdot 2\right)} \]
  3. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{\frac{\pi}{2} - \left(\pi + \left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right) \cdot 2\right)} \]
    2. lift-+.f64N/A

      \[\leadsto \frac{\pi}{2} - \color{blue}{\left(\pi + \left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right) \cdot 2\right)} \]
    3. associate--r+N/A

      \[\leadsto \color{blue}{\left(\frac{\pi}{2} - \pi\right) - \left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right) \cdot 2} \]
    4. sub-flipN/A

      \[\leadsto \color{blue}{\left(\frac{\pi}{2} - \pi\right) + \left(\mathsf{neg}\left(\left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right) \cdot 2\right)\right)} \]
    5. +-commutativeN/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(\left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right) \cdot 2\right)\right) + \left(\frac{\pi}{2} - \pi\right)} \]
    6. lift-*.f64N/A

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right) \cdot 2}\right)\right) + \left(\frac{\pi}{2} - \pi\right) \]
    7. lift-neg.f64N/A

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right)\right)} \cdot 2\right)\right) + \left(\frac{\pi}{2} - \pi\right) \]
    8. distribute-lft-neg-outN/A

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2\right)\right)}\right)\right) + \left(\frac{\pi}{2} - \pi\right) \]
    9. remove-double-negN/A

      \[\leadsto \color{blue}{\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2} + \left(\frac{\pi}{2} - \pi\right) \]
    10. sub-flipN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \color{blue}{\left(\frac{\pi}{2} + \left(\mathsf{neg}\left(\pi\right)\right)\right)} \]
    11. lift-/.f64N/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\color{blue}{\frac{\pi}{2}} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    12. mult-flipN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\color{blue}{\pi \cdot \frac{1}{2}} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    13. metadata-evalN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\pi \cdot \color{blue}{\frac{1}{2}} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    14. *-commutativeN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\color{blue}{\frac{1}{2} \cdot \pi} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    15. lift-PI.f64N/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\frac{1}{2} \cdot \color{blue}{\mathsf{PI}\left(\right)} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    16. add-log-expN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\frac{1}{2} \cdot \color{blue}{\log \left(e^{\mathsf{PI}\left(\right)}\right)} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    17. log-pow-revN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\color{blue}{\log \left({\left(e^{\mathsf{PI}\left(\right)}\right)}^{\frac{1}{2}}\right)} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    18. lift-PI.f64N/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\log \left({\left(e^{\mathsf{PI}\left(\right)}\right)}^{\frac{1}{2}}\right) + \left(\mathsf{neg}\left(\color{blue}{\mathsf{PI}\left(\right)}\right)\right)\right) \]
    19. add-log-expN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\log \left({\left(e^{\mathsf{PI}\left(\right)}\right)}^{\frac{1}{2}}\right) + \left(\mathsf{neg}\left(\color{blue}{\log \left(e^{\mathsf{PI}\left(\right)}\right)}\right)\right)\right) \]
  4. Applied rewrites8.5%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\cos^{-1} \left(\sqrt{\mathsf{fma}\left(-0.5, x, 0.5\right)}\right), 2, -0.5 \cdot \pi\right)} \]
  5. Add Preprocessing

Alternative 2: 5.3% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \mathsf{fma}\left(\cos^{-1} \left(\sqrt{0.5}\right), 2, -0.5 \cdot \pi\right) \end{array} \]
(FPCore (x) :precision binary64 (fma (acos (sqrt 0.5)) 2.0 (* -0.5 PI)))
double code(double x) {
	return fma(acos(sqrt(0.5)), 2.0, (-0.5 * ((double) M_PI)));
}
function code(x)
	return fma(acos(sqrt(0.5)), 2.0, Float64(-0.5 * pi))
end
code[x_] := N[(N[ArcCos[N[Sqrt[0.5], $MachinePrecision]], $MachinePrecision] * 2.0 + N[(-0.5 * Pi), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\mathsf{fma}\left(\cos^{-1} \left(\sqrt{0.5}\right), 2, -0.5 \cdot \pi\right)
\end{array}
Derivation
  1. Initial program 7.0%

    \[\frac{\pi}{2} - 2 \cdot \sin^{-1} \left(\sqrt{\frac{1 - x}{2}}\right) \]
  2. Applied rewrites8.5%

    \[\leadsto \frac{\pi}{2} - \color{blue}{\left(\pi + \left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, -0.5, 0.5\right)}\right)\right) \cdot 2\right)} \]
  3. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{\frac{\pi}{2} - \left(\pi + \left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right) \cdot 2\right)} \]
    2. lift-+.f64N/A

      \[\leadsto \frac{\pi}{2} - \color{blue}{\left(\pi + \left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right) \cdot 2\right)} \]
    3. associate--r+N/A

      \[\leadsto \color{blue}{\left(\frac{\pi}{2} - \pi\right) - \left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right) \cdot 2} \]
    4. sub-flipN/A

      \[\leadsto \color{blue}{\left(\frac{\pi}{2} - \pi\right) + \left(\mathsf{neg}\left(\left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right) \cdot 2\right)\right)} \]
    5. +-commutativeN/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(\left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right) \cdot 2\right)\right) + \left(\frac{\pi}{2} - \pi\right)} \]
    6. lift-*.f64N/A

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\left(-\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right) \cdot 2}\right)\right) + \left(\frac{\pi}{2} - \pi\right) \]
    7. lift-neg.f64N/A

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right)\right)\right)} \cdot 2\right)\right) + \left(\frac{\pi}{2} - \pi\right) \]
    8. distribute-lft-neg-outN/A

      \[\leadsto \left(\mathsf{neg}\left(\color{blue}{\left(\mathsf{neg}\left(\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2\right)\right)}\right)\right) + \left(\frac{\pi}{2} - \pi\right) \]
    9. remove-double-negN/A

      \[\leadsto \color{blue}{\cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2} + \left(\frac{\pi}{2} - \pi\right) \]
    10. sub-flipN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \color{blue}{\left(\frac{\pi}{2} + \left(\mathsf{neg}\left(\pi\right)\right)\right)} \]
    11. lift-/.f64N/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\color{blue}{\frac{\pi}{2}} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    12. mult-flipN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\color{blue}{\pi \cdot \frac{1}{2}} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    13. metadata-evalN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\pi \cdot \color{blue}{\frac{1}{2}} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    14. *-commutativeN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\color{blue}{\frac{1}{2} \cdot \pi} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    15. lift-PI.f64N/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\frac{1}{2} \cdot \color{blue}{\mathsf{PI}\left(\right)} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    16. add-log-expN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\frac{1}{2} \cdot \color{blue}{\log \left(e^{\mathsf{PI}\left(\right)}\right)} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    17. log-pow-revN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\color{blue}{\log \left({\left(e^{\mathsf{PI}\left(\right)}\right)}^{\frac{1}{2}}\right)} + \left(\mathsf{neg}\left(\pi\right)\right)\right) \]
    18. lift-PI.f64N/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\log \left({\left(e^{\mathsf{PI}\left(\right)}\right)}^{\frac{1}{2}}\right) + \left(\mathsf{neg}\left(\color{blue}{\mathsf{PI}\left(\right)}\right)\right)\right) \]
    19. add-log-expN/A

      \[\leadsto \cos^{-1} \left(\sqrt{\mathsf{fma}\left(x, \frac{-1}{2}, \frac{1}{2}\right)}\right) \cdot 2 + \left(\log \left({\left(e^{\mathsf{PI}\left(\right)}\right)}^{\frac{1}{2}}\right) + \left(\mathsf{neg}\left(\color{blue}{\log \left(e^{\mathsf{PI}\left(\right)}\right)}\right)\right)\right) \]
  4. Applied rewrites8.5%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\cos^{-1} \left(\sqrt{\mathsf{fma}\left(-0.5, x, 0.5\right)}\right), 2, -0.5 \cdot \pi\right)} \]
  5. Taylor expanded in x around 0

    \[\leadsto \mathsf{fma}\left(\cos^{-1} \left(\sqrt{\color{blue}{\frac{1}{2}}}\right), 2, \frac{-1}{2} \cdot \pi\right) \]
  6. Step-by-step derivation
    1. Applied rewrites5.3%

      \[\leadsto \mathsf{fma}\left(\cos^{-1} \left(\sqrt{\color{blue}{0.5}}\right), 2, -0.5 \cdot \pi\right) \]
    2. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 2025156 
    (FPCore (x)
      :name "Ian Simplification"
      :precision binary64
      (- (/ PI 2.0) (* 2.0 (asin (sqrt (/ (- 1.0 x) 2.0))))))