Average Error: 21.3 → 0.9
Time: 1.1s
Precision: binary64
\[\sqrt{x \cdot x + y} \]
\[\begin{array}{l} \mathbf{if}\;x \leq -1.2000405627480975 \cdot 10^{+154}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \leq 1.5782233992745074 \cdot 10^{+29}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) y)))
(FPCore (x y)
 :precision binary64
 (if (<= x -1.2000405627480975e+154)
   (- x)
   (if (<= x 1.5782233992745074e+29) (sqrt (fma x x y)) x)))
double code(double x, double y) {
	return sqrt(((x * x) + y));
}
double code(double x, double y) {
	double tmp;
	if (x <= -1.2000405627480975e+154) {
		tmp = -x;
	} else if (x <= 1.5782233992745074e+29) {
		tmp = sqrt(fma(x, x, y));
	} else {
		tmp = x;
	}
	return tmp;
}
function code(x, y)
	return sqrt(Float64(Float64(x * x) + y))
end
function code(x, y)
	tmp = 0.0
	if (x <= -1.2000405627480975e+154)
		tmp = Float64(-x);
	elseif (x <= 1.5782233992745074e+29)
		tmp = sqrt(fma(x, x, y));
	else
		tmp = x;
	end
	return tmp
end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision]
code[x_, y_] := If[LessEqual[x, -1.2000405627480975e+154], (-x), If[LessEqual[x, 1.5782233992745074e+29], N[Sqrt[N[(x * x + y), $MachinePrecision]], $MachinePrecision], x]]
\sqrt{x \cdot x + y}
\begin{array}{l}
\mathbf{if}\;x \leq -1.2000405627480975 \cdot 10^{+154}:\\
\;\;\;\;-x\\

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

\mathbf{else}:\\
\;\;\;\;x\\


\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original21.3
Target0.5
Herbie0.9
\[\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 < -1.2000405627480975e154

    1. Initial program 64.0

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

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

      \[\leadsto \color{blue}{-1 \cdot x} \]
    4. Simplified0

      \[\leadsto \color{blue}{-x} \]

    if -1.2000405627480975e154 < x < 1.57822339927450736e29

    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 1.57822339927450736e29 < x

    1. Initial program 38.4

      \[\sqrt{x \cdot x + y} \]
    2. Simplified38.4

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

      \[\leadsto \color{blue}{x} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification0.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -1.2000405627480975 \cdot 10^{+154}:\\ \;\;\;\;-x\\ \mathbf{elif}\;x \leq 1.5782233992745074 \cdot 10^{+29}:\\ \;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\ \mathbf{else}:\\ \;\;\;\;x\\ \end{array} \]

Reproduce

herbie shell --seed 2022150 
(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)))