
(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 -2.05e-74) (/ (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 <= -2.05e-74) {
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 <= -2.05e-74) 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, -2.05e-74], 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 -2.05 \cdot 10^{-74}:\\
\;\;\;\;\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 < -2.05000000000000016e-74Initial program 77.3%
Taylor expanded in y around 0
lower-/.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f6499.8
Applied rewrites99.8%
if -2.05000000000000016e-74 < x Initial program 75.4%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f6488.9
Applied rewrites88.9%
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y)
:precision binary64
(if (<= x -2.9e+142)
(/ 0.5 y)
(if (<= x -1.1e-194)
(/ (+ x y) (* y (* x 2.0)))
(if (<= x -3.6e-205) (/ 0.5 y) (/ 0.5 x)))))assert(x < y);
double code(double x, double y) {
double tmp;
if (x <= -2.9e+142) {
tmp = 0.5 / y;
} else if (x <= -1.1e-194) {
tmp = (x + y) / (y * (x * 2.0));
} else if (x <= -3.6e-205) {
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 (x <= (-2.9d+142)) then
tmp = 0.5d0 / y
else if (x <= (-1.1d-194)) then
tmp = (x + y) / (y * (x * 2.0d0))
else if (x <= (-3.6d-205)) 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 (x <= -2.9e+142) {
tmp = 0.5 / y;
} else if (x <= -1.1e-194) {
tmp = (x + y) / (y * (x * 2.0));
} else if (x <= -3.6e-205) {
tmp = 0.5 / y;
} else {
tmp = 0.5 / x;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if x <= -2.9e+142: tmp = 0.5 / y elif x <= -1.1e-194: tmp = (x + y) / (y * (x * 2.0)) elif x <= -3.6e-205: tmp = 0.5 / y else: tmp = 0.5 / x return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (x <= -2.9e+142) tmp = Float64(0.5 / y); elseif (x <= -1.1e-194) tmp = Float64(Float64(x + y) / Float64(y * Float64(x * 2.0))); elseif (x <= -3.6e-205) 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 (x <= -2.9e+142)
tmp = 0.5 / y;
elseif (x <= -1.1e-194)
tmp = (x + y) / (y * (x * 2.0));
elseif (x <= -3.6e-205)
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[x, -2.9e+142], N[(0.5 / y), $MachinePrecision], If[LessEqual[x, -1.1e-194], N[(N[(x + y), $MachinePrecision] / N[(y * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, -3.6e-205], N[(0.5 / y), $MachinePrecision], N[(0.5 / x), $MachinePrecision]]]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.9 \cdot 10^{+142}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{elif}\;x \leq -1.1 \cdot 10^{-194}:\\
\;\;\;\;\frac{x + y}{y \cdot \left(x \cdot 2\right)}\\
\mathbf{elif}\;x \leq -3.6 \cdot 10^{-205}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if x < -2.90000000000000013e142 or -1.1000000000000001e-194 < x < -3.5999999999999998e-205Initial program 63.4%
Taylor expanded in x around inf
lower-/.f6475.1
Applied rewrites75.1%
if -2.90000000000000013e142 < x < -1.1000000000000001e-194Initial program 89.7%
if -3.5999999999999998e-205 < x Initial program 73.3%
Taylor expanded in x around 0
lower-/.f6458.1
Applied rewrites58.1%
Final simplification68.0%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= x -7.6e+158) (/ 0.5 y) (/ (fma 0.5 (/ x y) 0.5) x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (x <= -7.6e+158) {
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 (x <= -7.6e+158) 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[x, -7.6e+158], 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}\;x \leq -7.6 \cdot 10^{+158}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(0.5, \frac{x}{y}, 0.5\right)}{x}\\
\end{array}
\end{array}
if x < -7.5999999999999997e158Initial program 68.1%
Taylor expanded in x around inf
lower-/.f6478.2
Applied rewrites78.2%
if -7.5999999999999997e158 < x Initial program 77.0%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f6489.7
Applied rewrites89.7%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= x -4.5e-103) (/ 0.5 y) (/ 0.5 x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (x <= -4.5e-103) {
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 (x <= (-4.5d-103)) 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 (x <= -4.5e-103) {
tmp = 0.5 / y;
} else {
tmp = 0.5 / x;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if x <= -4.5e-103: tmp = 0.5 / y else: tmp = 0.5 / x return tmp
x, y = sort([x, y]) function code(x, y) tmp = 0.0 if (x <= -4.5e-103) 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 (x <= -4.5e-103)
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[x, -4.5e-103], N[(0.5 / y), $MachinePrecision], N[(0.5 / x), $MachinePrecision]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.5 \cdot 10^{-103}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if x < -4.5e-103Initial program 79.6%
Taylor expanded in x around inf
lower-/.f6456.0
Applied rewrites56.0%
if -4.5e-103 < x Initial program 74.3%
Taylor expanded in x around 0
lower-/.f6460.7
Applied rewrites60.7%
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 75.9%
Taylor expanded in x around 0
lower-/.f6456.2
Applied rewrites56.2%
(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 2024221
(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)))