Average Error: 15.2 → 0.2
Time: 7.6s
Precision: binary64
Cost: 26884
\[1 - \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(1, x\right)}\right)} \]
\[\begin{array}{l} \mathbf{if}\;\mathsf{hypot}\left(1, x\right) \leq 2:\\ \;\;\;\;\mathsf{fma}\left(-0.0859375, {x}^{4}, 0.125 \cdot \left(x \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}}{0.5 + \frac{-0.5}{\mathsf{hypot}\left(1, x\right)}}}\\ \end{array} \]
(FPCore (x)
 :precision binary64
 (- 1.0 (sqrt (* 0.5 (+ 1.0 (/ 1.0 (hypot 1.0 x)))))))
(FPCore (x)
 :precision binary64
 (if (<= (hypot 1.0 x) 2.0)
   (fma -0.0859375 (pow x 4.0) (* 0.125 (* x x)))
   (/
    1.0
    (/
     (+ 1.0 (sqrt (+ 0.5 (/ 0.5 (hypot 1.0 x)))))
     (+ 0.5 (/ -0.5 (hypot 1.0 x)))))))
double code(double x) {
	return 1.0 - sqrt((0.5 * (1.0 + (1.0 / hypot(1.0, x)))));
}
double code(double x) {
	double tmp;
	if (hypot(1.0, x) <= 2.0) {
		tmp = fma(-0.0859375, pow(x, 4.0), (0.125 * (x * x)));
	} else {
		tmp = 1.0 / ((1.0 + sqrt((0.5 + (0.5 / hypot(1.0, x))))) / (0.5 + (-0.5 / hypot(1.0, x))));
	}
	return tmp;
}
function code(x)
	return Float64(1.0 - sqrt(Float64(0.5 * Float64(1.0 + Float64(1.0 / hypot(1.0, x))))))
end
function code(x)
	tmp = 0.0
	if (hypot(1.0, x) <= 2.0)
		tmp = fma(-0.0859375, (x ^ 4.0), Float64(0.125 * Float64(x * x)));
	else
		tmp = Float64(1.0 / Float64(Float64(1.0 + sqrt(Float64(0.5 + Float64(0.5 / hypot(1.0, x))))) / Float64(0.5 + Float64(-0.5 / hypot(1.0, x)))));
	end
	return tmp
