
(FPCore (x y) :precision binary64 (/ (+ x y) (- x y)))
double code(double x, double y) {
return (x + y) / (x - y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) / (x - y)
end function
public static double code(double x, double y) {
return (x + y) / (x - y);
}
def code(x, y): return (x + y) / (x - y)
function code(x, y) return Float64(Float64(x + y) / Float64(x - y)) end
function tmp = code(x, y) tmp = (x + y) / (x - y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{x - y}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (/ (+ x y) (- x y)))
double code(double x, double y) {
return (x + y) / (x - y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) / (x - y)
end function
public static double code(double x, double y) {
return (x + y) / (x - y);
}
def code(x, y): return (x + y) / (x - y)
function code(x, y) return Float64(Float64(x + y) / Float64(x - y)) end
function tmp = code(x, y) tmp = (x + y) / (x - y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{x - y}
\end{array}
(FPCore (x y) :precision binary64 (/ (+ x y) (- x y)))
double code(double x, double y) {
return (x + y) / (x - y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) / (x - y)
end function
public static double code(double x, double y) {
return (x + y) / (x - y);
}
def code(x, y): return (x + y) / (x - y)
function code(x, y) return Float64(Float64(x + y) / Float64(x - y)) end
function tmp = code(x, y) tmp = (x + y) / (x - y); end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{x - y}
\end{array}
Initial program 100.0%
(FPCore (x y) :precision binary64 (if (or (<= y -46000000000000.0) (not (<= y 3.8e-68))) (/ y (- x y)) (+ 1.0 (* 2.0 (/ y x)))))
double code(double x, double y) {
double tmp;
if ((y <= -46000000000000.0) || !(y <= 3.8e-68)) {
tmp = y / (x - y);
} else {
tmp = 1.0 + (2.0 * (y / x));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y <= (-46000000000000.0d0)) .or. (.not. (y <= 3.8d-68))) then
tmp = y / (x - y)
else
tmp = 1.0d0 + (2.0d0 * (y / x))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -46000000000000.0) || !(y <= 3.8e-68)) {
tmp = y / (x - y);
} else {
tmp = 1.0 + (2.0 * (y / x));
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -46000000000000.0) or not (y <= 3.8e-68): tmp = y / (x - y) else: tmp = 1.0 + (2.0 * (y / x)) return tmp
function code(x, y) tmp = 0.0 if ((y <= -46000000000000.0) || !(y <= 3.8e-68)) tmp = Float64(y / Float64(x - y)); else tmp = Float64(1.0 + Float64(2.0 * Float64(y / x))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -46000000000000.0) || ~((y <= 3.8e-68))) tmp = y / (x - y); else tmp = 1.0 + (2.0 * (y / x)); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -46000000000000.0], N[Not[LessEqual[y, 3.8e-68]], $MachinePrecision]], N[(y / N[(x - y), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(2.0 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -46000000000000 \lor \neg \left(y \leq 3.8 \cdot 10^{-68}\right):\\
\;\;\;\;\frac{y}{x - y}\\
\mathbf{else}:\\
\;\;\;\;1 + 2 \cdot \frac{y}{x}\\
\end{array}
\end{array}
if y < -4.6e13 or 3.80000000000000038e-68 < y Initial program 100.0%
Taylor expanded in x around 0 75.5%
if -4.6e13 < y < 3.80000000000000038e-68Initial program 100.0%
Taylor expanded in y around 0 83.5%
Final simplification79.5%
(FPCore (x y) :precision binary64 (if (<= y -7000000000000.0) (/ y (- x y)) (if (<= y 2.2e-66) (+ 1.0 (* 2.0 (/ y x))) (+ (* -2.0 (/ x y)) -1.0))))
double code(double x, double y) {
double tmp;
if (y <= -7000000000000.0) {
tmp = y / (x - y);
} else if (y <= 2.2e-66) {
tmp = 1.0 + (2.0 * (y / x));
} else {
tmp = (-2.0 * (x / y)) + -1.0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-7000000000000.0d0)) then
tmp = y / (x - y)
else if (y <= 2.2d-66) then
tmp = 1.0d0 + (2.0d0 * (y / x))
else
tmp = ((-2.0d0) * (x / y)) + (-1.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -7000000000000.0) {
tmp = y / (x - y);
} else if (y <= 2.2e-66) {
tmp = 1.0 + (2.0 * (y / x));
} else {
tmp = (-2.0 * (x / y)) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -7000000000000.0: tmp = y / (x - y) elif y <= 2.2e-66: tmp = 1.0 + (2.0 * (y / x)) else: tmp = (-2.0 * (x / y)) + -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -7000000000000.0) tmp = Float64(y / Float64(x - y)); elseif (y <= 2.2e-66) tmp = Float64(1.0 + Float64(2.0 * Float64(y / x))); else tmp = Float64(Float64(-2.0 * Float64(x / y)) + -1.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -7000000000000.0) tmp = y / (x - y); elseif (y <= 2.2e-66) tmp = 1.0 + (2.0 * (y / x)); else tmp = (-2.0 * (x / y)) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -7000000000000.0], N[(y / N[(x - y), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 2.2e-66], N[(1.0 + N[(2.0 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(-2.0 * N[(x / y), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -7000000000000:\\
\;\;\;\;\frac{y}{x - y}\\
\mathbf{elif}\;y \leq 2.2 \cdot 10^{-66}:\\
\;\;\;\;1 + 2 \cdot \frac{y}{x}\\
\mathbf{else}:\\
\;\;\;\;-2 \cdot \frac{x}{y} + -1\\
\end{array}
\end{array}
if y < -7e12Initial program 100.0%
Taylor expanded in x around 0 79.2%
if -7e12 < y < 2.2000000000000001e-66Initial program 100.0%
Taylor expanded in y around 0 83.0%
if 2.2000000000000001e-66 < y Initial program 100.0%
Taylor expanded in x around 0 73.6%
Final simplification79.6%
(FPCore (x y) :precision binary64 (if (or (<= y -64000000000000.0) (not (<= y 2.25e-66))) (/ y (- x y)) (/ x (- x y))))
double code(double x, double y) {
double tmp;
if ((y <= -64000000000000.0) || !(y <= 2.25e-66)) {
tmp = y / (x - y);
} else {
tmp = x / (x - y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y <= (-64000000000000.0d0)) .or. (.not. (y <= 2.25d-66))) then
tmp = y / (x - y)
else
tmp = x / (x - y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -64000000000000.0) || !(y <= 2.25e-66)) {
tmp = y / (x - y);
} else {
tmp = x / (x - y);
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -64000000000000.0) or not (y <= 2.25e-66): tmp = y / (x - y) else: tmp = x / (x - y) return tmp
function code(x, y) tmp = 0.0 if ((y <= -64000000000000.0) || !(y <= 2.25e-66)) tmp = Float64(y / Float64(x - y)); else tmp = Float64(x / Float64(x - y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -64000000000000.0) || ~((y <= 2.25e-66))) tmp = y / (x - y); else tmp = x / (x - y); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -64000000000000.0], N[Not[LessEqual[y, 2.25e-66]], $MachinePrecision]], N[(y / N[(x - y), $MachinePrecision]), $MachinePrecision], N[(x / N[(x - y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -64000000000000 \lor \neg \left(y \leq 2.25 \cdot 10^{-66}\right):\\
\;\;\;\;\frac{y}{x - y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{x - y}\\
\end{array}
\end{array}
if y < -6.4e13 or 2.2499999999999999e-66 < y Initial program 100.0%
Taylor expanded in x around 0 75.9%
if -6.4e13 < y < 2.2499999999999999e-66Initial program 100.0%
Taylor expanded in x around inf 82.8%
Final simplification79.4%
(FPCore (x y) :precision binary64 (if (or (<= y -1.7e+14) (not (<= y 2.25e-66))) (- -1.0 (/ x y)) (/ x (- x y))))
double code(double x, double y) {
double tmp;
if ((y <= -1.7e+14) || !(y <= 2.25e-66)) {
tmp = -1.0 - (x / y);
} else {
tmp = x / (x - y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y <= (-1.7d+14)) .or. (.not. (y <= 2.25d-66))) then
tmp = (-1.0d0) - (x / y)
else
tmp = x / (x - y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -1.7e+14) || !(y <= 2.25e-66)) {
tmp = -1.0 - (x / y);
} else {
tmp = x / (x - y);
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -1.7e+14) or not (y <= 2.25e-66): tmp = -1.0 - (x / y) else: tmp = x / (x - y) return tmp
function code(x, y) tmp = 0.0 if ((y <= -1.7e+14) || !(y <= 2.25e-66)) tmp = Float64(-1.0 - Float64(x / y)); else tmp = Float64(x / Float64(x - y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -1.7e+14) || ~((y <= 2.25e-66))) tmp = -1.0 - (x / y); else tmp = x / (x - y); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -1.7e+14], N[Not[LessEqual[y, 2.25e-66]], $MachinePrecision]], N[(-1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(x / N[(x - y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.7 \cdot 10^{+14} \lor \neg \left(y \leq 2.25 \cdot 10^{-66}\right):\\
\;\;\;\;-1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{x - y}\\
\end{array}
\end{array}
if y < -1.7e14 or 2.2499999999999999e-66 < y Initial program 100.0%
Taylor expanded in x around 0 75.4%
neg-mul-175.4%
Simplified75.4%
Taylor expanded in x around 0 75.4%
sub-neg75.4%
*-commutative75.4%
metadata-eval75.4%
distribute-lft1-in75.4%
*-rgt-identity75.4%
associate-*r/75.4%
rgt-mult-inverse75.2%
distribute-lft-in75.2%
+-commutative75.2%
*-commutative75.2%
mul-1-neg75.2%
neg-sub075.2%
distribute-lft-in75.2%
rgt-mult-inverse75.4%
associate--r+75.4%
metadata-eval75.4%
associate-*r/75.4%
*-rgt-identity75.4%
Simplified75.4%
if -1.7e14 < y < 2.2499999999999999e-66Initial program 100.0%
Taylor expanded in x around inf 82.8%
Final simplification79.1%
(FPCore (x y) :precision binary64 (if (or (<= y -1.7e+14) (not (<= y 2.25e-66))) (- -1.0 (/ x y)) (+ 1.0 (/ y x))))
double code(double x, double y) {
double tmp;
if ((y <= -1.7e+14) || !(y <= 2.25e-66)) {
tmp = -1.0 - (x / y);
} else {
tmp = 1.0 + (y / x);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y <= (-1.7d+14)) .or. (.not. (y <= 2.25d-66))) then
tmp = (-1.0d0) - (x / y)
else
tmp = 1.0d0 + (y / x)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -1.7e+14) || !(y <= 2.25e-66)) {
tmp = -1.0 - (x / y);
} else {
tmp = 1.0 + (y / x);
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -1.7e+14) or not (y <= 2.25e-66): tmp = -1.0 - (x / y) else: tmp = 1.0 + (y / x) return tmp
function code(x, y) tmp = 0.0 if ((y <= -1.7e+14) || !(y <= 2.25e-66)) tmp = Float64(-1.0 - Float64(x / y)); else tmp = Float64(1.0 + Float64(y / x)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -1.7e+14) || ~((y <= 2.25e-66))) tmp = -1.0 - (x / y); else tmp = 1.0 + (y / x); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -1.7e+14], N[Not[LessEqual[y, 2.25e-66]], $MachinePrecision]], N[(-1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(y / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.7 \cdot 10^{+14} \lor \neg \left(y \leq 2.25 \cdot 10^{-66}\right):\\
\;\;\;\;-1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{y}{x}\\
\end{array}
\end{array}
if y < -1.7e14 or 2.2499999999999999e-66 < y Initial program 100.0%
Taylor expanded in x around 0 75.4%
neg-mul-175.4%
Simplified75.4%
Taylor expanded in x around 0 75.4%
sub-neg75.4%
*-commutative75.4%
metadata-eval75.4%
distribute-lft1-in75.4%
*-rgt-identity75.4%
associate-*r/75.4%
rgt-mult-inverse75.2%
distribute-lft-in75.2%
+-commutative75.2%
*-commutative75.2%
mul-1-neg75.2%
neg-sub075.2%
distribute-lft-in75.2%
rgt-mult-inverse75.4%
associate--r+75.4%
metadata-eval75.4%
associate-*r/75.4%
*-rgt-identity75.4%
Simplified75.4%
if -1.7e14 < y < 2.2499999999999999e-66Initial program 100.0%
Taylor expanded in x around inf 82.8%
Taylor expanded in x around inf 82.5%
Final simplification79.0%
(FPCore (x y) :precision binary64 (if (<= y -1.5e+14) -1.0 (if (<= y 6e-67) (+ 1.0 (/ y x)) -1.0)))
double code(double x, double y) {
double tmp;
if (y <= -1.5e+14) {
tmp = -1.0;
} else if (y <= 6e-67) {
tmp = 1.0 + (y / x);
} else {
tmp = -1.0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-1.5d+14)) then
tmp = -1.0d0
else if (y <= 6d-67) then
tmp = 1.0d0 + (y / x)
else
tmp = -1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -1.5e+14) {
tmp = -1.0;
} else if (y <= 6e-67) {
tmp = 1.0 + (y / x);
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -1.5e+14: tmp = -1.0 elif y <= 6e-67: tmp = 1.0 + (y / x) else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -1.5e+14) tmp = -1.0; elseif (y <= 6e-67) tmp = Float64(1.0 + Float64(y / x)); else tmp = -1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -1.5e+14) tmp = -1.0; elseif (y <= 6e-67) tmp = 1.0 + (y / x); else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -1.5e+14], -1.0, If[LessEqual[y, 6e-67], N[(1.0 + N[(y / x), $MachinePrecision]), $MachinePrecision], -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.5 \cdot 10^{+14}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 6 \cdot 10^{-67}:\\
\;\;\;\;1 + \frac{y}{x}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < -1.5e14 or 6.00000000000000065e-67 < y Initial program 100.0%
Taylor expanded in x around 0 74.6%
if -1.5e14 < y < 6.00000000000000065e-67Initial program 100.0%
Taylor expanded in x around inf 83.3%
Taylor expanded in x around inf 83.0%
(FPCore (x y) :precision binary64 (if (<= y -6500000000000.0) -1.0 (if (<= y 6e-67) 1.0 -1.0)))
double code(double x, double y) {
double tmp;
if (y <= -6500000000000.0) {
tmp = -1.0;
} else if (y <= 6e-67) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-6500000000000.0d0)) then
tmp = -1.0d0
else if (y <= 6d-67) then
tmp = 1.0d0
else
tmp = -1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -6500000000000.0) {
tmp = -1.0;
} else if (y <= 6e-67) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -6500000000000.0: tmp = -1.0 elif y <= 6e-67: tmp = 1.0 else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -6500000000000.0) tmp = -1.0; elseif (y <= 6e-67) tmp = 1.0; else tmp = -1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -6500000000000.0) tmp = -1.0; elseif (y <= 6e-67) tmp = 1.0; else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -6500000000000.0], -1.0, If[LessEqual[y, 6e-67], 1.0, -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -6500000000000:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 6 \cdot 10^{-67}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < -6.5e12 or 6.00000000000000065e-67 < y Initial program 100.0%
Taylor expanded in x around 0 74.6%
if -6.5e12 < y < 6.00000000000000065e-67Initial program 100.0%
Taylor expanded in x around inf 82.7%
(FPCore (x y) :precision binary64 -1.0)
double code(double x, double y) {
return -1.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = -1.0d0
end function
public static double code(double x, double y) {
return -1.0;
}
def code(x, y): return -1.0
function code(x, y) return -1.0 end
function tmp = code(x, y) tmp = -1.0; end
code[x_, y_] := -1.0
\begin{array}{l}
\\
-1
\end{array}
Initial program 100.0%
Taylor expanded in x around 0 46.1%
(FPCore (x y) :precision binary64 (/ 1.0 (- (/ x (+ x y)) (/ y (+ x y)))))
double code(double x, double y) {
return 1.0 / ((x / (x + y)) - (y / (x + y)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0 / ((x / (x + y)) - (y / (x + y)))
end function
public static double code(double x, double y) {
return 1.0 / ((x / (x + y)) - (y / (x + y)));
}
def code(x, y): return 1.0 / ((x / (x + y)) - (y / (x + y)))
function code(x, y) return Float64(1.0 / Float64(Float64(x / Float64(x + y)) - Float64(y / Float64(x + y)))) end
function tmp = code(x, y) tmp = 1.0 / ((x / (x + y)) - (y / (x + y))); end
code[x_, y_] := N[(1.0 / N[(N[(x / N[(x + y), $MachinePrecision]), $MachinePrecision] - N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{x}{x + y} - \frac{y}{x + y}}
\end{array}
herbie shell --seed 2024165
(FPCore (x y)
:name "Linear.Projection:perspective from linear-1.19.1.3, A"
:precision binary64
:alt
(! :herbie-platform default (/ 1 (- (/ x (+ x y)) (/ y (+ x y)))))
(/ (+ x y) (- x y)))