
(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 (<= y 1e-38) (/ (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 (y <= 1e-38) {
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 (y <= 1e-38) 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[y, 1e-38], 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}\;y \leq 10^{-38}:\\
\;\;\;\;\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 y < 9.9999999999999996e-39Initial program 73.8%
Taylor expanded in y around 0
lower-/.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f6489.1
Applied rewrites89.1%
if 9.9999999999999996e-39 < y Initial program 84.3%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f6499.9
Applied rewrites99.9%
NOTE: x and y should be sorted in increasing order before calling this function.
(FPCore (x y)
:precision binary64
(let* ((t_0 (/ (+ y x) (* y (* x 2.0)))) (t_1 (/ (fma 0.5 (/ x y) 0.5) x)))
(if (<= t_0 (- INFINITY))
t_1
(if (<= t_0 -1e+40)
t_0
(if (<= t_0 5e+19) t_1 (if (<= t_0 1e+307) t_0 t_1))))))assert(x < y);
double code(double x, double y) {
double t_0 = (y + x) / (y * (x * 2.0));
double t_1 = fma(0.5, (x / y), 0.5) / x;
double tmp;
if (t_0 <= -((double) INFINITY)) {
tmp = t_1;
} else if (t_0 <= -1e+40) {
tmp = t_0;
} else if (t_0 <= 5e+19) {
tmp = t_1;
} else if (t_0 <= 1e+307) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
x, y = sort([x, y]) function code(x, y) t_0 = Float64(Float64(y + x) / Float64(y * Float64(x * 2.0))) t_1 = Float64(fma(0.5, Float64(x / y), 0.5) / x) tmp = 0.0 if (t_0 <= Float64(-Inf)) tmp = t_1; elseif (t_0 <= -1e+40) tmp = t_0; elseif (t_0 <= 5e+19) tmp = t_1; elseif (t_0 <= 1e+307) tmp = t_0; else tmp = t_1; end return tmp end
NOTE: x and y should be sorted in increasing order before calling this function.
code[x_, y_] := Block[{t$95$0 = N[(N[(y + x), $MachinePrecision] / N[(y * N[(x * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(0.5 * N[(x / y), $MachinePrecision] + 0.5), $MachinePrecision] / x), $MachinePrecision]}, If[LessEqual[t$95$0, (-Infinity)], t$95$1, If[LessEqual[t$95$0, -1e+40], t$95$0, If[LessEqual[t$95$0, 5e+19], t$95$1, If[LessEqual[t$95$0, 1e+307], t$95$0, t$95$1]]]]]]
\begin{array}{l}
[x, y] = \mathsf{sort}([x, y])\\
\\
\begin{array}{l}
t_0 := \frac{y + x}{y \cdot \left(x \cdot 2\right)}\\
t_1 := \frac{\mathsf{fma}\left(0.5, \frac{x}{y}, 0.5\right)}{x}\\
\mathbf{if}\;t\_0 \leq -\infty:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq -1 \cdot 10^{+40}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;t\_0 \leq 5 \cdot 10^{+19}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;t\_0 \leq 10^{+307}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (/.f64 (+.f64 x y) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) < -inf.0 or -1.00000000000000003e40 < (/.f64 (+.f64 x y) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) < 5e19 or 9.99999999999999986e306 < (/.f64 (+.f64 x y) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) Initial program 46.8%
Taylor expanded in x around 0
lower-/.f64N/A
+-commutativeN/A
lower-fma.f64N/A
lower-/.f6499.9
Applied rewrites99.9%
if -inf.0 < (/.f64 (+.f64 x y) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) < -1.00000000000000003e40 or 5e19 < (/.f64 (+.f64 x y) (*.f64 (*.f64 x #s(literal 2 binary64)) y)) < 9.99999999999999986e306Initial program 99.1%
Final simplification99.4%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= x -1.7e+160) (/ 0.5 y) (if (<= x -1.45e-165) (/ (+ y x) (* y (* x 2.0))) (/ 0.5 x))))
assert(x < y);
double code(double x, double y) {
double tmp;
if (x <= -1.7e+160) {
tmp = 0.5 / y;
} else if (x <= -1.45e-165) {
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 (x <= (-1.7d+160)) then
tmp = 0.5d0 / y
else if (x <= (-1.45d-165)) 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 (x <= -1.7e+160) {
tmp = 0.5 / y;
} else if (x <= -1.45e-165) {
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 x <= -1.7e+160: tmp = 0.5 / y elif x <= -1.45e-165: 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 (x <= -1.7e+160) tmp = Float64(0.5 / y); elseif (x <= -1.45e-165) 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 (x <= -1.7e+160)
tmp = 0.5 / y;
elseif (x <= -1.45e-165)
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[x, -1.7e+160], N[(0.5 / y), $MachinePrecision], If[LessEqual[x, -1.45e-165], 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}\;x \leq -1.7 \cdot 10^{+160}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{elif}\;x \leq -1.45 \cdot 10^{-165}:\\
\;\;\;\;\frac{y + x}{y \cdot \left(x \cdot 2\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if x < -1.70000000000000015e160Initial program 61.2%
Taylor expanded in x around inf
lower-/.f6479.7
Applied rewrites79.7%
if -1.70000000000000015e160 < x < -1.45e-165Initial program 91.5%
if -1.45e-165 < x Initial program 74.1%
Taylor expanded in x around 0
lower-/.f6462.9
Applied rewrites62.9%
Final simplification71.4%
NOTE: x and y should be sorted in increasing order before calling this function. (FPCore (x y) :precision binary64 (if (<= x -2.3e-140) (/ 0.5 y) (/ 0.5 x)))
assert(x < y);
double code(double x, double y) {
double tmp;
if (x <= -2.3e-140) {
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.3d-140)) 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.3e-140) {
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.3e-140: 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.3e-140) 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.3e-140)
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.3e-140], 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.3 \cdot 10^{-140}:\\
\;\;\;\;\frac{0.5}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{0.5}{x}\\
\end{array}
\end{array}
if x < -2.3000000000000001e-140Initial program 81.3%
Taylor expanded in x around inf
lower-/.f6458.0
Applied rewrites58.0%
if -2.3000000000000001e-140 < x Initial program 74.6%
Taylor expanded in x around 0
lower-/.f6463.5
Applied rewrites63.5%
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 76.8%
Taylor expanded in x around 0
lower-/.f6456.0
Applied rewrites56.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 2024233
(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)))