
(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 (<= x -4.9e+73) (/ (+ x y) x) (if (<= x 1.36e-42) (+ (* -2.0 (/ x y)) -1.0) (+ 1.0 (* 2.0 (/ y x))))))
double code(double x, double y) {
double tmp;
if (x <= -4.9e+73) {
tmp = (x + y) / x;
} else if (x <= 1.36e-42) {
tmp = (-2.0 * (x / y)) + -1.0;
} 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 (x <= (-4.9d+73)) then
tmp = (x + y) / x
else if (x <= 1.36d-42) then
tmp = ((-2.0d0) * (x / y)) + (-1.0d0)
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 (x <= -4.9e+73) {
tmp = (x + y) / x;
} else if (x <= 1.36e-42) {
tmp = (-2.0 * (x / y)) + -1.0;
} else {
tmp = 1.0 + (2.0 * (y / x));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -4.9e+73: tmp = (x + y) / x elif x <= 1.36e-42: tmp = (-2.0 * (x / y)) + -1.0 else: tmp = 1.0 + (2.0 * (y / x)) return tmp
function code(x, y) tmp = 0.0 if (x <= -4.9e+73) tmp = Float64(Float64(x + y) / x); elseif (x <= 1.36e-42) tmp = Float64(Float64(-2.0 * Float64(x / y)) + -1.0); 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 (x <= -4.9e+73) tmp = (x + y) / x; elseif (x <= 1.36e-42) tmp = (-2.0 * (x / y)) + -1.0; else tmp = 1.0 + (2.0 * (y / x)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -4.9e+73], N[(N[(x + y), $MachinePrecision] / x), $MachinePrecision], If[LessEqual[x, 1.36e-42], N[(N[(-2.0 * N[(x / y), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], N[(1.0 + N[(2.0 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.9 \cdot 10^{+73}:\\
\;\;\;\;\frac{x + y}{x}\\
\mathbf{elif}\;x \leq 1.36 \cdot 10^{-42}:\\
\;\;\;\;-2 \cdot \frac{x}{y} + -1\\
\mathbf{else}:\\
\;\;\;\;1 + 2 \cdot \frac{y}{x}\\
\end{array}
\end{array}
if x < -4.8999999999999999e73Initial program 100.0%
Taylor expanded in x around inf 99.9%
mul-1-neg99.9%
unsub-neg99.9%
Simplified99.9%
Taylor expanded in x around inf 78.7%
if -4.8999999999999999e73 < x < 1.36e-42Initial program 100.0%
Taylor expanded in x around 0 82.3%
if 1.36e-42 < x Initial program 99.9%
Taylor expanded in y around 0 71.7%
Final simplification78.6%
(FPCore (x y) :precision binary64 (if (<= x -4.2e+71) (/ (+ x y) x) (if (<= x 1.9e-98) (/ y (- x y)) (+ 1.0 (* 2.0 (/ y x))))))
double code(double x, double y) {
double tmp;
if (x <= -4.2e+71) {
tmp = (x + y) / x;
} else if (x <= 1.9e-98) {
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 (x <= (-4.2d+71)) then
tmp = (x + y) / x
else if (x <= 1.9d-98) 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 (x <= -4.2e+71) {
tmp = (x + y) / x;
} else if (x <= 1.9e-98) {
tmp = y / (x - y);
} else {
tmp = 1.0 + (2.0 * (y / x));
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -4.2e+71: tmp = (x + y) / x elif x <= 1.9e-98: tmp = y / (x - y) else: tmp = 1.0 + (2.0 * (y / x)) return tmp
function code(x, y) tmp = 0.0 if (x <= -4.2e+71) tmp = Float64(Float64(x + y) / x); elseif (x <= 1.9e-98) 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 (x <= -4.2e+71) tmp = (x + y) / x; elseif (x <= 1.9e-98) tmp = y / (x - y); else tmp = 1.0 + (2.0 * (y / x)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -4.2e+71], N[(N[(x + y), $MachinePrecision] / x), $MachinePrecision], If[LessEqual[x, 1.9e-98], 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}\;x \leq -4.2 \cdot 10^{+71}:\\
\;\;\;\;\frac{x + y}{x}\\
\mathbf{elif}\;x \leq 1.9 \cdot 10^{-98}:\\
\;\;\;\;\frac{y}{x - y}\\
\mathbf{else}:\\
\;\;\;\;1 + 2 \cdot \frac{y}{x}\\
\end{array}
\end{array}
if x < -4.19999999999999978e71Initial program 100.0%
Taylor expanded in x around inf 99.9%
mul-1-neg99.9%
unsub-neg99.9%
Simplified99.9%
Taylor expanded in x around inf 78.7%
if -4.19999999999999978e71 < x < 1.9000000000000002e-98Initial program 100.0%
Taylor expanded in x around 0 84.4%
if 1.9000000000000002e-98 < x Initial program 99.9%
Taylor expanded in y around 0 69.3%
(FPCore (x y) :precision binary64 (if (or (<= x -6.6e+72) (not (<= x 9e-60))) (/ x (- x y)) (/ y (- x y))))
double code(double x, double y) {
double tmp;
if ((x <= -6.6e+72) || !(x <= 9e-60)) {
tmp = x / (x - y);
} else {
tmp = y / (x - y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((x <= (-6.6d+72)) .or. (.not. (x <= 9d-60))) then
tmp = x / (x - y)
else
tmp = y / (x - y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -6.6e+72) || !(x <= 9e-60)) {
tmp = x / (x - y);
} else {
tmp = y / (x - y);
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -6.6e+72) or not (x <= 9e-60): tmp = x / (x - y) else: tmp = y / (x - y) return tmp
function code(x, y) tmp = 0.0 if ((x <= -6.6e+72) || !(x <= 9e-60)) tmp = Float64(x / Float64(x - y)); else tmp = Float64(y / Float64(x - y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -6.6e+72) || ~((x <= 9e-60))) tmp = x / (x - y); else tmp = y / (x - y); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -6.6e+72], N[Not[LessEqual[x, 9e-60]], $MachinePrecision]], N[(x / N[(x - y), $MachinePrecision]), $MachinePrecision], N[(y / N[(x - y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6.6 \cdot 10^{+72} \lor \neg \left(x \leq 9 \cdot 10^{-60}\right):\\
\;\;\;\;\frac{x}{x - y}\\
\mathbf{else}:\\
\;\;\;\;\frac{y}{x - y}\\
\end{array}
\end{array}
if x < -6.6e72 or 9.00000000000000001e-60 < x Initial program 99.9%
Taylor expanded in x around inf 73.3%
if -6.6e72 < x < 9.00000000000000001e-60Initial program 100.0%
Taylor expanded in x around 0 82.4%
Final simplification78.3%
(FPCore (x y) :precision binary64 (if (or (<= x -1.25e+72) (not (<= x 4e-41))) (/ x (- x y)) (- -1.0 (/ x y))))
double code(double x, double y) {
double tmp;
if ((x <= -1.25e+72) || !(x <= 4e-41)) {
tmp = x / (x - y);
} else {
tmp = -1.0 - (x / y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((x <= (-1.25d+72)) .or. (.not. (x <= 4d-41))) then
tmp = x / (x - y)
else
tmp = (-1.0d0) - (x / y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -1.25e+72) || !(x <= 4e-41)) {
tmp = x / (x - y);
} else {
tmp = -1.0 - (x / y);
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -1.25e+72) or not (x <= 4e-41): tmp = x / (x - y) else: tmp = -1.0 - (x / y) return tmp
function code(x, y) tmp = 0.0 if ((x <= -1.25e+72) || !(x <= 4e-41)) tmp = Float64(x / Float64(x - y)); else tmp = Float64(-1.0 - Float64(x / y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -1.25e+72) || ~((x <= 4e-41))) tmp = x / (x - y); else tmp = -1.0 - (x / y); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -1.25e+72], N[Not[LessEqual[x, 4e-41]], $MachinePrecision]], N[(x / N[(x - y), $MachinePrecision]), $MachinePrecision], N[(-1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.25 \cdot 10^{+72} \lor \neg \left(x \leq 4 \cdot 10^{-41}\right):\\
\;\;\;\;\frac{x}{x - y}\\
\mathbf{else}:\\
\;\;\;\;-1 - \frac{x}{y}\\
\end{array}
\end{array}
if x < -1.24999999999999998e72 or 4.00000000000000002e-41 < x Initial program 99.9%
Taylor expanded in x around inf 73.7%
if -1.24999999999999998e72 < x < 4.00000000000000002e-41Initial program 100.0%
Taylor expanded in x around 0 81.9%
Taylor expanded in y around inf 81.9%
sub-neg81.9%
metadata-eval81.9%
+-commutative81.9%
mul-1-neg81.9%
sub-neg81.9%
Simplified81.9%
Final simplification78.2%
(FPCore (x y) :precision binary64 (if (<= x -1.3e+73) (/ (+ x y) x) (if (<= x 1.25e-59) (/ y (- x y)) (/ x (- x y)))))
double code(double x, double y) {
double tmp;
if (x <= -1.3e+73) {
tmp = (x + y) / x;
} else if (x <= 1.25e-59) {
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 (x <= (-1.3d+73)) then
tmp = (x + y) / x
else if (x <= 1.25d-59) 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 (x <= -1.3e+73) {
tmp = (x + y) / x;
} else if (x <= 1.25e-59) {
tmp = y / (x - y);
} else {
tmp = x / (x - y);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1.3e+73: tmp = (x + y) / x elif x <= 1.25e-59: tmp = y / (x - y) else: tmp = x / (x - y) return tmp
function code(x, y) tmp = 0.0 if (x <= -1.3e+73) tmp = Float64(Float64(x + y) / x); elseif (x <= 1.25e-59) 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 (x <= -1.3e+73) tmp = (x + y) / x; elseif (x <= 1.25e-59) tmp = y / (x - y); else tmp = x / (x - y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1.3e+73], N[(N[(x + y), $MachinePrecision] / x), $MachinePrecision], If[LessEqual[x, 1.25e-59], N[(y / N[(x - y), $MachinePrecision]), $MachinePrecision], N[(x / N[(x - y), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.3 \cdot 10^{+73}:\\
\;\;\;\;\frac{x + y}{x}\\
\mathbf{elif}\;x \leq 1.25 \cdot 10^{-59}:\\
\;\;\;\;\frac{y}{x - y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{x - y}\\
\end{array}
\end{array}
if x < -1.3e73Initial program 100.0%
Taylor expanded in x around inf 99.9%
mul-1-neg99.9%
unsub-neg99.9%
Simplified99.9%
Taylor expanded in x around inf 78.7%
if -1.3e73 < x < 1.25e-59Initial program 100.0%
Taylor expanded in x around 0 82.4%
if 1.25e-59 < x Initial program 99.9%
Taylor expanded in x around inf 70.8%
(FPCore (x y) :precision binary64 (if (<= x -3.8e+71) 1.0 (if (<= x 1.32e-39) (- -1.0 (/ x y)) 1.0)))
double code(double x, double y) {
double tmp;
if (x <= -3.8e+71) {
tmp = 1.0;
} else if (x <= 1.32e-39) {
tmp = -1.0 - (x / y);
} 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 (x <= (-3.8d+71)) then
tmp = 1.0d0
else if (x <= 1.32d-39) then
tmp = (-1.0d0) - (x / y)
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -3.8e+71) {
tmp = 1.0;
} else if (x <= 1.32e-39) {
tmp = -1.0 - (x / y);
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -3.8e+71: tmp = 1.0 elif x <= 1.32e-39: tmp = -1.0 - (x / y) else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= -3.8e+71) tmp = 1.0; elseif (x <= 1.32e-39) tmp = Float64(-1.0 - Float64(x / y)); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -3.8e+71) tmp = 1.0; elseif (x <= 1.32e-39) tmp = -1.0 - (x / y); else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -3.8e+71], 1.0, If[LessEqual[x, 1.32e-39], N[(-1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.8 \cdot 10^{+71}:\\
\;\;\;\;1\\
\mathbf{elif}\;x \leq 1.32 \cdot 10^{-39}:\\
\;\;\;\;-1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -3.8000000000000001e71 or 1.31999999999999997e-39 < x Initial program 99.9%
Taylor expanded in x around inf 72.9%
if -3.8000000000000001e71 < x < 1.31999999999999997e-39Initial program 100.0%
Taylor expanded in x around 0 81.9%
Taylor expanded in y around inf 81.9%
sub-neg81.9%
metadata-eval81.9%
+-commutative81.9%
mul-1-neg81.9%
sub-neg81.9%
Simplified81.9%
(FPCore (x y) :precision binary64 (if (<= x -4.4e+72) 1.0 (if (<= x 1e-59) -1.0 1.0)))
double code(double x, double y) {
double tmp;
if (x <= -4.4e+72) {
tmp = 1.0;
} else if (x <= 1e-59) {
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 (x <= (-4.4d+72)) then
tmp = 1.0d0
else if (x <= 1d-59) 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 (x <= -4.4e+72) {
tmp = 1.0;
} else if (x <= 1e-59) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -4.4e+72: tmp = 1.0 elif x <= 1e-59: tmp = -1.0 else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= -4.4e+72) tmp = 1.0; elseif (x <= 1e-59) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -4.4e+72) tmp = 1.0; elseif (x <= 1e-59) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -4.4e+72], 1.0, If[LessEqual[x, 1e-59], -1.0, 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.4 \cdot 10^{+72}:\\
\;\;\;\;1\\
\mathbf{elif}\;x \leq 10^{-59}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -4.4e72 or 1e-59 < x Initial program 99.9%
Taylor expanded in x around inf 72.6%
if -4.4e72 < x < 1e-59Initial program 100.0%
Taylor expanded in x around 0 81.9%
(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 56.8%
(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 2024147
(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)))