?

Average Error: 23.79% → 0.09%
Time: 10.9s
Precision: binary64
Cost: 20932

?

\[1 - \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(1, x\right)}\right)} \]
\[\begin{array}{l} t_0 := x \cdot x + 1\\ t_1 := 0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}\\ t_2 := 1 + \sqrt{t_1}\\ \mathbf{if}\;x \leq -0.0019:\\ \;\;\;\;\frac{0.25 + \frac{-0.25}{t_0}}{t_1 \cdot t_2}\\ \mathbf{elif}\;x \leq 0.00195:\\ \;\;\;\;\mathsf{fma}\left(x \cdot 0.125, x, -0.0859375 \cdot {x}^{4}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 - \sqrt{\frac{0.25}{t_0}}}{t_2}\\ \end{array} \]
(FPCore (x)
 :precision binary64
 (- 1.0 (sqrt (* 0.5 (+ 1.0 (/ 1.0 (hypot 1.0 x)))))))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (+ (* x x) 1.0))
        (t_1 (+ 0.5 (/ 0.5 (hypot 1.0 x))))
        (t_2 (+ 1.0 (sqrt t_1))))
   (if (<= x -0.0019)
     (/ (+ 0.25 (/ -0.25 t_0)) (* t_1 t_2))
     (if (<= x 0.00195)
       (fma (* x 0.125) x (* -0.0859375 (pow x 4.0)))
       (/ (- 0.5 (sqrt (/ 0.25 t_0))) t_2)))))
