
(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}
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (if (<= x_m 1.8e-58) (sqrt y) (+ x_m (* 0.5 (/ y x_m)))))
x_m = fabs(x);
double code(double x_m, double y) {
double tmp;
if (x_m <= 1.8e-58) {
tmp = sqrt(y);
} else {
tmp = x_m + (0.5 * (y / x_m));
}
return tmp;
}
x_m = abs(x)
real(8) function code(x_m, y)
real(8), intent (in) :: x_m
real(8), intent (in) :: y
real(8) :: tmp
if (x_m <= 1.8d-58) then
tmp = sqrt(y)
else
tmp = x_m + (0.5d0 * (y / x_m))
end if
code = tmp
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
double tmp;
if (x_m <= 1.8e-58) {
tmp = Math.sqrt(y);
} else {
tmp = x_m + (0.5 * (y / x_m));
}
return tmp;
}
x_m = math.fabs(x) def code(x_m, y): tmp = 0 if x_m <= 1.8e-58: tmp = math.sqrt(y) else: tmp = x_m + (0.5 * (y / x_m)) return tmp
x_m = abs(x) function code(x_m, y) tmp = 0.0 if (x_m <= 1.8e-58) tmp = sqrt(y); else tmp = Float64(x_m + Float64(0.5 * Float64(y / x_m))); end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, y) tmp = 0.0; if (x_m <= 1.8e-58) tmp = sqrt(y); else tmp = x_m + (0.5 * (y / x_m)); end tmp_2 = tmp; end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := If[LessEqual[x$95$m, 1.8e-58], N[Sqrt[y], $MachinePrecision], N[(x$95$m + N[(0.5 * N[(y / x$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x_m \leq 1.8 \cdot 10^{-58}:\\
\;\;\;\;\sqrt{y}\\
\mathbf{else}:\\
\;\;\;\;x_m + 0.5 \cdot \frac{y}{x_m}\\
\end{array}
\end{array}
if x < 1.80000000000000005e-58Initial program 74.8%
Taylor expanded in x around 0 46.1%
if 1.80000000000000005e-58 < x Initial program 58.8%
Taylor expanded in x around inf 89.3%
Final simplification61.8%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (if (<= (* x_m x_m) INFINITY) (sqrt (fma x_m x_m y)) x_m))
x_m = fabs(x);
double code(double x_m, double y) {
double tmp;
if ((x_m * x_m) <= ((double) INFINITY)) {
tmp = sqrt(fma(x_m, x_m, y));
} else {
tmp = x_m;
}
return tmp;
}
x_m = abs(x) function code(x_m, y) tmp = 0.0 if (Float64(x_m * x_m) <= Inf) tmp = sqrt(fma(x_m, x_m, y)); else tmp = x_m; end return tmp end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := If[LessEqual[N[(x$95$m * x$95$m), $MachinePrecision], Infinity], N[Sqrt[N[(x$95$m * x$95$m + y), $MachinePrecision]], $MachinePrecision], x$95$m]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x_m \cdot x_m \leq \infty:\\
\;\;\;\;\sqrt{\mathsf{fma}\left(x_m, x_m, y\right)}\\
\mathbf{else}:\\
\;\;\;\;x_m\\
\end{array}
\end{array}
if (*.f64 x x) < +inf.0Initial program 69.0%
fma-def69.0%
Simplified69.0%
if +inf.0 < (*.f64 x x) Initial program 69.0%
Taylor expanded in x around inf 35.7%
Final simplification69.0%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (if (<= (* x_m x_m) INFINITY) (sqrt (+ (* x_m x_m) y)) x_m))
x_m = fabs(x);
double code(double x_m, double y) {
double tmp;
if ((x_m * x_m) <= ((double) INFINITY)) {
tmp = sqrt(((x_m * x_m) + y));
} else {
tmp = x_m;
}
return tmp;
}
x_m = Math.abs(x);
public static double code(double x_m, double y) {
double tmp;
if ((x_m * x_m) <= Double.POSITIVE_INFINITY) {
tmp = Math.sqrt(((x_m * x_m) + y));
} else {
tmp = x_m;
}
return tmp;
}
x_m = math.fabs(x) def code(x_m, y): tmp = 0 if (x_m * x_m) <= math.inf: tmp = math.sqrt(((x_m * x_m) + y)) else: tmp = x_m return tmp
x_m = abs(x) function code(x_m, y) tmp = 0.0 if (Float64(x_m * x_m) <= Inf) tmp = sqrt(Float64(Float64(x_m * x_m) + y)); else tmp = x_m; end return tmp end
x_m = abs(x); function tmp_2 = code(x_m, y) tmp = 0.0; if ((x_m * x_m) <= Inf) tmp = sqrt(((x_m * x_m) + y)); else tmp = x_m; end tmp_2 = tmp; end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := If[LessEqual[N[(x$95$m * x$95$m), $MachinePrecision], Infinity], N[Sqrt[N[(N[(x$95$m * x$95$m), $MachinePrecision] + y), $MachinePrecision]], $MachinePrecision], x$95$m]
\begin{array}{l}
x_m = \left|x\right|
\\
\begin{array}{l}
\mathbf{if}\;x_m \cdot x_m \leq \infty:\\
\;\;\;\;\sqrt{x_m \cdot x_m + y}\\
\mathbf{else}:\\
\;\;\;\;x_m\\
\end{array}
\end{array}
if (*.f64 x x) < +inf.0Initial program 69.0%
if +inf.0 < (*.f64 x x) Initial program 69.0%
Taylor expanded in x around inf 35.7%
Final simplification69.0%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (+ x_m (* 0.5 (/ y x_m))))
x_m = fabs(x);
double code(double x_m, double y) {
return x_m + (0.5 * (y / x_m));
}
x_m = abs(x)
real(8) function code(x_m, y)
real(8), intent (in) :: x_m
real(8), intent (in) :: y
code = x_m + (0.5d0 * (y / x_m))
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
return x_m + (0.5 * (y / x_m));
}
x_m = math.fabs(x) def code(x_m, y): return x_m + (0.5 * (y / x_m))
x_m = abs(x) function code(x_m, y) return Float64(x_m + Float64(0.5 * Float64(y / x_m))) end
x_m = abs(x); function tmp = code(x_m, y) tmp = x_m + (0.5 * (y / x_m)); end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := N[(x$95$m + N[(0.5 * N[(y / x$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_m + 0.5 \cdot \frac{y}{x_m}
\end{array}
Initial program 69.0%
Taylor expanded in x around inf 36.1%
Final simplification36.1%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 (+ x_m (* y (/ 0.5 x_m))))
x_m = fabs(x);
double code(double x_m, double y) {
return x_m + (y * (0.5 / x_m));
}
x_m = abs(x)
real(8) function code(x_m, y)
real(8), intent (in) :: x_m
real(8), intent (in) :: y
code = x_m + (y * (0.5d0 / x_m))
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
return x_m + (y * (0.5 / x_m));
}
x_m = math.fabs(x) def code(x_m, y): return x_m + (y * (0.5 / x_m))
x_m = abs(x) function code(x_m, y) return Float64(x_m + Float64(y * Float64(0.5 / x_m))) end
x_m = abs(x); function tmp = code(x_m, y) tmp = x_m + (y * (0.5 / x_m)); end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := N[(x$95$m + N[(y * N[(0.5 / x$95$m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
x_m = \left|x\right|
\\
x_m + y \cdot \frac{0.5}{x_m}
\end{array}
Initial program 69.0%
Taylor expanded in x around inf 36.1%
clear-num36.1%
un-div-inv36.1%
Applied egg-rr36.1%
associate-/r/36.1%
Applied egg-rr36.1%
Final simplification36.1%
x_m = (fabs.f64 x) (FPCore (x_m y) :precision binary64 x_m)
x_m = fabs(x);
double code(double x_m, double y) {
return x_m;
}
x_m = abs(x)
real(8) function code(x_m, y)
real(8), intent (in) :: x_m
real(8), intent (in) :: y
code = x_m
end function
x_m = Math.abs(x);
public static double code(double x_m, double y) {
return x_m;
}
x_m = math.fabs(x) def code(x_m, y): return x_m
x_m = abs(x) function code(x_m, y) return x_m end
x_m = abs(x); function tmp = code(x_m, y) tmp = x_m; end
x_m = N[Abs[x], $MachinePrecision] code[x$95$m_, y_] := x$95$m
\begin{array}{l}
x_m = \left|x\right|
\\
x_m
\end{array}
Initial program 69.0%
Taylor expanded in x around inf 35.7%
Final simplification35.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 2024011
(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)))