
(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 -1.2e-15) (/ (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 <= -1.2e-15) {
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 <= -1.2e-15) 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, -1.2e-15], 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 -1.2 \cdot 10^{-15}:\\
\;\;\;\;\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 < -1.19999999999999997e-15Initial program 77.0%
Taylor expanded in y around 0
/-lowering-/.f64N/A
+-commutativeN/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6499.9
Simplified99.9%
if -1.19999999999999997e-15 < x Initial program 78.1%
Taylor expanded in x around 0
/-lowering-/.f64N/A
+-commutativeN/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6491.9
Simplified91.9%
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y)
:precision binary64
(if (<= y 7e-189)
(/ 0.5 y)
(if (<= y 1.4e-158)
(/ 0.5 x)
(if (<= y 2.05e+102) (/ (+ x y) (* y (* x 2.0))) (/ 0.5 x)))))assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 7e-189) {
tmp = 0.5 / y;
} else if (y <= 1.4e-158) {
tmp = 0.5 / x;
} else if (y <= 2.05e+102) {
tmp = (x + y) / (y * (x * 2.0));
} 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 <= 7d-189) then
tmp = 0.5d0 / y
else if (y <= 1.4d-158) then
tmp = 0.5d0 / x
else if (y <= 2.05d+102) then
tmp = (x + y) / (y * (x * 2.0d0))
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 <= 7e-189) {
tmp = 0.5 / y;
} else if (y <= 1.4e-158) {
tmp = 0.5 / x;
} else if (y <= 2.05e+102) {
tmp = (x + y) / (y * (x * 2.0));
} else {
tmp = 0.5 / x;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 7e-189: tmp = 0.5 / y elif y <= 1.4e-158: tmp = 0.5 / x elif y <= 2.05e+102: tmp = (x + y) / (y * (x * 2.0)) else: tmp = 0.5 / x return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 7e-189) tmp = Float64(0.5 / y); elseif (y <= 1.4e-158) tmp = Float64(0.5 / x); elseif (y <= 2.05e+102) tmp = Float64(Float64(x + y) / Float64(y * Float64(x * 2.0))); 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 <= 7e-189)
tmp = 0.5 / y;
elseif (y <= 1.4e-158)
tmp = 0.5 / x;
elseif (y <= 2.05e+102)
tmp = (x + y) / (y * (x * 2.0));
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, 7e-189], N[(0.5 / y), $MachinePrecision], If[LessEqual[y, 1.4e-158], N[(0.5 / x), $MachinePrecision], If[LessEqual[y, 2.05e+102], N[(N[(x + y), $MachinePrecision] / N[(y * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 / x), $MachinePrecision]]]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 7 \cdot 10^{-189}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{elif}\;y \leq 1.4 \cdot 10^{-158}:\\
\;\;\;\;\frac{0.5}{x}\\
\mathbf{elif}\;y \leq 2.05 \cdot 10^{+102}:\\
\;\;\;\;\frac{x + y}{y \cdot \left(x \cdot 2\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if y < 7.0000000000000003e-189Initial program 74.0%
Taylor expanded in x around inf
/-lowering-/.f6460.8
Simplified60.8%
if 7.0000000000000003e-189 < y < 1.40000000000000001e-158 or 2.05e102 < y Initial program 74.6%
Taylor expanded in x around 0
/-lowering-/.f6492.5
Simplified92.5%
if 1.40000000000000001e-158 < y < 2.05e102Initial program 92.1%
Final simplification73.6%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 4.5e-211) (/ 0.5 y) (/ (fma 0.5 (/ x y) 0.5) x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 4.5e-211) {
tmp = 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 (y <= 4.5e-211) tmp = Float64(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[y, 4.5e-211], N[(0.5 / 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}\;y \leq 4.5 \cdot 10^{-211}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(0.5, \frac{x}{y}, 0.5\right)}{x}\\
\end{array}
\end{array}
if y < 4.4999999999999999e-211Initial program 73.8%
Taylor expanded in x around inf
/-lowering-/.f6459.8
Simplified59.8%
if 4.4999999999999999e-211 < y Initial program 83.3%
Taylor expanded in x around 0
/-lowering-/.f64N/A
+-commutativeN/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6494.6
Simplified94.6%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 1e-59) (/ 0.5 y) (/ 0.5 x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 1e-59) {
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 <= 1d-59) 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 <= 1e-59) {
tmp = 0.5 / y;
} else {
tmp = 0.5 / x;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 1e-59: 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 <= 1e-59) 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 <= 1e-59)
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, 1e-59], 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 10^{-59}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if y < 1e-59Initial program 73.9%
Taylor expanded in x around inf
/-lowering-/.f6461.8
Simplified61.8%
if 1e-59 < y Initial program 87.6%
Taylor expanded in x around 0
/-lowering-/.f6483.0
Simplified83.0%
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 77.8%
Taylor expanded in x around 0
/-lowering-/.f6452.1
Simplified52.1%
(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 2024205
(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)))