double code(double x) {
	return 1.0 - sqrt((0.5 * (1.0 + (1.0 / hypot(1.0, x)))));
}
double code(double x) {
	double t_0 = (x * x) + 1.0;
	double t_1 = 0.5 + (0.5 / hypot(1.0, x));
	double t_2 = 1.0 + sqrt(t_1);
	double tmp;
	if (x <= -0.0019) {
		tmp = (0.25 + (-0.25 / t_0)) / (t_1 * t_2);
	} else if (x <= 0.00195) {
		tmp = fma((x * 0.125), x, (-0.0859375 * pow(x, 4.0)));
	} else {
		tmp = (0.5 - sqrt((0.25 / t_0))) / t_2;
	}
	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)
	t_0 = Float64(Float64(x * x) + 1.0)
	t_1 = Float64(0.5 + Float64(0.5 / hypot(1.0, x)))
	t_2 = Float64(1.0 + sqrt(t_1))
	tmp = 0.0
	if (x <= -0.0019)
		tmp = Float64(Float64(0.25 + Float64(-0.25 / t_0)) / Float64(t_1 * t_2));
	elseif (x <= 0.00195)
		tmp = fma(Float64(x * 0.125), x, Float64(-0.0859375 * (x ^ 4.0)));
	else
		tmp = Float64(Float64(0.5 - sqrt(Float64(0.25 / t_0))) / t_2);
	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_] := Block[{t$95$0 = N[(N[(x * x), $MachinePrecision] + 1.0), $MachinePrecision]}, Block[{t$95$1 = N[(0.5 + N[(0.5 / N[Sqrt[1.0 ^ 2 + x ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(1.0 + N[Sqrt[t$95$1], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -0.0019], N[(N[(0.25 + N[(-0.25 / t$95$0), $MachinePrecision]), $MachinePrecision] / N[(t$95$1 * t$95$2), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 0.00195], N[(N[(x * 0.125), $MachinePrecision] * x + N[(-0.0859375 * N[Power[x, 4.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(0.5 - N[Sqrt[N[(0.25 / t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] / t$95$2), $MachinePrecision]]]]]]
1 - \sqrt{0.5 \cdot \left(1 + \frac{1}{\mathsf{hypot}\left(1, x\right)}\right)}
\begin{array}{l}
t_0 := x \cdot x + 1\\
t_1 := 0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}\\
t_2 := 1 + \sqrt{t_1}\\
\mathbf{if}\;x \leq -0.0019:\\
\;\;\;\;\frac{0.25 + \frac{-0.25}{t_0}}{t_1 \cdot t_2}\\

\mathbf{elif}\;x \leq 0.00195:\\
\;\;\;\;\mathsf{fma}\left(x \cdot 0.125, x, -0.0859375 \cdot {x}^{4}\right)\\

\mathbf{else}:\\
\;\;\;\;\frac{0.5 - \sqrt{\frac{0.25}{t_0}}}{t_2}\\


\end{array}

Error?

Derivation?

  1. Split input into 3 regimes
  2. if x < -0.0019

    1. Initial program 1.64

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

      \[\leadsto \color{blue}{1 - \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}} \]
      Proof

      [Start]1.64

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

      distribute-lft-in [=>]1.64

      \[ 1 - \sqrt{\color{blue}{0.5 \cdot 1 + 0.5 \cdot \frac{1}{\mathsf{hypot}\left(1, x\right)}}} \]

      metadata-eval [=>]1.64

      \[ 1 - \sqrt{\color{blue}{0.5} + 0.5 \cdot \frac{1}{\mathsf{hypot}\left(1, x\right)}} \]

      associate-*r/ [=>]1.64

      \[ 1 - \sqrt{0.5 + \color{blue}{\frac{0.5 \cdot 1}{\mathsf{hypot}\left(1, x\right)}}} \]

      metadata-eval [=>]1.64

      \[ 1 - \sqrt{0.5 + \frac{\color{blue}{0.5}}{\mathsf{hypot}\left(1, x\right)}} \]
    3. Applied egg-rr0.14

      \[\leadsto \color{blue}{\frac{0.25 - \frac{0.25}{1 + x \cdot x}}{\left(1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}\right) \cdot \left(0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}\right)}} \]
    4. Simplified0.14

      \[\leadsto \color{blue}{\frac{0.25 - \frac{0.25}{x \cdot x + 1}}{\left(0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}\right) \cdot \left(1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}\right)}} \]
      Proof

      [Start]0.14

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

      +-commutative [=>]0.14

      \[ \frac{0.25 - \frac{0.25}{\color{blue}{x \cdot x + 1}}}{\left(1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}\right) \cdot \left(0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}\right)} \]

      *-commutative [=>]0.14

      \[ \frac{0.25 - \frac{0.25}{x \cdot x + 1}}{\color{blue}{\left(0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}\right) \cdot \left(1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}\right)}} \]

    if -0.0019 < x < 0.0019499999999999999

    1. Initial program 46.45

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

      \[\leadsto \color{blue}{1 - \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}} \]
      Proof

      [Start]46.45

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

      distribute-lft-in [=>]46.45

      \[ 1 - \sqrt{\color{blue}{0.5 \cdot 1 + 0.5 \cdot \frac{1}{\mathsf{hypot}\left(1, x\right)}}} \]

      metadata-eval [=>]46.45

      \[ 1 - \sqrt{\color{blue}{0.5} + 0.5 \cdot \frac{1}{\mathsf{hypot}\left(1, x\right)}} \]

      associate-*r/ [=>]46.45

      \[ 1 - \sqrt{0.5 + \color{blue}{\frac{0.5 \cdot 1}{\mathsf{hypot}\left(1, x\right)}}} \]

      metadata-eval [=>]46.45

      \[ 1 - \sqrt{0.5 + \frac{\color{blue}{0.5}}{\mathsf{hypot}\left(1, x\right)}} \]
    3. Taylor expanded in x around 0 0.07

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.125, x \cdot x, -0.0859375 \cdot {x}^{4}\right)} \]
      Proof

      [Start]0.07

      \[ 0.125 \cdot {x}^{2} + -0.0859375 \cdot {x}^{4} \]

      fma-def [=>]0.07

      \[ \color{blue}{\mathsf{fma}\left(0.125, {x}^{2}, -0.0859375 \cdot {x}^{4}\right)} \]

      unpow2 [=>]0.07

      \[ \mathsf{fma}\left(0.125, \color{blue}{x \cdot x}, -0.0859375 \cdot {x}^{4}\right) \]
    5. Applied egg-rr0.07

      \[\leadsto \color{blue}{\left(0.125 \cdot x\right) \cdot x + -0.0859375 \cdot {x}^{4}} \]
    6. Applied egg-rr0.06

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.125 \cdot x, x, -0.0859375 \cdot {x}^{4}\right)} \]

    if 0.0019499999999999999 < x

    1. Initial program 1.57

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

      \[\leadsto \color{blue}{1 - \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}} \]
      Proof

      [Start]1.57

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

      distribute-lft-in [=>]1.57

      \[ 1 - \sqrt{\color{blue}{0.5 \cdot 1 + 0.5 \cdot \frac{1}{\mathsf{hypot}\left(1, x\right)}}} \]

      metadata-eval [=>]1.57

      \[ 1 - \sqrt{\color{blue}{0.5} + 0.5 \cdot \frac{1}{\mathsf{hypot}\left(1, x\right)}} \]

      associate-*r/ [=>]1.57

      \[ 1 - \sqrt{0.5 + \color{blue}{\frac{0.5 \cdot 1}{\mathsf{hypot}\left(1, x\right)}}} \]

      metadata-eval [=>]1.57

      \[ 1 - \sqrt{0.5 + \frac{\color{blue}{0.5}}{\mathsf{hypot}\left(1, x\right)}} \]
    3. Applied egg-rr1.58

      \[\leadsto \color{blue}{\frac{1 - \left(-\sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}\right) \cdot \left(-\sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}\right)}{1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}}} \]
    4. Simplified0.08

      \[\leadsto \color{blue}{\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)}}}} \]
      Proof

      [Start]1.58

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

      sqr-neg [=>]1.58

      \[ \frac{1 - \color{blue}{\sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}} \cdot \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}}}{1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}} \]

      rem-square-sqrt [=>]0.1

      \[ \frac{1 - \color{blue}{\left(0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}\right)}}{1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}} \]

      associate--r+ [=>]0.08

      \[ \frac{\color{blue}{\left(1 - 0.5\right) - \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}}{1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}} \]

      metadata-eval [=>]0.08

      \[ \frac{\color{blue}{0.5} - \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}{1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}} \]
    5. Applied egg-rr0.08

      \[\leadsto \frac{0.5 - \color{blue}{\sqrt{\frac{0.25}{1 + x \cdot x}}}}{1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}} \]
    6. Simplified0.08

      \[\leadsto \frac{0.5 - \color{blue}{\sqrt{\frac{0.25}{x \cdot x + 1}}}}{1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}} \]
      Proof

      [Start]0.08

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

      +-commutative [=>]0.08

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

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

