
(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 6 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 (+ (/ y (- x y)) (/ x (- x y))))
double code(double x, double y) {
return (y / (x - y)) + (x / (x - y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y / (x - y)) + (x / (x - y))
end function
public static double code(double x, double y) {
return (y / (x - y)) + (x / (x - y));
}
def code(x, y): return (y / (x - y)) + (x / (x - y))
function code(x, y) return Float64(Float64(y / Float64(x - y)) + Float64(x / Float64(x - y))) end
function tmp = code(x, y) tmp = (y / (x - y)) + (x / (x - y)); end
code[x_, y_] := N[(N[(y / N[(x - y), $MachinePrecision]), $MachinePrecision] + N[(x / N[(x - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{y}{x - y} + \frac{x}{x - y}
\end{array}
Initial program 100.0%
clear-num99.9%
inv-pow99.9%
Applied egg-rr99.9%
unpow-199.9%
associate-/r/99.8%
+-commutative99.8%
distribute-rgt-in99.8%
un-div-inv99.9%
un-div-inv100.0%
Applied egg-rr100.0%
(FPCore (x y)
:precision binary64
(if (or (<= x -2.85e+171)
(and (not (<= x -8.6e+122))
(or (<= x -7.6e-28) (not (<= x 8.5e+64)))))
(+ 1.0 (* 2.0 (/ y x)))
(+ (* -2.0 (/ x y)) -1.0)))
double code(double x, double y) {
double tmp;
if ((x <= -2.85e+171) || (!(x <= -8.6e+122) && ((x <= -7.6e-28) || !(x <= 8.5e+64)))) {
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 ((x <= (-2.85d+171)) .or. (.not. (x <= (-8.6d+122))) .and. (x <= (-7.6d-28)) .or. (.not. (x <= 8.5d+64))) 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 ((x <= -2.85e+171) || (!(x <= -8.6e+122) && ((x <= -7.6e-28) || !(x <= 8.5e+64)))) {
tmp = 1.0 + (2.0 * (y / x));
} else {
tmp = (-2.0 * (x / y)) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -2.85e+171) or (not (x <= -8.6e+122) and ((x <= -7.6e-28) or not (x <= 8.5e+64))): 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 ((x <= -2.85e+171) || (!(x <= -8.6e+122) && ((x <= -7.6e-28) || !(x <= 8.5e+64)))) 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 ((x <= -2.85e+171) || (~((x <= -8.6e+122)) && ((x <= -7.6e-28) || ~((x <= 8.5e+64))))) tmp = 1.0 + (2.0 * (y / x)); else tmp = (-2.0 * (x / y)) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -2.85e+171], And[N[Not[LessEqual[x, -8.6e+122]], $MachinePrecision], Or[LessEqual[x, -7.6e-28], N[Not[LessEqual[x, 8.5e+64]], $MachinePrecision]]]], 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}\;x \leq -2.85 \cdot 10^{+171} \lor \neg \left(x \leq -8.6 \cdot 10^{+122}\right) \land \left(x \leq -7.6 \cdot 10^{-28} \lor \neg \left(x \leq 8.5 \cdot 10^{+64}\right)\right):\\
\;\;\;\;1 + 2 \cdot \frac{y}{x}\\
\mathbf{else}:\\
\;\;\;\;-2 \cdot \frac{x}{y} + -1\\
\end{array}
\end{array}
if x < -2.85e171 or -8.59999999999999943e122 < x < -7.60000000000000018e-28 or 8.4999999999999998e64 < x Initial program 100.0%
Taylor expanded in y around 0 85.9%
if -2.85e171 < x < -8.59999999999999943e122 or -7.60000000000000018e-28 < x < 8.4999999999999998e64Initial program 99.9%
Taylor expanded in x around 0 74.8%
Final simplification79.4%
(FPCore (x y)
:precision binary64
(if (or (<= x -7.5e+166)
(not (or (<= x -8.6e+122) (and (not (<= x -4e-28)) (<= x 2.3e+64)))))
(+ 1.0 (* 2.0 (/ y x)))
-1.0))
double code(double x, double y) {
double tmp;
if ((x <= -7.5e+166) || !((x <= -8.6e+122) || (!(x <= -4e-28) && (x <= 2.3e+64)))) {
tmp = 1.0 + (2.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 ((x <= (-7.5d+166)) .or. (.not. (x <= (-8.6d+122)) .or. (.not. (x <= (-4d-28))) .and. (x <= 2.3d+64))) then
tmp = 1.0d0 + (2.0d0 * (y / x))
else
tmp = -1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -7.5e+166) || !((x <= -8.6e+122) || (!(x <= -4e-28) && (x <= 2.3e+64)))) {
tmp = 1.0 + (2.0 * (y / x));
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -7.5e+166) or not ((x <= -8.6e+122) or (not (x <= -4e-28) and (x <= 2.3e+64))): tmp = 1.0 + (2.0 * (y / x)) else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if ((x <= -7.5e+166) || !((x <= -8.6e+122) || (!(x <= -4e-28) && (x <= 2.3e+64)))) tmp = Float64(1.0 + Float64(2.0 * Float64(y / x))); else tmp = -1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -7.5e+166) || ~(((x <= -8.6e+122) || (~((x <= -4e-28)) && (x <= 2.3e+64))))) tmp = 1.0 + (2.0 * (y / x)); else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -7.5e+166], N[Not[Or[LessEqual[x, -8.6e+122], And[N[Not[LessEqual[x, -4e-28]], $MachinePrecision], LessEqual[x, 2.3e+64]]]], $MachinePrecision]], N[(1.0 + N[(2.0 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.5 \cdot 10^{+166} \lor \neg \left(x \leq -8.6 \cdot 10^{+122} \lor \neg \left(x \leq -4 \cdot 10^{-28}\right) \land x \leq 2.3 \cdot 10^{+64}\right):\\
\;\;\;\;1 + 2 \cdot \frac{y}{x}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if x < -7.50000000000000029e166 or -8.59999999999999943e122 < x < -3.99999999999999988e-28 or 2.3e64 < x Initial program 100.0%
Taylor expanded in y around 0 85.4%
if -7.50000000000000029e166 < x < -8.59999999999999943e122 or -3.99999999999999988e-28 < x < 2.3e64Initial program 99.9%
Taylor expanded in x around 0 73.4%
Final simplification78.5%
(FPCore (x y)
:precision binary64
(if (<= x -2.85e+171)
1.0
(if (<= x -8.6e+122)
-1.0
(if (<= x -3.7e-28) 1.0 (if (<= x 4e+64) -1.0 1.0)))))
double code(double x, double y) {
double tmp;
if (x <= -2.85e+171) {
tmp = 1.0;
} else if (x <= -8.6e+122) {
tmp = -1.0;
} else if (x <= -3.7e-28) {
tmp = 1.0;
} else if (x <= 4e+64) {
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 <= (-2.85d+171)) then
tmp = 1.0d0
else if (x <= (-8.6d+122)) then
tmp = -1.0d0
else if (x <= (-3.7d-28)) then
tmp = 1.0d0
else if (x <= 4d+64) 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 <= -2.85e+171) {
tmp = 1.0;
} else if (x <= -8.6e+122) {
tmp = -1.0;
} else if (x <= -3.7e-28) {
tmp = 1.0;
} else if (x <= 4e+64) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2.85e+171: tmp = 1.0 elif x <= -8.6e+122: tmp = -1.0 elif x <= -3.7e-28: tmp = 1.0 elif x <= 4e+64: tmp = -1.0 else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= -2.85e+171) tmp = 1.0; elseif (x <= -8.6e+122) tmp = -1.0; elseif (x <= -3.7e-28) tmp = 1.0; elseif (x <= 4e+64) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -2.85e+171) tmp = 1.0; elseif (x <= -8.6e+122) tmp = -1.0; elseif (x <= -3.7e-28) tmp = 1.0; elseif (x <= 4e+64) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2.85e+171], 1.0, If[LessEqual[x, -8.6e+122], -1.0, If[LessEqual[x, -3.7e-28], 1.0, If[LessEqual[x, 4e+64], -1.0, 1.0]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.85 \cdot 10^{+171}:\\
\;\;\;\;1\\
\mathbf{elif}\;x \leq -8.6 \cdot 10^{+122}:\\
\;\;\;\;-1\\
\mathbf{elif}\;x \leq -3.7 \cdot 10^{-28}:\\
\;\;\;\;1\\
\mathbf{elif}\;x \leq 4 \cdot 10^{+64}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -2.85e171 or -8.59999999999999943e122 < x < -3.7000000000000002e-28 or 4.00000000000000009e64 < x Initial program 100.0%
Taylor expanded in x around inf 84.9%
if -2.85e171 < x < -8.59999999999999943e122 or -3.7000000000000002e-28 < x < 4.00000000000000009e64Initial program 99.9%
Taylor expanded in x around 0 73.1%
(FPCore (x y) :precision binary64 (/ (+ y x) (- x y)))
double code(double x, double y) {
return (y + x) / (x - y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (y + x) / (x - y)
end function
public static double code(double x, double y) {
return (y + x) / (x - y);
}
def code(x, y): return (y + x) / (x - y)
function code(x, y) return Float64(Float64(y + x) / Float64(x - y)) end
function tmp = code(x, y) tmp = (y + x) / (x - y); end
code[x_, y_] := N[(N[(y + x), $MachinePrecision] / N[(x - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{y + x}{x - y}
\end{array}
Initial program 100.0%
Final simplification100.0%
(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 48.7%
(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 2024103
(FPCore (x y)
:name "Linear.Projection:perspective from linear-1.19.1.3, A"
:precision binary64
:alt
(/ 1.0 (- (/ x (+ x y)) (/ y (+ x y))))
(/ (+ x y) (- x y)))