
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) y)))
double code(double x, double y) {
return sqrt(((x * x) + y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt(((x * x) + y))
end function
public static double code(double x, double y) {
return Math.sqrt(((x * x) + y));
}
def code(x, y): return math.sqrt(((x * x) + y))
function code(x, y) return sqrt(Float64(Float64(x * x) + y)) end
function tmp = code(x, y) tmp = sqrt(((x * x) + y)); end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + y}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (sqrt (+ (* x x) y)))
double code(double x, double y) {
return sqrt(((x * x) + y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = sqrt(((x * x) + y))
end function
public static double code(double x, double y) {
return Math.sqrt(((x * x) + y));
}
def code(x, y): return math.sqrt(((x * x) + y))
function code(x, y) return sqrt(Float64(Float64(x * x) + y)) end
function tmp = code(x, y) tmp = sqrt(((x * x) + y)); end
code[x_, y_] := N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\sqrt{x \cdot x + y}
\end{array}
(FPCore (x y) :precision binary64 (if (<= x -1e+155) (- x) (if (<= x 2e+146) (sqrt (+ (* x x) y)) x)))
double code(double x, double y) {
double tmp;
if (x <= -1e+155) {
tmp = -x;
} else if (x <= 2e+146) {
tmp = sqrt(((x * x) + y));
} else {
tmp = x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-1d+155)) then
tmp = -x
else if (x <= 2d+146) then
tmp = sqrt(((x * x) + y))
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1e+155) {
tmp = -x;
} else if (x <= 2e+146) {
tmp = Math.sqrt(((x * x) + y));
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1e+155: tmp = -x elif x <= 2e+146: tmp = math.sqrt(((x * x) + y)) else: tmp = x return tmp
function code(x, y) tmp = 0.0 if (x <= -1e+155) tmp = Float64(-x); elseif (x <= 2e+146) tmp = sqrt(Float64(Float64(x * x) + y)); else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1e+155) tmp = -x; elseif (x <= 2e+146) tmp = sqrt(((x * x) + y)); else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1e+155], (-x), If[LessEqual[x, 2e+146], N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], x]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \cdot 10^{+155}:\\
\;\;\;\;-x\\
\mathbf{elif}\;x \leq 2 \cdot 10^{+146}:\\
\;\;\;\;\sqrt{x \cdot x + y}\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < -1.00000000000000001e155Initial program 6.8%
Taylor expanded in x around -inf 100.0%
mul-1-neg100.0%
Simplified100.0%
if -1.00000000000000001e155 < x < 1.99999999999999987e146Initial program 100.0%
if 1.99999999999999987e146 < x Initial program 11.2%
Taylor expanded in x around inf 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= x -3.6e-46) (- (* (/ y x) -0.5) x) (if (<= x 6.2e-100) (sqrt y) (+ x (* (/ y x) 0.5)))))
double code(double x, double y) {
double tmp;
if (x <= -3.6e-46) {
tmp = ((y / x) * -0.5) - x;
} else if (x <= 6.2e-100) {
tmp = sqrt(y);
} else {
tmp = x + ((y / x) * 0.5);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-3.6d-46)) then
tmp = ((y / x) * (-0.5d0)) - x
else if (x <= 6.2d-100) then
tmp = sqrt(y)
else
tmp = x + ((y / x) * 0.5d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -3.6e-46) {
tmp = ((y / x) * -0.5) - x;
} else if (x <= 6.2e-100) {
tmp = Math.sqrt(y);
} else {
tmp = x + ((y / x) * 0.5);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -3.6e-46: tmp = ((y / x) * -0.5) - x elif x <= 6.2e-100: tmp = math.sqrt(y) else: tmp = x + ((y / x) * 0.5) return tmp
function code(x, y) tmp = 0.0 if (x <= -3.6e-46) tmp = Float64(Float64(Float64(y / x) * -0.5) - x); elseif (x <= 6.2e-100) tmp = sqrt(y); else tmp = Float64(x + Float64(Float64(y / x) * 0.5)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -3.6e-46) tmp = ((y / x) * -0.5) - x; elseif (x <= 6.2e-100) tmp = sqrt(y); else tmp = x + ((y / x) * 0.5); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -3.6e-46], N[(N[(N[(y / x), $MachinePrecision] * -0.5), $MachinePrecision] - x), $MachinePrecision], If[LessEqual[x, 6.2e-100], N[Sqrt[y], $MachinePrecision], N[(x + N[(N[(y / x), $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.6 \cdot 10^{-46}:\\
\;\;\;\;\frac{y}{x} \cdot -0.5 - x\\
\mathbf{elif}\;x \leq 6.2 \cdot 10^{-100}:\\
\;\;\;\;\sqrt{y}\\
\mathbf{else}:\\
\;\;\;\;x + \frac{y}{x} \cdot 0.5\\
\end{array}
\end{array}
if x < -3.6e-46Initial program 57.7%
Taylor expanded in x around -inf 88.1%
mul-1-neg88.1%
unsub-neg88.1%
*-commutative88.1%
Simplified88.1%
if -3.6e-46 < x < 6.1999999999999997e-100Initial program 100.0%
Taylor expanded in x around 0 90.1%
if 6.1999999999999997e-100 < x Initial program 52.9%
Taylor expanded in x around inf 91.5%
Final simplification90.0%
(FPCore (x y) :precision binary64 (if (<= x -5e-310) (- x) (+ x (* (/ y x) 0.5))))
double code(double x, double y) {
double tmp;
if (x <= -5e-310) {
tmp = -x;
} else {
tmp = x + ((y / x) * 0.5);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-5d-310)) then
tmp = -x
else
tmp = x + ((y / x) * 0.5d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -5e-310) {
tmp = -x;
} else {
tmp = x + ((y / x) * 0.5);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -5e-310: tmp = -x else: tmp = x + ((y / x) * 0.5) return tmp
function code(x, y) tmp = 0.0 if (x <= -5e-310) tmp = Float64(-x); else tmp = Float64(x + Float64(Float64(y / x) * 0.5)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -5e-310) tmp = -x; else tmp = x + ((y / x) * 0.5); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -5e-310], (-x), N[(x + N[(N[(y / x), $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-310}:\\
\;\;\;\;-x\\
\mathbf{else}:\\
\;\;\;\;x + \frac{y}{x} \cdot 0.5\\
\end{array}
\end{array}
if x < -4.999999999999985e-310Initial program 71.1%
Taylor expanded in x around -inf 66.0%
mul-1-neg66.0%
Simplified66.0%
if -4.999999999999985e-310 < x Initial program 64.5%
Taylor expanded in x around inf 71.1%
Final simplification68.6%
(FPCore (x y) :precision binary64 (if (<= x -5e-310) (- (* (/ y x) -0.5) x) (+ x (* (/ y x) 0.5))))
double code(double x, double y) {
double tmp;
if (x <= -5e-310) {
tmp = ((y / x) * -0.5) - x;
} else {
tmp = x + ((y / x) * 0.5);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-5d-310)) then
tmp = ((y / x) * (-0.5d0)) - x
else
tmp = x + ((y / x) * 0.5d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -5e-310) {
tmp = ((y / x) * -0.5) - x;
} else {
tmp = x + ((y / x) * 0.5);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -5e-310: tmp = ((y / x) * -0.5) - x else: tmp = x + ((y / x) * 0.5) return tmp
function code(x, y) tmp = 0.0 if (x <= -5e-310) tmp = Float64(Float64(Float64(y / x) * -0.5) - x); else tmp = Float64(x + Float64(Float64(y / x) * 0.5)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -5e-310) tmp = ((y / x) * -0.5) - x; else tmp = x + ((y / x) * 0.5); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -5e-310], N[(N[(N[(y / x), $MachinePrecision] * -0.5), $MachinePrecision] - x), $MachinePrecision], N[(x + N[(N[(y / x), $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-310}:\\
\;\;\;\;\frac{y}{x} \cdot -0.5 - x\\
\mathbf{else}:\\
\;\;\;\;x + \frac{y}{x} \cdot 0.5\\
\end{array}
\end{array}
if x < -4.999999999999985e-310Initial program 71.1%
Taylor expanded in x around -inf 66.2%
mul-1-neg66.2%
unsub-neg66.2%
*-commutative66.2%
Simplified66.2%
if -4.999999999999985e-310 < x Initial program 64.5%
Taylor expanded in x around inf 71.1%
Final simplification68.7%
(FPCore (x y) :precision binary64 (if (<= x -5e-310) (- x) x))
double code(double x, double y) {
double tmp;
if (x <= -5e-310) {
tmp = -x;
} else {
tmp = x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-5d-310)) then
tmp = -x
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -5e-310) {
tmp = -x;
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -5e-310: tmp = -x else: tmp = x return tmp
function code(x, y) tmp = 0.0 if (x <= -5e-310) tmp = Float64(-x); else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -5e-310) tmp = -x; else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -5e-310], (-x), x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-310}:\\
\;\;\;\;-x\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < -4.999999999999985e-310Initial program 71.1%
Taylor expanded in x around -inf 66.0%
mul-1-neg66.0%
Simplified66.0%
if -4.999999999999985e-310 < x Initial program 64.5%
Taylor expanded in x around inf 71.0%
Final simplification68.5%
(FPCore (x y) :precision binary64 x)
double code(double x, double y) {
return x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x
end function
public static double code(double x, double y) {
return x;
}
def code(x, y): return x
function code(x, y) return x end
function tmp = code(x, y) tmp = x; end
code[x_, y_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 67.8%
Taylor expanded in x around inf 36.7%
Final simplification36.7%
(FPCore (x y)
:precision binary64
(let* ((t_0 (+ (* 0.5 (/ y x)) x)))
(if (< x -1.5097698010472593e+153)
(- t_0)
(if (< x 5.582399551122541e+57) (sqrt (+ (* x x) y)) t_0))))
double code(double x, double y) {
double t_0 = (0.5 * (y / x)) + x;
double tmp;
if (x < -1.5097698010472593e+153) {
tmp = -t_0;
} else if (x < 5.582399551122541e+57) {
tmp = sqrt(((x * x) + y));
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: tmp
t_0 = (0.5d0 * (y / x)) + x
if (x < (-1.5097698010472593d+153)) then
tmp = -t_0
else if (x < 5.582399551122541d+57) then
tmp = sqrt(((x * x) + y))
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = (0.5 * (y / x)) + x;
double tmp;
if (x < -1.5097698010472593e+153) {
tmp = -t_0;
} else if (x < 5.582399551122541e+57) {
tmp = Math.sqrt(((x * x) + y));
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = (0.5 * (y / x)) + x tmp = 0 if x < -1.5097698010472593e+153: tmp = -t_0 elif x < 5.582399551122541e+57: tmp = math.sqrt(((x * x) + y)) else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(Float64(0.5 * Float64(y / x)) + x) tmp = 0.0 if (x < -1.5097698010472593e+153) tmp = Float64(-t_0); elseif (x < 5.582399551122541e+57) tmp = sqrt(Float64(Float64(x * x) + y)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = (0.5 * (y / x)) + x; tmp = 0.0; if (x < -1.5097698010472593e+153) tmp = -t_0; elseif (x < 5.582399551122541e+57) tmp = sqrt(((x * x) + y)); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(0.5 * N[(y / x), $MachinePrecision]), $MachinePrecision] + x), $MachinePrecision]}, If[Less[x, -1.5097698010472593e+153], (-t$95$0), If[Less[x, 5.582399551122541e+57], N[Sqrt[N[(N[(x * x), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \frac{y}{x} + x\\
\mathbf{if}\;x < -1.5097698010472593 \cdot 10^{+153}:\\
\;\;\;\;-t_0\\
\mathbf{elif}\;x < 5.582399551122541 \cdot 10^{+57}:\\
\;\;\;\;\sqrt{x \cdot x + y}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
herbie shell --seed 2023175
(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)))