
(FPCore (x y) :precision binary64 (/ (+ x y) (* (* x 2.0) y)))
double code(double x, double y) {
return (x + y) / ((x * 2.0) * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) / ((x * 2.0d0) * y)
end function
public static double code(double x, double y) {
return (x + y) / ((x * 2.0) * y);
}
def code(x, y): return (x + y) / ((x * 2.0) * y)
function code(x, y) return Float64(Float64(x + y) / Float64(Float64(x * 2.0) * y)) end
function tmp = code(x, y) tmp = (x + y) / ((x * 2.0) * y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{\left(x \cdot 2\right) \cdot y}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (/ (+ x y) (* (* x 2.0) y)))
double code(double x, double y) {
return (x + y) / ((x * 2.0) * y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) / ((x * 2.0d0) * y)
end function
public static double code(double x, double y) {
return (x + y) / ((x * 2.0) * y);
}
def code(x, y): return (x + y) / ((x * 2.0) * y)
function code(x, y) return Float64(Float64(x + y) / Float64(Float64(x * 2.0) * y)) end
function tmp = code(x, y) tmp = (x + y) / ((x * 2.0) * y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(N[(x * 2.0), $MachinePrecision] * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{\left(x \cdot 2\right) \cdot y}
\end{array}
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= x -4.5e+39) (/ (fma 0.5 (/ y x) 0.5) y) (/ (fma 0.5 (/ x y) 0.5) x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (x <= -4.5e+39) {
tmp = fma(0.5, (y / x), 0.5) / y;
} else {
tmp = fma(0.5, (x / y), 0.5) / x;
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (x <= -4.5e+39) tmp = Float64(fma(0.5, Float64(y / x), 0.5) / y); else tmp = Float64(fma(0.5, Float64(x / y), 0.5) / x); end return tmp end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[x, -4.5e+39], N[(N[(0.5 * N[(y / x), $MachinePrecision] + 0.5), $MachinePrecision] / y), $MachinePrecision], N[(N[(0.5 * N[(x / y), $MachinePrecision] + 0.5), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.5 \cdot 10^{+39}:\\
\;\;\;\;\frac{\mathsf{fma}\left(0.5, \frac{y}{x}, 0.5\right)}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(0.5, \frac{x}{y}, 0.5\right)}{x}\\
\end{array}
\end{array}
if x < -4.49999999999999996e39Initial program 73.4%
Taylor expanded in y around 0
/-lowering-/.f64N/A
+-commutativeN/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f64100.0
Simplified100.0%
if -4.49999999999999996e39 < x Initial program 83.1%
Taylor expanded in x around 0
/-lowering-/.f64N/A
+-commutativeN/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6493.3
Simplified93.3%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= x -1.75e+165) (/ 0.5 y) (if (<= x -9e+39) (/ (+ x y) (* y (* x 2.0))) (/ (fma 0.5 (/ x y) 0.5) x))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (x <= -1.75e+165) {
tmp = 0.5 / y;
} else if (x <= -9e+39) {
tmp = (x + y) / (y * (x * 2.0));
} else {
tmp = fma(0.5, (x / y), 0.5) / x;
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (x <= -1.75e+165) tmp = Float64(0.5 / y); elseif (x <= -9e+39) tmp = Float64(Float64(x + y) / Float64(y * Float64(x * 2.0))); else tmp = Float64(fma(0.5, Float64(x / y), 0.5) / x); end return tmp end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[x, -1.75e+165], N[(0.5 / y), $MachinePrecision], If[LessEqual[x, -9e+39], N[(N[(x + y), $MachinePrecision] / N[(y * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * N[(x / y), $MachinePrecision] + 0.5), $MachinePrecision] / x), $MachinePrecision]]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.75 \cdot 10^{+165}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{elif}\;x \leq -9 \cdot 10^{+39}:\\
\;\;\;\;\frac{x + y}{y \cdot \left(x \cdot 2\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(0.5, \frac{x}{y}, 0.5\right)}{x}\\
\end{array}
\end{array}
if x < -1.74999999999999998e165Initial program 63.0%
Taylor expanded in x around inf
/-lowering-/.f6487.9
Simplified87.9%
if -1.74999999999999998e165 < x < -8.99999999999999991e39Initial program 87.4%
if -8.99999999999999991e39 < x Initial program 83.1%
Taylor expanded in x around 0
/-lowering-/.f64N/A
+-commutativeN/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6493.3
Simplified93.3%
Final simplification92.1%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 5e-161) (/ 0.5 y) (if (<= y 1.6e+156) (* (+ x y) (/ 0.5 (* x y))) (/ 0.5 x))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 5e-161) {
tmp = 0.5 / y;
} else if (y <= 1.6e+156) {
tmp = (x + y) * (0.5 / (x * y));
} else {
tmp = 0.5 / x;
}
return tmp;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 5d-161) then
tmp = 0.5d0 / y
else if (y <= 1.6d+156) then
tmp = (x + y) * (0.5d0 / (x * y))
else
tmp = 0.5d0 / x
end if
code = tmp
end function
assert x < y;
public static double code(double x, double y) {
double tmp;
if (y <= 5e-161) {
tmp = 0.5 / y;
} else if (y <= 1.6e+156) {
tmp = (x + y) * (0.5 / (x * y));
} else {
tmp = 0.5 / x;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 5e-161: tmp = 0.5 / y elif y <= 1.6e+156: tmp = (x + y) * (0.5 / (x * y)) else: tmp = 0.5 / x return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 5e-161) tmp = Float64(0.5 / y); elseif (y <= 1.6e+156) tmp = Float64(Float64(x + y) * Float64(0.5 / Float64(x * y))); else tmp = Float64(0.5 / x); end return tmp end
x, y = num2cell(sort([x, y])){:}
function tmp_2 = code(x, y)
tmp = 0.0;
if (y <= 5e-161)
tmp = 0.5 / y;
elseif (y <= 1.6e+156)
tmp = (x + y) * (0.5 / (x * y));
else
tmp = 0.5 / x;
end
tmp_2 = tmp;
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[y, 5e-161], N[(0.5 / y), $MachinePrecision], If[LessEqual[y, 1.6e+156], N[(N[(x + y), $MachinePrecision] * N[(0.5 / N[(x * y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 / x), $MachinePrecision]]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 5 \cdot 10^{-161}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{elif}\;y \leq 1.6 \cdot 10^{+156}:\\
\;\;\;\;\left(x + y\right) \cdot \frac{0.5}{x \cdot y}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if y < 4.9999999999999999e-161Initial program 78.2%
Taylor expanded in x around inf
/-lowering-/.f6459.6
Simplified59.6%
if 4.9999999999999999e-161 < y < 1.60000000000000001e156Initial program 90.3%
clear-numN/A
associate-/r/N/A
*-lowering-*.f64N/A
*-commutativeN/A
associate-*l*N/A
associate-/r*N/A
metadata-evalN/A
metadata-evalN/A
/-lowering-/.f64N/A
metadata-evalN/A
*-lowering-*.f64N/A
+-lowering-+.f6491.4
Applied egg-rr91.4%
if 1.60000000000000001e156 < y Initial program 68.2%
Taylor expanded in x around 0
/-lowering-/.f6489.5
Simplified89.5%
Final simplification72.9%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 1.25e-85) (/ 0.5 y) (/ 0.5 x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 1.25e-85) {
tmp = 0.5 / y;
} else {
tmp = 0.5 / x;
}
return tmp;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 1.25d-85) then
tmp = 0.5d0 / y
else
tmp = 0.5d0 / x
end if
code = tmp
end function
assert x < y;
public static double code(double x, double y) {
double tmp;
if (y <= 1.25e-85) {
tmp = 0.5 / y;
} else {
tmp = 0.5 / x;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 1.25e-85: tmp = 0.5 / y else: tmp = 0.5 / x return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 1.25e-85) tmp = Float64(0.5 / y); else tmp = Float64(0.5 / x); end return tmp end
x, y = num2cell(sort([x, y])){:}
function tmp_2 = code(x, y)
tmp = 0.0;
if (y <= 1.25e-85)
tmp = 0.5 / y;
else
tmp = 0.5 / x;
end
tmp_2 = tmp;
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := If[LessEqual[y, 1.25e-85], N[(0.5 / y), $MachinePrecision], N[(0.5 / x), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.25 \cdot 10^{-85}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if y < 1.25e-85Initial program 79.1%
Taylor expanded in x around inf
/-lowering-/.f6460.9
Simplified60.9%
if 1.25e-85 < y Initial program 84.7%
Taylor expanded in x around 0
/-lowering-/.f6473.2
Simplified73.2%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (/ 0.5 x))
assert(x < y);
double code(double x, double y) {
return 0.5 / x;
}
NOTE: x and y should be sorted in increasing order before calling this function.
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 0.5d0 / x
end function
assert x < y;
public static double code(double x, double y) {
return 0.5 / x;
}
[x, y] = sort([x, y]) def code(x, y): return 0.5 / x
x, y = sort([x, y]) function code(x, y) return Float64(0.5 / x) end
x, y = num2cell(sort([x, y])){:}
function tmp = code(x, y)
tmp = 0.5 / x;
end
NOTE: x and y should be sorted in increasing order before calling this function. code[x_, y_] := N[(0.5 / x), $MachinePrecision]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\frac{0.5}{x}
\end{array}
Initial program 81.0%
Taylor expanded in x around 0
/-lowering-/.f6450.7
Simplified50.7%
(FPCore (x y) :precision binary64 (+ (/ 0.5 x) (/ 0.5 y)))
double code(double x, double y) {
return (0.5 / x) + (0.5 / y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (0.5d0 / x) + (0.5d0 / y)
end function
public static double code(double x, double y) {
return (0.5 / x) + (0.5 / y);
}
def code(x, y): return (0.5 / x) + (0.5 / y)
function code(x, y) return Float64(Float64(0.5 / x) + Float64(0.5 / y)) end
function tmp = code(x, y) tmp = (0.5 / x) + (0.5 / y); end
code[x_, y_] := N[(N[(0.5 / x), $MachinePrecision] + N[(0.5 / y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{0.5}{x} + \frac{0.5}{y}
\end{array}
herbie shell --seed 2024199
(FPCore (x y)
:name "Linear.Projection:inversePerspective from linear-1.19.1.3, C"
:precision binary64
:alt
(! :herbie-platform default (+ (/ 1/2 x) (/ 1/2 y)))
(/ (+ x y) (* (* x 2.0) y)))