
(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 5 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 99.9%
Final simplification99.9%
(FPCore (x y)
:precision binary64
(if (<= y -2.9e+125)
-1.0
(if (or (<= y -6.4e+113)
(and (not (<= y -2.6e+18))
(or (<= y 1.45e+43)
(and (not (<= y 3.7e+70)) (<= y 6.6e+131)))))
(+ 1.0 (* 2.0 (/ y x)))
-1.0)))
double code(double x, double y) {
double tmp;
if (y <= -2.9e+125) {
tmp = -1.0;
} else if ((y <= -6.4e+113) || (!(y <= -2.6e+18) && ((y <= 1.45e+43) || (!(y <= 3.7e+70) && (y <= 6.6e+131))))) {
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 (y <= (-2.9d+125)) then
tmp = -1.0d0
else if ((y <= (-6.4d+113)) .or. (.not. (y <= (-2.6d+18))) .and. (y <= 1.45d+43) .or. (.not. (y <= 3.7d+70)) .and. (y <= 6.6d+131)) 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 (y <= -2.9e+125) {
tmp = -1.0;
} else if ((y <= -6.4e+113) || (!(y <= -2.6e+18) && ((y <= 1.45e+43) || (!(y <= 3.7e+70) && (y <= 6.6e+131))))) {
tmp = 1.0 + (2.0 * (y / x));
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -2.9e+125: tmp = -1.0 elif (y <= -6.4e+113) or (not (y <= -2.6e+18) and ((y <= 1.45e+43) or (not (y <= 3.7e+70) and (y <= 6.6e+131)))): tmp = 1.0 + (2.0 * (y / x)) else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -2.9e+125) tmp = -1.0; elseif ((y <= -6.4e+113) || (!(y <= -2.6e+18) && ((y <= 1.45e+43) || (!(y <= 3.7e+70) && (y <= 6.6e+131))))) 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 (y <= -2.9e+125) tmp = -1.0; elseif ((y <= -6.4e+113) || (~((y <= -2.6e+18)) && ((y <= 1.45e+43) || (~((y <= 3.7e+70)) && (y <= 6.6e+131))))) tmp = 1.0 + (2.0 * (y / x)); else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -2.9e+125], -1.0, If[Or[LessEqual[y, -6.4e+113], And[N[Not[LessEqual[y, -2.6e+18]], $MachinePrecision], Or[LessEqual[y, 1.45e+43], And[N[Not[LessEqual[y, 3.7e+70]], $MachinePrecision], LessEqual[y, 6.6e+131]]]]], N[(1.0 + N[(2.0 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.9 \cdot 10^{+125}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq -6.4 \cdot 10^{+113} \lor \neg \left(y \leq -2.6 \cdot 10^{+18}\right) \land \left(y \leq 1.45 \cdot 10^{+43} \lor \neg \left(y \leq 3.7 \cdot 10^{+70}\right) \land y \leq 6.6 \cdot 10^{+131}\right):\\
\;\;\;\;1 + 2 \cdot \frac{y}{x}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < -2.89999999999999993e125 or -6.3999999999999996e113 < y < -2.6e18 or 1.4500000000000001e43 < y < 3.69999999999999989e70 or 6.5999999999999997e131 < y Initial program 99.9%
Taylor expanded in x around 0 89.3%
if -2.89999999999999993e125 < y < -6.3999999999999996e113 or -2.6e18 < y < 1.4500000000000001e43 or 3.69999999999999989e70 < y < 6.5999999999999997e131Initial program 99.9%
Taylor expanded in y around 0 82.2%
Final simplification85.2%
(FPCore (x y)
:precision binary64
(let* ((t_0 (+ (* -2.0 (/ x y)) -1.0)) (t_1 (+ 1.0 (* 2.0 (/ y x)))))
(if (<= y -1.25e+124)
t_0
(if (<= y -6.4e+113)
t_1
(if (<= y -28000000000000.0)
t_0
(if (<= y 1.95e+42)
t_1
(if (<= y 1.45e+70) -1.0 (if (<= y 1.65e+131) t_1 t_0))))))))
double code(double x, double y) {
double t_0 = (-2.0 * (x / y)) + -1.0;
double t_1 = 1.0 + (2.0 * (y / x));
double tmp;
if (y <= -1.25e+124) {
tmp = t_0;
} else if (y <= -6.4e+113) {
tmp = t_1;
} else if (y <= -28000000000000.0) {
tmp = t_0;
} else if (y <= 1.95e+42) {
tmp = t_1;
} else if (y <= 1.45e+70) {
tmp = -1.0;
} else if (y <= 1.65e+131) {
tmp = t_1;
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = ((-2.0d0) * (x / y)) + (-1.0d0)
t_1 = 1.0d0 + (2.0d0 * (y / x))
if (y <= (-1.25d+124)) then
tmp = t_0
else if (y <= (-6.4d+113)) then
tmp = t_1
else if (y <= (-28000000000000.0d0)) then
tmp = t_0
else if (y <= 1.95d+42) then
tmp = t_1
else if (y <= 1.45d+70) then
tmp = -1.0d0
else if (y <= 1.65d+131) then
tmp = t_1
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = (-2.0 * (x / y)) + -1.0;
double t_1 = 1.0 + (2.0 * (y / x));
double tmp;
if (y <= -1.25e+124) {
tmp = t_0;
} else if (y <= -6.4e+113) {
tmp = t_1;
} else if (y <= -28000000000000.0) {
tmp = t_0;
} else if (y <= 1.95e+42) {
tmp = t_1;
} else if (y <= 1.45e+70) {
tmp = -1.0;
} else if (y <= 1.65e+131) {
tmp = t_1;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = (-2.0 * (x / y)) + -1.0 t_1 = 1.0 + (2.0 * (y / x)) tmp = 0 if y <= -1.25e+124: tmp = t_0 elif y <= -6.4e+113: tmp = t_1 elif y <= -28000000000000.0: tmp = t_0 elif y <= 1.95e+42: tmp = t_1 elif y <= 1.45e+70: tmp = -1.0 elif y <= 1.65e+131: tmp = t_1 else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(Float64(-2.0 * Float64(x / y)) + -1.0) t_1 = Float64(1.0 + Float64(2.0 * Float64(y / x))) tmp = 0.0 if (y <= -1.25e+124) tmp = t_0; elseif (y <= -6.4e+113) tmp = t_1; elseif (y <= -28000000000000.0) tmp = t_0; elseif (y <= 1.95e+42) tmp = t_1; elseif (y <= 1.45e+70) tmp = -1.0; elseif (y <= 1.65e+131) tmp = t_1; else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = (-2.0 * (x / y)) + -1.0; t_1 = 1.0 + (2.0 * (y / x)); tmp = 0.0; if (y <= -1.25e+124) tmp = t_0; elseif (y <= -6.4e+113) tmp = t_1; elseif (y <= -28000000000000.0) tmp = t_0; elseif (y <= 1.95e+42) tmp = t_1; elseif (y <= 1.45e+70) tmp = -1.0; elseif (y <= 1.65e+131) tmp = t_1; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(-2.0 * N[(x / y), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 + N[(2.0 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -1.25e+124], t$95$0, If[LessEqual[y, -6.4e+113], t$95$1, If[LessEqual[y, -28000000000000.0], t$95$0, If[LessEqual[y, 1.95e+42], t$95$1, If[LessEqual[y, 1.45e+70], -1.0, If[LessEqual[y, 1.65e+131], t$95$1, t$95$0]]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := -2 \cdot \frac{x}{y} + -1\\
t_1 := 1 + 2 \cdot \frac{y}{x}\\
\mathbf{if}\;y \leq -1.25 \cdot 10^{+124}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq -6.4 \cdot 10^{+113}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;y \leq -28000000000000:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 1.95 \cdot 10^{+42}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;y \leq 1.45 \cdot 10^{+70}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 1.65 \cdot 10^{+131}:\\
\;\;\;\;t\_1\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -1.2499999999999999e124 or -6.3999999999999996e113 < y < -2.8e13 or 1.6499999999999999e131 < y Initial program 99.9%
Taylor expanded in x around 0 89.4%
if -1.2499999999999999e124 < y < -6.3999999999999996e113 or -2.8e13 < y < 1.94999999999999985e42 or 1.4499999999999999e70 < y < 1.6499999999999999e131Initial program 99.9%
Taylor expanded in y around 0 82.2%
if 1.94999999999999985e42 < y < 1.4499999999999999e70Initial program 100.0%
Taylor expanded in x around 0 100.0%
Final simplification85.5%
(FPCore (x y)
:precision binary64
(if (<= y -1.25e+124)
-1.0
(if (<= y -1.62e+108)
1.0
(if (<= y -3.8e-37)
-1.0
(if (<= y 5e+42)
1.0
(if (<= y 5e+66) -1.0 (if (<= y 1.65e+131) 1.0 -1.0)))))))
double code(double x, double y) {
double tmp;
if (y <= -1.25e+124) {
tmp = -1.0;
} else if (y <= -1.62e+108) {
tmp = 1.0;
} else if (y <= -3.8e-37) {
tmp = -1.0;
} else if (y <= 5e+42) {
tmp = 1.0;
} else if (y <= 5e+66) {
tmp = -1.0;
} else if (y <= 1.65e+131) {
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 <= (-1.25d+124)) then
tmp = -1.0d0
else if (y <= (-1.62d+108)) then
tmp = 1.0d0
else if (y <= (-3.8d-37)) then
tmp = -1.0d0
else if (y <= 5d+42) then
tmp = 1.0d0
else if (y <= 5d+66) then
tmp = -1.0d0
else if (y <= 1.65d+131) 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 <= -1.25e+124) {
tmp = -1.0;
} else if (y <= -1.62e+108) {
tmp = 1.0;
} else if (y <= -3.8e-37) {
tmp = -1.0;
} else if (y <= 5e+42) {
tmp = 1.0;
} else if (y <= 5e+66) {
tmp = -1.0;
} else if (y <= 1.65e+131) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -1.25e+124: tmp = -1.0 elif y <= -1.62e+108: tmp = 1.0 elif y <= -3.8e-37: tmp = -1.0 elif y <= 5e+42: tmp = 1.0 elif y <= 5e+66: tmp = -1.0 elif y <= 1.65e+131: tmp = 1.0 else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -1.25e+124) tmp = -1.0; elseif (y <= -1.62e+108) tmp = 1.0; elseif (y <= -3.8e-37) tmp = -1.0; elseif (y <= 5e+42) tmp = 1.0; elseif (y <= 5e+66) tmp = -1.0; elseif (y <= 1.65e+131) tmp = 1.0; else tmp = -1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -1.25e+124) tmp = -1.0; elseif (y <= -1.62e+108) tmp = 1.0; elseif (y <= -3.8e-37) tmp = -1.0; elseif (y <= 5e+42) tmp = 1.0; elseif (y <= 5e+66) tmp = -1.0; elseif (y <= 1.65e+131) tmp = 1.0; else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -1.25e+124], -1.0, If[LessEqual[y, -1.62e+108], 1.0, If[LessEqual[y, -3.8e-37], -1.0, If[LessEqual[y, 5e+42], 1.0, If[LessEqual[y, 5e+66], -1.0, If[LessEqual[y, 1.65e+131], 1.0, -1.0]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.25 \cdot 10^{+124}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq -1.62 \cdot 10^{+108}:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq -3.8 \cdot 10^{-37}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 5 \cdot 10^{+42}:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq 5 \cdot 10^{+66}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 1.65 \cdot 10^{+131}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < -1.2499999999999999e124 or -1.61999999999999996e108 < y < -3.8000000000000004e-37 or 5.00000000000000007e42 < y < 4.99999999999999991e66 or 1.6499999999999999e131 < y Initial program 99.9%
Taylor expanded in x around 0 86.7%
if -1.2499999999999999e124 < y < -1.61999999999999996e108 or -3.8000000000000004e-37 < y < 5.00000000000000007e42 or 4.99999999999999991e66 < y < 1.6499999999999999e131Initial program 99.9%
Taylor expanded in x around inf 82.5%
Final simplification84.4%
(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 99.9%
Taylor expanded in x around 0 48.7%
Final simplification48.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 2024027
(FPCore (x y)
:name "Linear.Projection:perspective from linear-1.19.1.3, A"
:precision binary64
:herbie-target
(/ 1.0 (- (/ x (+ x y)) (/ y (+ x y))))
(/ (+ x y) (- x y)))