
(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 (<= x -5e-54) (/ (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 <= -5e-54) {
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 <= -5e-54) 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, -5e-54], 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 -5 \cdot 10^{-54}:\\
\;\;\;\;\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 < -5.00000000000000015e-54Initial program 89.9%
Taylor expanded in y around 0
lower-/.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f6499.9
Applied rewrites99.9%
if -5.00000000000000015e-54 < x Initial program 78.9%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f6493.4
Applied rewrites93.4%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 5e-154) (/ 0.5 y) (if (<= y 3e+112) (/ (+ x y) (* y (* x 2.0))) (/ 0.5 x))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 5e-154) {
tmp = 0.5 / y;
} else if (y <= 3e+112) {
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 <= 5d-154) then
tmp = 0.5d0 / y
else if (y <= 3d+112) 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 <= 5e-154) {
tmp = 0.5 / y;
} else if (y <= 3e+112) {
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 <= 5e-154: tmp = 0.5 / y elif y <= 3e+112: 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 <= 5e-154) tmp = Float64(0.5 / y); elseif (y <= 3e+112) 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 <= 5e-154)
tmp = 0.5 / y;
elseif (y <= 3e+112)
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, 5e-154], N[(0.5 / y), $MachinePrecision], If[LessEqual[y, 3e+112], 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 5 \cdot 10^{-154}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{elif}\;y \leq 3 \cdot 10^{+112}:\\
\;\;\;\;\frac{x + y}{y \cdot \left(x \cdot 2\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if y < 5.0000000000000002e-154Initial program 77.2%
Taylor expanded in x around inf
lower-/.f6457.2
Applied rewrites57.2%
if 5.0000000000000002e-154 < y < 2.99999999999999979e112Initial program 96.4%
if 2.99999999999999979e112 < y Initial program 79.6%
Taylor expanded in x around 0
lower-/.f6495.1
Applied rewrites95.1%
Final simplification71.5%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 5.6e-154) (/ 0.5 y) (if (<= y 3e+112) (* (+ x y) (/ 0.5 (* x y))) (/ 0.5 x))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 5.6e-154) {
tmp = 0.5 / y;
} else if (y <= 3e+112) {
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 <= 5.6d-154) then
tmp = 0.5d0 / y
else if (y <= 3d+112) 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 <= 5.6e-154) {
tmp = 0.5 / y;
} else if (y <= 3e+112) {
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 <= 5.6e-154: tmp = 0.5 / y elif y <= 3e+112: 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 <= 5.6e-154) tmp = Float64(0.5 / y); elseif (y <= 3e+112) 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 <= 5.6e-154)
tmp = 0.5 / y;
elseif (y <= 3e+112)
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, 5.6e-154], N[(0.5 / y), $MachinePrecision], If[LessEqual[y, 3e+112], 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.6 \cdot 10^{-154}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{elif}\;y \leq 3 \cdot 10^{+112}:\\
\;\;\;\;\left(x + y\right) \cdot \frac{0.5}{x \cdot y}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if y < 5.60000000000000025e-154Initial program 77.2%
Taylor expanded in x around inf
lower-/.f6457.2
Applied rewrites57.2%
if 5.60000000000000025e-154 < y < 2.99999999999999979e112Initial program 96.4%
lift-+.f64N/A
lift-*.f64N/A
lift-*.f64N/A
div-invN/A
*-commutativeN/A
lower-*.f64N/A
lift-*.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-*l*N/A
associate-/r*N/A
metadata-evalN/A
metadata-evalN/A
lower-/.f64N/A
metadata-evalN/A
lower-*.f6496.3
Applied rewrites96.3%
if 2.99999999999999979e112 < y Initial program 79.6%
Taylor expanded in x around 0
lower-/.f6495.1
Applied rewrites95.1%
Final simplification71.5%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 5e-171) (/ 0.5 y) (/ (fma 0.5 (/ x y) 0.5) x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 5e-171) {
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 <= 5e-171) 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, 5e-171], 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 5 \cdot 10^{-171}:\\
\;\;\;\;\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.99999999999999992e-171Initial program 78.1%
Taylor expanded in x around inf
lower-/.f6457.5
Applied rewrites57.5%
if 4.99999999999999992e-171 < y Initial program 88.0%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f6495.9
Applied rewrites95.9%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= y 5.8e-90) (/ 0.5 y) (/ 0.5 x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (y <= 5.8e-90) {
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 <= 5.8d-90) 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 <= 5.8e-90) {
tmp = 0.5 / y;
} else {
tmp = 0.5 / x;
}
return tmp;
}
[x, y] = sort([x, y]) def code(x, y): tmp = 0 if y <= 5.8e-90: 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 <= 5.8e-90) 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 <= 5.8e-90)
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, 5.8e-90], 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 5.8 \cdot 10^{-90}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if y < 5.79999999999999967e-90Initial program 78.3%
Taylor expanded in x around inf
lower-/.f6458.7
Applied rewrites58.7%
if 5.79999999999999967e-90 < y Initial program 88.7%
Taylor expanded in x around 0
lower-/.f6470.8
Applied rewrites70.8%
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.8%
Taylor expanded in x around 0
lower-/.f6452.0
Applied rewrites52.0%
(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 2024219
(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)))