
(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 7 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 -2e+154) (- x) (if (<= x 1.95e+111) (sqrt (fma x x y)) x)))
double code(double x, double y) {
double tmp;
if (x <= -2e+154) {
tmp = -x;
} else if (x <= 1.95e+111) {
tmp = sqrt(fma(x, x, y));
} else {
tmp = x;
}
return tmp;
}
function code(x, y) tmp = 0.0 if (x <= -2e+154) tmp = Float64(-x); elseif (x <= 1.95e+111) tmp = sqrt(fma(x, x, y)); else tmp = x; end return tmp end
code[x_, y_] := If[LessEqual[x, -2e+154], (-x), If[LessEqual[x, 1.95e+111], N[Sqrt[N[(x * x + y), $MachinePrecision]], $MachinePrecision], x]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{+154}:\\
\;\;\;\;-x\\
\mathbf{elif}\;x \leq 1.95 \cdot 10^{+111}:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(x, x, y\right)}\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < -2.00000000000000007e154Initial program 6.6%
Taylor expanded in x around -inf 100.0%
mul-1-neg100.0%
Simplified100.0%
if -2.00000000000000007e154 < x < 1.9499999999999999e111Initial program 100.0%
fma-def100.0%
Simplified100.0%
if 1.9499999999999999e111 < x Initial program 26.7%
Taylor expanded in x around inf 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= x -2e+154) (- x) (if (<= x 1e+111) (sqrt (+ y (* x x))) x)))
double code(double x, double y) {
double tmp;
if (x <= -2e+154) {
tmp = -x;
} else if (x <= 1e+111) {
tmp = sqrt((y + (x * 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 <= (-2d+154)) then
tmp = -x
else if (x <= 1d+111) then
tmp = sqrt((y + (x * x)))
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -2e+154) {
tmp = -x;
} else if (x <= 1e+111) {
tmp = Math.sqrt((y + (x * x)));
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2e+154: tmp = -x elif x <= 1e+111: tmp = math.sqrt((y + (x * x))) else: tmp = x return tmp
function code(x, y) tmp = 0.0 if (x <= -2e+154) tmp = Float64(-x); elseif (x <= 1e+111) tmp = sqrt(Float64(y + Float64(x * x))); else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -2e+154) tmp = -x; elseif (x <= 1e+111) tmp = sqrt((y + (x * x))); else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2e+154], (-x), If[LessEqual[x, 1e+111], N[Sqrt[N[(y + N[(x * x), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], x]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{+154}:\\
\;\;\;\;-x\\
\mathbf{elif}\;x \leq 10^{+111}:\\
\;\;\;\;\sqrt{y + x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < -2.00000000000000007e154Initial program 6.6%
Taylor expanded in x around -inf 100.0%
mul-1-neg100.0%
Simplified100.0%
if -2.00000000000000007e154 < x < 9.99999999999999957e110Initial program 100.0%
if 9.99999999999999957e110 < x Initial program 26.7%
Taylor expanded in x around inf 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= x -8.4e-49) (- (* y (/ -0.5 x)) x) (if (<= x 1.9e-10) (sqrt y) (+ x (* 0.5 (/ y x))))))
double code(double x, double y) {
double tmp;
if (x <= -8.4e-49) {
tmp = (y * (-0.5 / x)) - x;
} else if (x <= 1.9e-10) {
tmp = sqrt(y);
} else {
tmp = x + (0.5 * (y / x));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-8.4d-49)) then
tmp = (y * ((-0.5d0) / x)) - x
else if (x <= 1.9d-10) then
tmp = sqrt(y)
else
tmp = x + (0.5d0 * (y / x))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -8.4e-49) {
tmp = (y * (-0.5 / x)) - x;
} else if (x <= 1.9e-10) {
tmp = Math.sqrt(y);
} else {
tmp = x + (0.5 * (y / x));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -8.4e-49: tmp = (y * (-0.5 / x)) - x elif x <= 1.9e-10: tmp = math.sqrt(y) else: tmp = x + (0.5 * (y / x)) return tmp
function code(x, y) tmp = 0.0 if (x <= -8.4e-49) tmp = Float64(Float64(y * Float64(-0.5 / x)) - x); elseif (x <= 1.9e-10) tmp = sqrt(y); else tmp = Float64(x + Float64(0.5 * Float64(y / x))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -8.4e-49) tmp = (y * (-0.5 / x)) - x; elseif (x <= 1.9e-10) tmp = sqrt(y); else tmp = x + (0.5 * (y / x)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -8.4e-49], N[(N[(y * N[(-0.5 / x), $MachinePrecision]), $MachinePrecision] - x), $MachinePrecision], If[LessEqual[x, 1.9e-10], N[Sqrt[y], $MachinePrecision], N[(x + N[(0.5 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -8.4 \cdot 10^{-49}:\\
\;\;\;\;y \cdot \frac{-0.5}{x} - x\\
\mathbf{elif}\;x \leq 1.9 \cdot 10^{-10}:\\
\;\;\;\;\sqrt{y}\\
\mathbf{else}:\\
\;\;\;\;x + 0.5 \cdot \frac{y}{x}\\
\end{array}
\end{array}
if x < -8.3999999999999995e-49Initial program 52.2%
Taylor expanded in x around -inf 74.5%
+-commutative74.5%
mul-1-neg74.5%
unsub-neg74.5%
associate-+r-74.5%
unpow274.5%
associate-*r/74.5%
unpow374.5%
associate-*r*74.5%
times-frac94.3%
distribute-rgt-out94.3%
*-commutative94.3%
Simplified94.3%
Taylor expanded in y around 0 94.4%
*-commutative94.4%
associate-*l/94.4%
associate-*r/94.4%
Simplified94.4%
if -8.3999999999999995e-49 < x < 1.8999999999999999e-10Initial program 100.0%
Taylor expanded in x around 0 88.6%
if 1.8999999999999999e-10 < x Initial program 47.8%
Taylor expanded in x around inf 97.9%
Final simplification93.7%
(FPCore (x y) :precision binary64 (if (<= x -1e-309) (- x) (+ x (* 0.5 (/ y x)))))
double code(double x, double y) {
double tmp;
if (x <= -1e-309) {
tmp = -x;
} else {
tmp = x + (0.5 * (y / 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-309)) then
tmp = -x
else
tmp = x + (0.5d0 * (y / x))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1e-309) {
tmp = -x;
} else {
tmp = x + (0.5 * (y / x));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1e-309: tmp = -x else: tmp = x + (0.5 * (y / x)) return tmp
function code(x, y) tmp = 0.0 if (x <= -1e-309) tmp = Float64(-x); else tmp = Float64(x + Float64(0.5 * Float64(y / x))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1e-309) tmp = -x; else tmp = x + (0.5 * (y / x)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1e-309], (-x), N[(x + N[(0.5 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \cdot 10^{-309}:\\
\;\;\;\;-x\\
\mathbf{else}:\\
\;\;\;\;x + 0.5 \cdot \frac{y}{x}\\
\end{array}
\end{array}
if x < -1.000000000000002e-309Initial program 65.2%
Taylor expanded in x around -inf 71.4%
mul-1-neg71.4%
Simplified71.4%
if -1.000000000000002e-309 < x Initial program 67.1%
Taylor expanded in x around inf 68.7%
Final simplification69.9%
(FPCore (x y) :precision binary64 (if (<= x -1e-309) (- (* y (/ -0.5 x)) x) (+ x (* 0.5 (/ y x)))))
double code(double x, double y) {
double tmp;
if (x <= -1e-309) {
tmp = (y * (-0.5 / x)) - x;
} else {
tmp = x + (0.5 * (y / 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-309)) then
tmp = (y * ((-0.5d0) / x)) - x
else
tmp = x + (0.5d0 * (y / x))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1e-309) {
tmp = (y * (-0.5 / x)) - x;
} else {
tmp = x + (0.5 * (y / x));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1e-309: tmp = (y * (-0.5 / x)) - x else: tmp = x + (0.5 * (y / x)) return tmp
function code(x, y) tmp = 0.0 if (x <= -1e-309) tmp = Float64(Float64(y * Float64(-0.5 / x)) - x); else tmp = Float64(x + Float64(0.5 * Float64(y / x))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1e-309) tmp = (y * (-0.5 / x)) - x; else tmp = x + (0.5 * (y / x)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1e-309], N[(N[(y * N[(-0.5 / x), $MachinePrecision]), $MachinePrecision] - x), $MachinePrecision], N[(x + N[(0.5 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \cdot 10^{-309}:\\
\;\;\;\;y \cdot \frac{-0.5}{x} - x\\
\mathbf{else}:\\
\;\;\;\;x + 0.5 \cdot \frac{y}{x}\\
\end{array}
\end{array}
if x < -1.000000000000002e-309Initial program 65.2%
Taylor expanded in x around -inf 55.3%
+-commutative55.3%
mul-1-neg55.3%
unsub-neg55.3%
associate-+r-55.3%
unpow255.3%
associate-*r/55.3%
unpow355.3%
associate-*r*55.3%
times-frac70.6%
distribute-rgt-out70.6%
*-commutative70.6%
Simplified70.6%
Taylor expanded in y around 0 71.7%
*-commutative71.7%
associate-*l/71.7%
associate-*r/71.7%
Simplified71.7%
if -1.000000000000002e-309 < x Initial program 67.1%
Taylor expanded in x around inf 68.7%
Final simplification70.1%
(FPCore (x y) :precision binary64 (if (<= x -1e-309) (- x) x))
double code(double x, double y) {
double tmp;
if (x <= -1e-309) {
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 <= (-1d-309)) then
tmp = -x
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1e-309) {
tmp = -x;
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1e-309: tmp = -x else: tmp = x return tmp
function code(x, y) tmp = 0.0 if (x <= -1e-309) tmp = Float64(-x); else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1e-309) tmp = -x; else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1e-309], (-x), x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \cdot 10^{-309}:\\
\;\;\;\;-x\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < -1.000000000000002e-309Initial program 65.2%
Taylor expanded in x around -inf 71.4%
mul-1-neg71.4%
Simplified71.4%
if -1.000000000000002e-309 < x Initial program 67.1%
Taylor expanded in x around inf 68.3%
Final simplification69.7%
(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 66.2%
Taylor expanded in x around inf 37.4%
Final simplification37.4%
(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 2023185
(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)))