
(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 6 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 (<= y 6.8e+20) (/ (fma (/ 0.5 x) y 0.5) y) (/ (* (/ 0.5 y) (+ y x)) x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 6.8e+20) {
tmp = fma((0.5 / x), y, 0.5) / y;
} else {
tmp = ((0.5 / y) * (y + x)) / x;
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 6.8e+20) tmp = Float64(fma(Float64(0.5 / x), y, 0.5) / y); else tmp = Float64(Float64(Float64(0.5 / y) * Float64(y + x)) / 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, 6.8e+20], N[(N[(N[(0.5 / x), $MachinePrecision] * y + 0.5), $MachinePrecision] / y), $MachinePrecision], N[(N[(N[(0.5 / y), $MachinePrecision] * N[(y + x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 6.8 \cdot 10^{+20}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{0.5}{x}, y, 0.5\right)}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{0.5}{y} \cdot \left(y + x\right)}{x}\\
\end{array}
\end{array}
if y < 6.8e20Initial program 72.6%
Taylor expanded in y around 0
lower-/.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f6491.8
Applied rewrites91.8%
clear-numN/A
un-div-invN/A
associate-/r/N/A
lift-/.f64N/A
lower-fma.f6491.8
Applied rewrites91.8%
if 6.8e20 < y Initial program 73.9%
lift-+.f64N/A
lift-*.f64N/A
associate-/r*N/A
div-invN/A
lift-*.f64N/A
associate-/l/N/A
associate-*l/N/A
lower-/.f64N/A
lower-*.f64N/A
div-invN/A
metadata-evalN/A
metadata-evalN/A
lower-*.f64N/A
metadata-evalN/A
lower-/.f6499.8
Applied rewrites99.8%
lift-+.f64N/A
lift-*.f64N/A
un-div-invN/A
lift-*.f64N/A
*-commutativeN/A
associate-*l/N/A
lift-/.f64N/A
lower-*.f6499.8
Applied rewrites99.8%
Final simplification93.3%
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y)
:precision binary64
(if (<= y 1.75e-174)
(/ 0.5 y)
(if (<= y 1.55e-137)
(/ 0.5 x)
(if (<= y 3.5e+122) (/ (+ y x) (* y (* x 2.0))) (/ 0.5 x)))))assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 1.75e-174) {
tmp = 0.5 / y;
} else if (y <= 1.55e-137) {
tmp = 0.5 / x;
} else if (y <= 3.5e+122) {
tmp = (y + x) / (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 <= 1.75d-174) then
tmp = 0.5d0 / y
else if (y <= 1.55d-137) then
tmp = 0.5d0 / x
else if (y <= 3.5d+122) then
tmp = (y + x) / (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 <= 1.75e-174) {
tmp = 0.5 / y;
} else if (y <= 1.55e-137) {
tmp = 0.5 / x;
} else if (y <= 3.5e+122) {
tmp = (y + x) / (y * (x * 2.0));
} else {
tmp = 0.5 / x;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 1.75e-174: tmp = 0.5 / y elif y <= 1.55e-137: tmp = 0.5 / x elif y <= 3.5e+122: tmp = (y + x) / (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 <= 1.75e-174) tmp = Float64(0.5 / y); elseif (y <= 1.55e-137) tmp = Float64(0.5 / x); elseif (y <= 3.5e+122) tmp = Float64(Float64(y + x) / 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 <= 1.75e-174)
tmp = 0.5 / y;
elseif (y <= 1.55e-137)
tmp = 0.5 / x;
elseif (y <= 3.5e+122)
tmp = (y + x) / (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, 1.75e-174], N[(0.5 / y), $MachinePrecision], If[LessEqual[y, 1.55e-137], N[(0.5 / x), $MachinePrecision], If[LessEqual[y, 3.5e+122], N[(N[(y + x), $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 1.75 \cdot 10^{-174}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{elif}\;y \leq 1.55 \cdot 10^{-137}:\\
\;\;\;\;\frac{0.5}{x}\\
\mathbf{elif}\;y \leq 3.5 \cdot 10^{+122}:\\
\;\;\;\;\frac{y + x}{y \cdot \left(x \cdot 2\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if y < 1.74999999999999994e-174Initial program 70.5%
Taylor expanded in x around inf
lower-/.f6461.2
Applied rewrites61.2%
if 1.74999999999999994e-174 < y < 1.54999999999999989e-137 or 3.50000000000000014e122 < y Initial program 62.0%
Taylor expanded in x around 0
lower-/.f6479.5
Applied rewrites79.5%
if 1.54999999999999989e-137 < y < 3.50000000000000014e122Initial program 94.8%
Final simplification69.7%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 3.9e+122) (/ (fma (/ 0.5 x) y 0.5) y) (/ 0.5 x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 3.9e+122) {
tmp = fma((0.5 / x), y, 0.5) / y;
} else {
tmp = 0.5 / x;
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 3.9e+122) tmp = Float64(fma(Float64(0.5 / x), y, 0.5) / y); else tmp = Float64(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, 3.9e+122], N[(N[(N[(0.5 / x), $MachinePrecision] * y + 0.5), $MachinePrecision] / y), $MachinePrecision], N[(0.5 / x), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 3.9 \cdot 10^{+122}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{0.5}{x}, y, 0.5\right)}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if y < 3.8999999999999999e122Initial program 74.1%
Taylor expanded in y around 0
lower-/.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f6492.0
Applied rewrites92.0%
clear-numN/A
un-div-invN/A
associate-/r/N/A
lift-/.f64N/A
lower-fma.f6492.0
Applied rewrites92.0%
if 3.8999999999999999e122 < y Initial program 64.1%
Taylor expanded in x around 0
lower-/.f6494.0
Applied rewrites94.0%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 3.9e+122) (/ (fma 0.5 (/ y x) 0.5) y) (/ 0.5 x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 3.9e+122) {
tmp = fma(0.5, (y / x), 0.5) / y;
} else {
tmp = 0.5 / x;
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (y <= 3.9e+122) tmp = Float64(fma(0.5, Float64(y / x), 0.5) / y); else tmp = Float64(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, 3.9e+122], N[(N[(0.5 * N[(y / x), $MachinePrecision] + 0.5), $MachinePrecision] / y), $MachinePrecision], N[(0.5 / x), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;y \leq 3.9 \cdot 10^{+122}:\\
\;\;\;\;\frac{\mathsf{fma}\left(0.5, \frac{y}{x}, 0.5\right)}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if y < 3.8999999999999999e122Initial program 74.1%
Taylor expanded in y around 0
lower-/.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f6492.0
Applied rewrites92.0%
if 3.8999999999999999e122 < y Initial program 64.1%
Taylor expanded in x around 0
lower-/.f6494.0
Applied rewrites94.0%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 1.75e-174) (/ 0.5 y) (/ 0.5 x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 1.75e-174) {
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.75d-174) 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.75e-174) {
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.75e-174: 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.75e-174) 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.75e-174)
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.75e-174], 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.75 \cdot 10^{-174}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if y < 1.74999999999999994e-174Initial program 70.5%
Taylor expanded in x around inf
lower-/.f6461.2
Applied rewrites61.2%
if 1.74999999999999994e-174 < y Initial program 77.5%
Taylor expanded in x around 0
lower-/.f6463.1
Applied rewrites63.1%
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 72.8%
Taylor expanded in x around 0
lower-/.f6447.5
Applied rewrites47.5%
(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 2024216
(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)))