
(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 99.9%
(FPCore (x y) :precision binary64 (if (<= y -59.0) (/ (+ x y) (- y)) (if (<= y 1.08e-11) (+ 1.0 (* 2.0 (/ y x))) (+ (* -2.0 (/ x y)) -1.0))))
double code(double x, double y) {
double tmp;
if (y <= -59.0) {
tmp = (x + y) / -y;
} else if (y <= 1.08e-11) {
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 <= (-59.0d0)) then
tmp = (x + y) / -y
else if (y <= 1.08d-11) 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 <= -59.0) {
tmp = (x + y) / -y;
} else if (y <= 1.08e-11) {
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 <= -59.0: tmp = (x + y) / -y elif y <= 1.08e-11: 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 <= -59.0) tmp = Float64(Float64(x + y) / Float64(-y)); elseif (y <= 1.08e-11) 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 <= -59.0) tmp = (x + y) / -y; elseif (y <= 1.08e-11) 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, -59.0], N[(N[(x + y), $MachinePrecision] / (-y)), $MachinePrecision], If[LessEqual[y, 1.08e-11], 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 -59:\\
\;\;\;\;\frac{x + y}{-y}\\
\mathbf{elif}\;y \leq 1.08 \cdot 10^{-11}:\\
\;\;\;\;1 + 2 \cdot \frac{y}{x}\\
\mathbf{else}:\\
\;\;\;\;-2 \cdot \frac{x}{y} + -1\\
\end{array}
\end{array}
if y < -59Initial program 100.0%
Taylor expanded in x around 0 75.1%
neg-mul-175.1%
Simplified75.1%
if -59 < y < 1.07999999999999992e-11Initial program 99.9%
Taylor expanded in y around 0 83.4%
if 1.07999999999999992e-11 < y Initial program 99.9%
Taylor expanded in x around 0 82.0%
Final simplification81.3%
(FPCore (x y) :precision binary64 (if (<= y -59.0) (/ (+ x y) (- y)) (if (<= y 7e-12) (+ 1.0 (* 2.0 (/ y x))) (/ y (- x y)))))
double code(double x, double y) {
double tmp;
if (y <= -59.0) {
tmp = (x + y) / -y;
} else if (y <= 7e-12) {
tmp = 1.0 + (2.0 * (y / x));
} 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 (y <= (-59.0d0)) then
tmp = (x + y) / -y
else if (y <= 7d-12) then
tmp = 1.0d0 + (2.0d0 * (y / x))
else
tmp = y / (x - y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -59.0) {
tmp = (x + y) / -y;
} else if (y <= 7e-12) {
tmp = 1.0 + (2.0 * (y / x));
} else {
tmp = y / (x - y);
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -59.0: tmp = (x + y) / -y elif y <= 7e-12: tmp = 1.0 + (2.0 * (y / x)) else: tmp = y / (x - y) return tmp
function code(x, y) tmp = 0.0 if (y <= -59.0) tmp = Float64(Float64(x + y) / Float64(-y)); elseif (y <= 7e-12) tmp = Float64(1.0 + Float64(2.0 * Float64(y / x))); else tmp = Float64(y / Float64(x - y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -59.0) tmp = (x + y) / -y; elseif (y <= 7e-12) tmp = 1.0 + (2.0 * (y / x)); else tmp = y / (x - y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -59.0], N[(N[(x + y), $MachinePrecision] / (-y)), $MachinePrecision], If[LessEqual[y, 7e-12], N[(1.0 + N[(2.0 * N[(y / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y / N[(x - y), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -59:\\
\;\;\;\;\frac{x + y}{-y}\\
\mathbf{elif}\;y \leq 7 \cdot 10^{-12}:\\
\;\;\;\;1 + 2 \cdot \frac{y}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{y}{x - y}\\
\end{array}
\end{array}
if y < -59Initial program 100.0%
Taylor expanded in x around 0 75.1%
neg-mul-175.1%
Simplified75.1%
if -59 < y < 7.0000000000000001e-12Initial program 99.9%
Taylor expanded in y around 0 83.4%
if 7.0000000000000001e-12 < y Initial program 99.9%
Taylor expanded in x around 0 79.8%
(FPCore (x y) :precision binary64 (if (or (<= y -34.0) (not (<= y 6e-12))) (/ y (- x y)) (/ x (- x y))))
double code(double x, double y) {
double tmp;
if ((y <= -34.0) || !(y <= 6e-12)) {
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 <= (-34.0d0)) .or. (.not. (y <= 6d-12))) 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 <= -34.0) || !(y <= 6e-12)) {
tmp = y / (x - y);
} else {
tmp = x / (x - y);
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -34.0) or not (y <= 6e-12): tmp = y / (x - y) else: tmp = x / (x - y) return tmp
function code(x, y) tmp = 0.0 if ((y <= -34.0) || !(y <= 6e-12)) 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 <= -34.0) || ~((y <= 6e-12))) tmp = y / (x - y); else tmp = x / (x - y); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -34.0], N[Not[LessEqual[y, 6e-12]], $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 -34 \lor \neg \left(y \leq 6 \cdot 10^{-12}\right):\\
\;\;\;\;\frac{y}{x - y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{x - y}\\
\end{array}
\end{array}
if y < -34 or 6.0000000000000003e-12 < y Initial program 99.9%
Taylor expanded in x around 0 78.0%
if -34 < y < 6.0000000000000003e-12Initial program 99.9%
Taylor expanded in x around inf 82.8%
Final simplification80.3%
(FPCore (x y) :precision binary64 (if (<= y -21.0) (/ (+ x y) (- y)) (if (<= y 5.9e-12) (/ x (- x y)) (/ y (- x y)))))
double code(double x, double y) {
double tmp;
if (y <= -21.0) {
tmp = (x + y) / -y;
} else if (y <= 5.9e-12) {
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 (y <= (-21.0d0)) then
tmp = (x + y) / -y
else if (y <= 5.9d-12) 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 (y <= -21.0) {
tmp = (x + y) / -y;
} else if (y <= 5.9e-12) {
tmp = x / (x - y);
} else {
tmp = y / (x - y);
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -21.0: tmp = (x + y) / -y elif y <= 5.9e-12: tmp = x / (x - y) else: tmp = y / (x - y) return tmp
function code(x, y) tmp = 0.0 if (y <= -21.0) tmp = Float64(Float64(x + y) / Float64(-y)); elseif (y <= 5.9e-12) 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 (y <= -21.0) tmp = (x + y) / -y; elseif (y <= 5.9e-12) tmp = x / (x - y); else tmp = y / (x - y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -21.0], N[(N[(x + y), $MachinePrecision] / (-y)), $MachinePrecision], If[LessEqual[y, 5.9e-12], N[(x / N[(x - y), $MachinePrecision]), $MachinePrecision], N[(y / N[(x - y), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -21:\\
\;\;\;\;\frac{x + y}{-y}\\
\mathbf{elif}\;y \leq 5.9 \cdot 10^{-12}:\\
\;\;\;\;\frac{x}{x - y}\\
\mathbf{else}:\\
\;\;\;\;\frac{y}{x - y}\\
\end{array}
\end{array}
if y < -21Initial program 100.0%
Taylor expanded in x around 0 75.1%
neg-mul-175.1%
Simplified75.1%
if -21 < y < 5.9e-12Initial program 99.9%
Taylor expanded in x around inf 82.8%
if 5.9e-12 < y Initial program 99.9%
Taylor expanded in x around 0 79.8%
(FPCore (x y) :precision binary64 (if (<= y -28.0) -1.0 (if (<= y 1.95e-7) (/ x (- x y)) -1.0)))
double code(double x, double y) {
double tmp;
if (y <= -28.0) {
tmp = -1.0;
} else if (y <= 1.95e-7) {
tmp = x / (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 (y <= (-28.0d0)) then
tmp = -1.0d0
else if (y <= 1.95d-7) then
tmp = x / (x - y)
else
tmp = -1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -28.0) {
tmp = -1.0;
} else if (y <= 1.95e-7) {
tmp = x / (x - y);
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -28.0: tmp = -1.0 elif y <= 1.95e-7: tmp = x / (x - y) else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -28.0) tmp = -1.0; elseif (y <= 1.95e-7) tmp = Float64(x / Float64(x - y)); else tmp = -1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -28.0) tmp = -1.0; elseif (y <= 1.95e-7) tmp = x / (x - y); else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -28.0], -1.0, If[LessEqual[y, 1.95e-7], N[(x / N[(x - y), $MachinePrecision]), $MachinePrecision], -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -28:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 1.95 \cdot 10^{-7}:\\
\;\;\;\;\frac{x}{x - y}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < -28 or 1.95000000000000012e-7 < y Initial program 99.9%
Taylor expanded in x around 0 77.6%
if -28 < y < 1.95000000000000012e-7Initial program 99.9%
Taylor expanded in x around inf 82.4%
(FPCore (x y) :precision binary64 (if (<= y -13.6) -1.0 (if (<= y 1.7e-11) (+ 1.0 (/ y x)) -1.0)))
double code(double x, double y) {
double tmp;
if (y <= -13.6) {
tmp = -1.0;
} else if (y <= 1.7e-11) {
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 <= (-13.6d0)) then
tmp = -1.0d0
else if (y <= 1.7d-11) 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 <= -13.6) {
tmp = -1.0;
} else if (y <= 1.7e-11) {
tmp = 1.0 + (y / x);
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -13.6: tmp = -1.0 elif y <= 1.7e-11: tmp = 1.0 + (y / x) else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -13.6) tmp = -1.0; elseif (y <= 1.7e-11) 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 <= -13.6) tmp = -1.0; elseif (y <= 1.7e-11) tmp = 1.0 + (y / x); else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -13.6], -1.0, If[LessEqual[y, 1.7e-11], N[(1.0 + N[(y / x), $MachinePrecision]), $MachinePrecision], -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -13.6:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 1.7 \cdot 10^{-11}:\\
\;\;\;\;1 + \frac{y}{x}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < -13.5999999999999996 or 1.6999999999999999e-11 < y Initial program 99.9%
Taylor expanded in x around 0 77.2%
if -13.5999999999999996 < y < 1.6999999999999999e-11Initial program 99.9%
Taylor expanded in x around inf 82.8%
Taylor expanded in x around inf 82.8%
(FPCore (x y) :precision binary64 (if (<= y -110.0) -1.0 (if (<= y 9.6e-12) 1.0 -1.0)))
double code(double x, double y) {
double tmp;
if (y <= -110.0) {
tmp = -1.0;
} else if (y <= 9.6e-12) {
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 <= (-110.0d0)) then
tmp = -1.0d0
else if (y <= 9.6d-12) 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 <= -110.0) {
tmp = -1.0;
} else if (y <= 9.6e-12) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -110.0: tmp = -1.0 elif y <= 9.6e-12: tmp = 1.0 else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -110.0) tmp = -1.0; elseif (y <= 9.6e-12) tmp = 1.0; else tmp = -1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -110.0) tmp = -1.0; elseif (y <= 9.6e-12) tmp = 1.0; else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -110.0], -1.0, If[LessEqual[y, 9.6e-12], 1.0, -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -110:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 9.6 \cdot 10^{-12}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < -110 or 9.59999999999999948e-12 < y Initial program 99.9%
Taylor expanded in x around 0 77.2%
if -110 < y < 9.59999999999999948e-12Initial program 99.9%
Taylor expanded in x around inf 82.3%
(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 47.3%
(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 2024172
(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)))