Alternatives

Alternative 1
Error0.11%
Cost26948
\[\begin{array}{l} \mathbf{if}\;\mathsf{hypot}\left(1, x\right) \leq 1.001:\\ \;\;\;\;\mathsf{fma}\left(x \cdot 0.125, x, -0.0859375 \cdot {x}^{4}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 - \sqrt{\frac{0.25}{x \cdot x + 1}}}{1 + \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}}\\ \end{array} \]
Alternative 2
Error0.11%
Cost26756
\[\begin{array}{l} t_0 := \frac{0.5}{\mathsf{hypot}\left(1, x\right)}\\ \mathbf{if}\;\mathsf{hypot}\left(1, x\right) \leq 1.001:\\ \;\;\;\;\mathsf{fma}\left(x \cdot 0.125, x, -0.0859375 \cdot {x}^{4}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 - t_0}{1 + \sqrt{0.5 + t_0}}\\ \end{array} \]
Alternative 3
Error1.07%
Cost13828
\[\begin{array}{l} t_0 := \frac{0.5}{\mathsf{hypot}\left(1, x\right)}\\ \mathbf{if}\;x \leq -1:\\ \;\;\;\;\frac{0.5 - t_0}{1 + \sqrt{0.5 + \frac{0.5}{x}}}\\ \mathbf{elif}\;x \leq 0.00275:\\ \;\;\;\;\mathsf{fma}\left(x \cdot 0.125, x, -0.0859375 \cdot {x}^{4}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \sqrt{0.5 + t_0}\\ \end{array} \]
Alternative 4
Error1.1%
Cost13576
\[\begin{array}{l} \mathbf{if}\;x \leq -1.1:\\ \;\;\;\;\frac{0.5}{1 + \sqrt{0.5}}\\ \mathbf{elif}\;x \leq 0.00275:\\ \;\;\;\;-0.0859375 \cdot {x}^{4} + x \cdot \left(x \cdot 0.125\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}\\ \end{array} \]
Alternative 5
Error1.1%
Cost13576
\[\begin{array}{l} \mathbf{if}\;x \leq -1.1:\\ \;\;\;\;\frac{0.5}{1 + \sqrt{0.5}}\\ \mathbf{elif}\;x \leq 0.00275:\\ \;\;\;\;\mathsf{fma}\left(x \cdot 0.125, x, -0.0859375 \cdot {x}^{4}\right)\\ \mathbf{else}:\\ \;\;\;\;1 - \sqrt{0.5 + \frac{0.5}{\mathsf{hypot}\left(1, x\right)}}\\ \end{array} \]
Alternative 6
Error1.39%
Cost7305
\[\begin{array}{l} \mathbf{if}\;x \leq -1.1 \lor \neg \left(x \leq 1.1\right):\\ \;\;\;\;\frac{0.5}{1 + \sqrt{0.5}}\\ \mathbf{else}:\\ \;\;\;\;-0.0859375 \cdot {x}^{4} + x \cdot \left(x \cdot 0.125\right)\\ \end{array} \]
Alternative 7
Error1.39%
Cost6985
\[\begin{array}{l} \mathbf{if}\;x \leq -1.1 \lor \neg \left(x \leq 1.1\right):\\ \;\;\;\;\frac{0.5}{1 + \sqrt{0.5}}\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot x\right) \cdot \left(0.125 + \left(x \cdot x\right) \cdot -0.0859375\right)\\ \end{array} \]
Alternative 8
Error2.13%
Cost6857
\[\begin{array}{l} \mathbf{if}\;x \leq -1.1 \lor \neg \left(x \leq 1.1\right):\\ \;\;\;\;1 - \sqrt{0.5}\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot x\right) \cdot \left(0.125 + \left(x \cdot x\right) \cdot -0.0859375\right)\\ \end{array} \]
Alternative 9
Error41.18%
Cost969
\[\begin{array}{l} \mathbf{if}\;x \leq -0.68 \lor \neg \left(x \leq 0.66\right):\\ \;\;\;\;\frac{0.25 + \frac{-0.25}{x \cdot x + 1}}{2}\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot x\right) \cdot \left(0.125 + \left(x \cdot x\right) \cdot -0.0859375\right)\\ \end{array} \]
Alternative 10
Error49.3%
Cost320
\[\left(x \cdot x\right) \cdot 0.125 \]
Alternative 11
Error49.3%
Cost320
\[x \cdot \left(x \cdot 0.125\right) \]
Alternative 12
Error72.26%
Cost64
\[0 \]

Error

Reproduce?

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