Average Error: 21.7 → 0.1
Time: 1.3s
Precision: binary64
\[\sqrt{x \cdot x + y} \]
\[\begin{array}{l} t_0 := \mathsf{fma}\left(0.5, \frac{y}{x}, x\right)\\ \mathbf{if}\;x \leq -5.298076076378501 \cdot 10^{+152}:\\ \;\;\;\;-t_0\\ \mathbf{elif}\;x \leq 5.945564708482161 \cdot 10^{+108}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) y)))
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (fma 0.5 (/ y x) x)))
   (if (<= x -5.298076076378501e+152)
     (- t_0)
     (if (<= x 5.945564708482161e+108) (sqrt (fma x x y)) t_0))))
double code(double x, double y) {
	return sqrt(((x * x) + y));
}
double code(double x, double y) {
	double t_0 = fma(0.5, (y / x), x);
	double tmp;
	if (x <= -5.298076076378501e+152) {
		tmp = -t_0;
	} else if (x <= 5.945564708482161e+108) {
		tmp = sqrt(fma(x, x, y));
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(x, y)
	return sqrt(Float64(Float64(x * x) + y))
end
function code(x, y)
	t_0 = fma(0.5, Float64(y / x), x)
	tmp = 0.0
	if (x <= -5.298076076378501e+152)
		tmp = Float64(-t_0);
	elseif (x <= 5.945564708482161e+108)
		tmp = sqrt(fma(x, x, y));
	else
		tmp = t_0;
	end
	return tmp
end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision]
code[x_, y_] := Block[{t$95$0 = N[(0.5 * N[(y / x), $MachinePrecision] + x), $MachinePrecision]}, If[LessEqual[x, -5.298076076378501e+152], (-t$95$0), If[LessEqual[x, 5.945564708482161e+108], N[Sqrt[N[(x * x + y), $MachinePrecision]], $MachinePrecision], t$95$0]]]
\sqrt{x \cdot x + y}
\begin{array}{l}
t_0 := \mathsf{fma}\left(0.5, \frac{y}{x}, x\right)\\
\mathbf{if}\;x \leq -5.298076076378501 \cdot 10^{+152}:\\
\;\;\;\;-t_0\\

\mathbf{elif}\;x \leq 5.945564708482161 \cdot 10^{+108}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original21.7
Target0.5
Herbie0.1
\[\begin{array}{l} \mathbf{if}\;x < -1.5097698010472593 \cdot 10^{+153}:\\ \;\;\;\;-\left(0.5 \cdot \frac{y}{x} + x\right)\\ \mathbf{elif}\;x < 5.582399551122541 \cdot 10^{+57}:\\ \;\;\;\;\sqrt{x \cdot x + y}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{y}{x} + x\\ \end{array} \]

Derivation

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

    1. Initial program 63.5

      \[\sqrt{x \cdot x + y} \]
    2. Simplified63.5

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, x, y\right)}} \]
    3. Taylor expanded in x around -inf 0

      \[\leadsto \color{blue}{-\left(0.5 \cdot \frac{y}{x} + x\right)} \]
    4. Simplified0

      \[\leadsto \color{blue}{-\mathsf{fma}\left(0.5, \frac{y}{x}, x\right)} \]

    if -5.29807607637850107e152 < x < 5.94556470848216066e108

    1. Initial program 0.0

      \[\sqrt{x \cdot x + y} \]
    2. Simplified0.0

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, x, y\right)}} \]

    if 5.94556470848216066e108 < x

    1. Initial program 50.0

      \[\sqrt{x \cdot x + y} \]
    2. Simplified50.0

      \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(x, x, y\right)}} \]
    3. Taylor expanded in x around inf 0.5

      \[\leadsto \color{blue}{0.5 \cdot \frac{y}{x} + x} \]
    4. Simplified0.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -5.298076076378501 \cdot 10^{+152}:\\ \;\;\;\;-\mathsf{fma}\left(0.5, \frac{y}{x}, x\right)\\ \mathbf{elif}\;x \leq 5.945564708482161 \cdot 10^{+108}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(0.5, \frac{y}{x}, x\right)\\ \end{array} \]

Reproduce

herbie shell --seed 2022131 
(FPCore (x y)
  :name "Linear.Quaternion:$clog from linear-1.19.1.3"
  :precision binary64

  :herbie-target
  (if (< x -1.5097698010472593e+153) (- (+ (* 0.5 (/ y x)) x)) (if (< x 5.582399551122541e+57) (sqrt (+ (* x x) y)) (+ (* 0.5 (/ y x)) x)))

  (sqrt (+ (* x x) y)))