end
code[x_] := N[(1.0 - N[Sqrt[N[(0.5 * N[(1.0 + N[(1.0 / N[Sqrt[1.0 ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
code[x_] := If[LessEqual[N[Sqrt[1.0 ^ 2 + x ^ 2], $MachinePrecision], 2.0], N[(-0.0859375 * N[Power[x, 4.0], $MachinePrecision] + N[(0.125 * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(N[(1.0 + N[Sqrt[N[(0.5 + N[(0.5 / N[Sqrt[1.0 ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / N[(0.5 + N[(-0.5 / N[Sqrt[1.0 ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
1 - \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(1, x\right)}\right)}
\begin{array}{l}
\mathbf{if}\;\mathsf{hypot}\left(1, x\right) \leq 2:\\
\;\;\;\;\mathsf{fma}\left(-0.0859375, {x}^{4}, 0.125 \cdot \left(x \cdot x\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}}{0.5 + \frac{-0.5}{\mathsf{hypot}\left(1, x\right)}}}\\


\end{array}

Error

Derivation

  1. Split input into 2 regimes
  2. if (hypot.f64 1 x) < 2

    1. Initial program 29.4

      \[1 - \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(1, x\right)}\right)} \]
    2. Simplified29.4

      \[\leadsto \color{blue}{1 - \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}} \]
      Proof
      (-.f64 1 (sqrt.f64 (+.f64 1/2 (/.f64 1/2 (hypot.f64 1 x))))): 0 points increase in error, 0 points decrease in error
      (-.f64 1 (sqrt.f64 (+.f64 (Rewrite<= metadata-eval (*.f64 1 1/2)) (/.f64 1/2 (hypot.f64 1 x))))): 0 points increase in error, 0 points decrease in error
      (-.f64 1 (sqrt.f64 (+.f64 (*.f64 1 1/2) (/.f64 (Rewrite<= metadata-eval (*.f64 1 1/2)) (hypot.f64 1 x))))): 0 points increase in error, 0 points decrease in error
      (-.f64 1 (sqrt.f64 (+.f64 (*.f64 1 1/2) (Rewrite<= associate-*l/_binary64 (*.f64 (/.f64 1 (hypot.f64 1 x)) 1/2))))): 0 points increase in error, 0 points decrease in error
      (-.f64 1 (sqrt.f64 (Rewrite<= distribute-rgt-in_binary64 (*.f64 1/2 (+.f64 1 (/.f64 1 (hypot.f64 1 x))))))): 0 points increase in error, 0 points decrease in error
    3. Taylor expanded in x around 0 0.4

      \[\leadsto \color{blue}{0.125 \cdot {x}^{2} + -0.0859375 \cdot {x}^{4}} \]
    4. Simplified0.4

      \[\leadsto \color{blue}{\mathsf{fma}\left(-0.0859375, {x}^{4}, 0.125 \cdot \left(x \cdot x\right)\right)} \]
      Proof
      (fma.f64 -11/128 (pow.f64 x 4) (*.f64 1/8 (*.f64 x x))): 0 points increase in error, 0 points decrease in error
      (fma.f64 -11/128 (pow.f64 x 4) (*.f64 1/8 (Rewrite<= unpow2_binary64 (pow.f64 x 2)))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 -11/128 (pow.f64 x 4)) (*.f64 1/8 (pow.f64 x 2)))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= +-commutative_binary64 (+.f64 (*.f64 1/8 (pow.f64 x 2)) (*.f64 -11/128 (pow.f64 x 4)))): 0 points increase in error, 0 points decrease in error

    if 2 < (hypot.f64 1 x)

    1. Initial program 1.0

      \[1 - \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(1, x\right)}\right)} \]
    2. Simplified1.0

      \[\leadsto \color{blue}{1 - \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}} \]
      Proof
      (-.f64 1 (sqrt.f64 (+.f64 1/2 (/.f64 1/2 (hypot.f64 1 x))))): 0 points increase in error, 0 points decrease in error
      (-.f64 1 (sqrt.f64 (+.f64 (Rewrite<= metadata-eval (*.f64 1 1/2)) (/.f64 1/2 (hypot.f64 1 x))))): 0 points increase in error, 0 points decrease in error
      (-.f64 1 (sqrt.f64 (+.f64 (*.f64 1 1/2) (/.f64 (Rewrite<= metadata-eval (*.f64 1 1/2)) (hypot.f64 1 x))))): 0 points increase in error, 0 points decrease in error
      (-.f64 1 (sqrt.f64 (+.f64 (*.f64 1 1/2) (Rewrite<= associate-*l/_binary64 (*.f64 (/.f64 1 (hypot.f64 1 x)) 1/2))))): 0 points increase in error, 0 points decrease in error
      (-.f64 1 (sqrt.f64 (Rewrite<= distribute-rgt-in_binary64 (*.f64 1/2 (+.f64 1 (/.f64 1 (hypot.f64 1 x))))))): 0 points increase in error, 0 points decrease in error
    3. Applied egg-rr0.0

      \[\leadsto \color{blue}{\frac{1}{\frac{1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}}{0.5 - \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification0.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;\mathsf{hypot}\left(1, x\right) \leq 2:\\ \;\;\;\;\mathsf{fma}\left(-0.0859375, {x}^{4}, 0.125 \cdot \left(x \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}}{0.5 + \frac{-0.5}{\mathsf{hypot}\left(1, x\right)}}}\\ \end{array} \]

Alternatives

Alternative 1
Error0.2
Cost26756
\[\begin{array}{l} \mathbf{if}\;\mathsf{hypot}\left(1, x\right) \leq 2:\\ \;\;\;\;\mathsf{fma}\left(-0.0859375, {x}^{4}, 0.125 \cdot \left(x \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 + \frac{-0.5}{\mathsf{hypot}\left(1, x\right)}}{1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}}\\ \end{array} \]
Alternative 2
Error1.2
Cost13576
\[\begin{array}{l} \mathbf{if}\;x \leq -73890.4955567544:\\ \;\;\;\;\frac{1}{\frac{1 + \sqrt{0.5 + \frac{-0.5}{x}}}{0.5 + \frac{0.5}{x}}}\\ \mathbf{elif}\;x \leq 1.1182393929147128 \cdot 10^{-10}:\\ \;\;\;\;\mathsf{fma}\left(-0.0859375, {x}^{4}, 0.125 \cdot \left(x \cdot x\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5}{1 + \sqrt{0.5}}\\ \end{array} \]
Alternative 3
Error1.3
Cost7492
\[\begin{array}{l} \mathbf{if}\;x \leq -73890.4955567544:\\ \;\;\;\;\frac{1}{\frac{1 + \sqrt{0.5 + \frac{-0.5}{x}}}{0.5 + \frac{0.5}{x}}}\\ \mathbf{elif}\;x \leq 1.1182393929147128 \cdot 10^{-10}:\\ \;\;\;\;0.125 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5}{1 + \sqrt{0.5}}\\ \end{array} \]
Alternative 4
Error1.5
Cost7236
\[\begin{array}{l} \mathbf{if}\;x \leq -73890.4955567544:\\ \;\;\;\;1 - \sqrt{0.5 + \frac{0.5}{\frac{-0.5}{x} - x}}\\ \mathbf{elif}\;x \leq 1.1182393929147128 \cdot 10^{-10}:\\ \;\;\;\;0.125 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5}{1 + \sqrt{0.5}}\\ \end{array} \]
Alternative 5
Error1.4
Cost6984
\[\begin{array}{l} t_0 := \frac{0.5}{1 + \sqrt{0.5}}\\ \mathbf{if}\;x \leq -73890.4955567544:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.1182393929147128 \cdot 10^{-10}:\\ \;\;\;\;0.125 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error1.5
Cost6984
\[\begin{array}{l} \mathbf{if}\;x \leq -73890.4955567544:\\ \;\;\;\;1 - \sqrt{0.5 + \frac{-0.5}{x}}\\ \mathbf{elif}\;x \leq 1.1182393929147128 \cdot 10^{-10}:\\ \;\;\;\;0.125 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5}{1 + \sqrt{0.5}}\\ \end{array} \]
Alternative 7
Error1.9
Cost6856
\[\begin{array}{l} t_0 := 1 - \sqrt{0.5}\\ \mathbf{if}\;x \leq -73890.4955567544:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 1.1182393929147128 \cdot 10^{-10}:\\ \;\;\;\;0.125 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 8
Error26.5
Cost584
\[\begin{array}{l} \mathbf{if}\;x \leq -73890.4955567544:\\ \;\;\;\;0.18181818181818182\\ \mathbf{elif}\;x \leq 1.1182393929147128 \cdot 10^{-10}:\\ \;\;\;\;0.125 \cdot \left(x \cdot x\right)\\ \mathbf{else}:\\ \;\;\;\;0.18181818181818182\\ \end{array} \]
Alternative 9
Error26.4
Cost576
\[\frac{1}{5.5 + \frac{\frac{8}{x}}{x}} \]
Alternative 10
Error56.4
Cost64
\[0.18181818181818182 \]

Error

Reproduce

herbie shell --seed 2022293 
(FPCore (x)
  :name "Given's Rotation SVD example, simplified"
  :precision binary64
  (- 1.0 (sqrt (* 0.5 (+ 1.0 (/ 1.0 (hypot 1.0 x)))))))