
(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 8 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 (/ 1.0 (/ (- x y) (+ x y))))
double code(double x, double y) {
return 1.0 / ((x - y) / (x + y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0 / ((x - y) / (x + y))
end function
public static double code(double x, double y) {
return 1.0 / ((x - y) / (x + y));
}
def code(x, y): return 1.0 / ((x - y) / (x + y))
function code(x, y) return Float64(1.0 / Float64(Float64(x - y) / Float64(x + y))) end
function tmp = code(x, y) tmp = 1.0 / ((x - y) / (x + y)); end
code[x_, y_] := N[(1.0 / N[(N[(x - y), $MachinePrecision] / N[(x + y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{x - y}{x + y}}
\end{array}
Initial program 100.0%
clear-num100.0%
inv-pow100.0%
Applied egg-rr100.0%
unpow-1100.0%
clear-num100.0%
flip3--35.4%
associate-/r/35.4%
unpow235.4%
distribute-rgt-in35.4%
+-commutative35.4%
+-commutative35.4%
fma-udef35.4%
associate-/r/35.4%
clear-num35.3%
associate-/l/34.9%
fma-udef34.9%
+-commutative34.9%
+-commutative34.9%
distribute-rgt-in34.9%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(if (or (<= y -4.2e+103)
(and (not (<= y 1.3e-194))
(or (<= y 9.8e-156) (not (<= y 420000000.0)))))
(- -1.0 (/ x y))
1.0))
double code(double x, double y) {
double tmp;
if ((y <= -4.2e+103) || (!(y <= 1.3e-194) && ((y <= 9.8e-156) || !(y <= 420000000.0)))) {
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 ((y <= (-4.2d+103)) .or. (.not. (y <= 1.3d-194)) .and. (y <= 9.8d-156) .or. (.not. (y <= 420000000.0d0))) 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 ((y <= -4.2e+103) || (!(y <= 1.3e-194) && ((y <= 9.8e-156) || !(y <= 420000000.0)))) {
tmp = -1.0 - (x / y);
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -4.2e+103) or (not (y <= 1.3e-194) and ((y <= 9.8e-156) or not (y <= 420000000.0))): tmp = -1.0 - (x / y) else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if ((y <= -4.2e+103) || (!(y <= 1.3e-194) && ((y <= 9.8e-156) || !(y <= 420000000.0)))) 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 ((y <= -4.2e+103) || (~((y <= 1.3e-194)) && ((y <= 9.8e-156) || ~((y <= 420000000.0))))) tmp = -1.0 - (x / y); else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -4.2e+103], And[N[Not[LessEqual[y, 1.3e-194]], $MachinePrecision], Or[LessEqual[y, 9.8e-156], N[Not[LessEqual[y, 420000000.0]], $MachinePrecision]]]], N[(-1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -4.2 \cdot 10^{+103} \lor \neg \left(y \leq 1.3 \cdot 10^{-194}\right) \land \left(y \leq 9.8 \cdot 10^{-156} \lor \neg \left(y \leq 420000000\right)\right):\\
\;\;\;\;-1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if y < -4.2000000000000003e103 or 1.30000000000000001e-194 < y < 9.79999999999999902e-156 or 4.2e8 < y Initial program 100.0%
flip3--11.0%
associate-/r/11.0%
+-commutative11.0%
distribute-rgt-out11.0%
+-commutative11.0%
fma-def11.0%
pow211.0%
Applied egg-rr11.0%
associate-*l/10.3%
associate-/l*11.0%
Simplified11.0%
Taylor expanded in x around 0 78.9%
neg-mul-178.9%
Simplified78.9%
Taylor expanded in x around 0 78.9%
fma-neg78.9%
metadata-eval78.9%
*-lft-identity78.9%
metadata-eval78.9%
rem-square-sqrt0.0%
unpow20.0%
rem-square-sqrt0.0%
unpow20.0%
pow-sqr0.0%
metadata-eval0.0%
*-commutative0.0%
metadata-eval0.0%
rem-square-sqrt0.0%
unpow20.0%
fma-def0.0%
+-commutative0.0%
mul-1-neg0.0%
sub-neg0.0%
unpow20.0%
rem-square-sqrt0.0%
metadata-eval0.0%
*-commutative0.0%
Simplified78.9%
if -4.2000000000000003e103 < y < 1.30000000000000001e-194 or 9.79999999999999902e-156 < y < 4.2e8Initial program 100.0%
Taylor expanded in x around inf 77.1%
Final simplification77.9%
(FPCore (x y)
:precision binary64
(if (<= y -4.2e+103)
-1.0
(if (<= y 1.15e-179)
1.0
(if (<= y 8.1e-156) -1.0 (if (<= y 2000000000.0) 1.0 -1.0)))))
double code(double x, double y) {
double tmp;
if (y <= -4.2e+103) {
tmp = -1.0;
} else if (y <= 1.15e-179) {
tmp = 1.0;
} else if (y <= 8.1e-156) {
tmp = -1.0;
} else if (y <= 2000000000.0) {
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 <= (-4.2d+103)) then
tmp = -1.0d0
else if (y <= 1.15d-179) then
tmp = 1.0d0
else if (y <= 8.1d-156) then
tmp = -1.0d0
else if (y <= 2000000000.0d0) 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 <= -4.2e+103) {
tmp = -1.0;
} else if (y <= 1.15e-179) {
tmp = 1.0;
} else if (y <= 8.1e-156) {
tmp = -1.0;
} else if (y <= 2000000000.0) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -4.2e+103: tmp = -1.0 elif y <= 1.15e-179: tmp = 1.0 elif y <= 8.1e-156: tmp = -1.0 elif y <= 2000000000.0: tmp = 1.0 else: tmp = -1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -4.2e+103) tmp = -1.0; elseif (y <= 1.15e-179) tmp = 1.0; elseif (y <= 8.1e-156) tmp = -1.0; elseif (y <= 2000000000.0) tmp = 1.0; else tmp = -1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -4.2e+103) tmp = -1.0; elseif (y <= 1.15e-179) tmp = 1.0; elseif (y <= 8.1e-156) tmp = -1.0; elseif (y <= 2000000000.0) tmp = 1.0; else tmp = -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -4.2e+103], -1.0, If[LessEqual[y, 1.15e-179], 1.0, If[LessEqual[y, 8.1e-156], -1.0, If[LessEqual[y, 2000000000.0], 1.0, -1.0]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -4.2 \cdot 10^{+103}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 1.15 \cdot 10^{-179}:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq 8.1 \cdot 10^{-156}:\\
\;\;\;\;-1\\
\mathbf{elif}\;y \leq 2000000000:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if y < -4.2000000000000003e103 or 1.14999999999999994e-179 < y < 8.0999999999999998e-156 or 2e9 < y Initial program 100.0%
Taylor expanded in x around 0 78.8%
if -4.2000000000000003e103 < y < 1.14999999999999994e-179 or 8.0999999999999998e-156 < y < 2e9Initial program 100.0%
Taylor expanded in x around inf 76.8%
Final simplification77.6%
(FPCore (x y) :precision binary64 (if (or (<= y -8e+103) (not (<= y 2000000000000.0))) (- -1.0 (/ x y)) (+ 1.0 (* 2.0 (/ y x)))))
double code(double x, double y) {
double tmp;
if ((y <= -8e+103) || !(y <= 2000000000000.0)) {
tmp = -1.0 - (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 <= (-8d+103)) .or. (.not. (y <= 2000000000000.0d0))) then
tmp = (-1.0d0) - (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 <= -8e+103) || !(y <= 2000000000000.0)) {
tmp = -1.0 - (x / y);
} else {
tmp = 1.0 + (2.0 * (y / x));
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -8e+103) or not (y <= 2000000000000.0): tmp = -1.0 - (x / y) else: tmp = 1.0 + (2.0 * (y / x)) return tmp
function code(x, y) tmp = 0.0 if ((y <= -8e+103) || !(y <= 2000000000000.0)) tmp = Float64(-1.0 - 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 <= -8e+103) || ~((y <= 2000000000000.0))) tmp = -1.0 - (x / y); else tmp = 1.0 + (2.0 * (y / x)); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -8e+103], N[Not[LessEqual[y, 2000000000000.0]], $MachinePrecision]], N[(-1.0 - 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 -8 \cdot 10^{+103} \lor \neg \left(y \leq 2000000000000\right):\\
\;\;\;\;-1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;1 + 2 \cdot \frac{y}{x}\\
\end{array}
\end{array}
if y < -8e103 or 2e12 < y Initial program 100.0%
flip3--10.9%
associate-/r/10.9%
+-commutative10.9%
distribute-rgt-out10.9%
+-commutative10.9%
fma-def10.9%
pow210.9%
Applied egg-rr10.9%
associate-*l/10.3%
associate-/l*10.9%
Simplified10.9%
Taylor expanded in x around 0 79.7%
neg-mul-179.7%
Simplified79.7%
Taylor expanded in x around 0 79.7%
fma-neg79.7%
metadata-eval79.7%
*-lft-identity79.7%
metadata-eval79.7%
rem-square-sqrt0.0%
unpow20.0%
rem-square-sqrt0.0%
unpow20.0%
pow-sqr0.0%
metadata-eval0.0%
*-commutative0.0%
metadata-eval0.0%
rem-square-sqrt0.0%
unpow20.0%
fma-def0.0%
+-commutative0.0%
mul-1-neg0.0%
sub-neg0.0%
unpow20.0%
rem-square-sqrt0.0%
metadata-eval0.0%
*-commutative0.0%
Simplified79.7%
if -8e103 < y < 2e12Initial program 100.0%
Taylor expanded in y around 0 74.8%
Final simplification76.7%
(FPCore (x y) :precision binary64 (if (or (<= y -4.2e+103) (not (<= y 12500000.0))) (+ (* -2.0 (/ x y)) -1.0) (+ 1.0 (* 2.0 (/ y x)))))
double code(double x, double y) {
double tmp;
if ((y <= -4.2e+103) || !(y <= 12500000.0)) {
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 ((y <= (-4.2d+103)) .or. (.not. (y <= 12500000.0d0))) 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 ((y <= -4.2e+103) || !(y <= 12500000.0)) {
tmp = (-2.0 * (x / y)) + -1.0;
} else {
tmp = 1.0 + (2.0 * (y / x));
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -4.2e+103) or not (y <= 12500000.0): 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 ((y <= -4.2e+103) || !(y <= 12500000.0)) 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 ((y <= -4.2e+103) || ~((y <= 12500000.0))) tmp = (-2.0 * (x / y)) + -1.0; else tmp = 1.0 + (2.0 * (y / x)); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -4.2e+103], N[Not[LessEqual[y, 12500000.0]], $MachinePrecision]], 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}\;y \leq -4.2 \cdot 10^{+103} \lor \neg \left(y \leq 12500000\right):\\
\;\;\;\;-2 \cdot \frac{x}{y} + -1\\
\mathbf{else}:\\
\;\;\;\;1 + 2 \cdot \frac{y}{x}\\
\end{array}
\end{array}
if y < -4.2000000000000003e103 or 1.25e7 < y Initial program 100.0%
Taylor expanded in x around 0 81.1%
if -4.2000000000000003e103 < y < 1.25e7Initial program 100.0%
Taylor expanded in y around 0 74.8%
Final simplification77.2%
(FPCore (x y) :precision binary64 (if (or (<= y -7.5e+103) (not (<= y 1450000000.0))) (- -1.0 (/ x y)) (/ (+ x y) x)))
double code(double x, double y) {
double tmp;
if ((y <= -7.5e+103) || !(y <= 1450000000.0)) {
tmp = -1.0 - (x / y);
} else {
tmp = (x + 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 <= (-7.5d+103)) .or. (.not. (y <= 1450000000.0d0))) then
tmp = (-1.0d0) - (x / y)
else
tmp = (x + y) / x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -7.5e+103) || !(y <= 1450000000.0)) {
tmp = -1.0 - (x / y);
} else {
tmp = (x + y) / x;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -7.5e+103) or not (y <= 1450000000.0): tmp = -1.0 - (x / y) else: tmp = (x + y) / x return tmp
function code(x, y) tmp = 0.0 if ((y <= -7.5e+103) || !(y <= 1450000000.0)) tmp = Float64(-1.0 - Float64(x / y)); else tmp = Float64(Float64(x + y) / x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -7.5e+103) || ~((y <= 1450000000.0))) tmp = -1.0 - (x / y); else tmp = (x + y) / x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -7.5e+103], N[Not[LessEqual[y, 1450000000.0]], $MachinePrecision]], N[(-1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(N[(x + y), $MachinePrecision] / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -7.5 \cdot 10^{+103} \lor \neg \left(y \leq 1450000000\right):\\
\;\;\;\;-1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x + y}{x}\\
\end{array}
\end{array}
if y < -7.49999999999999922e103 or 1.45e9 < y Initial program 100.0%
flip3--10.9%
associate-/r/10.9%
+-commutative10.9%
distribute-rgt-out10.9%
+-commutative10.9%
fma-def10.9%
pow210.9%
Applied egg-rr10.9%
associate-*l/10.3%
associate-/l*10.9%
Simplified10.9%
Taylor expanded in x around 0 79.7%
neg-mul-179.7%
Simplified79.7%
Taylor expanded in x around 0 79.7%
fma-neg79.7%
metadata-eval79.7%
*-lft-identity79.7%
metadata-eval79.7%
rem-square-sqrt0.0%
unpow20.0%
rem-square-sqrt0.0%
unpow20.0%
pow-sqr0.0%
metadata-eval0.0%
*-commutative0.0%
metadata-eval0.0%
rem-square-sqrt0.0%
unpow20.0%
fma-def0.0%
+-commutative0.0%
mul-1-neg0.0%
sub-neg0.0%
unpow20.0%
rem-square-sqrt0.0%
metadata-eval0.0%
*-commutative0.0%
Simplified79.7%
if -7.49999999999999922e103 < y < 1.45e9Initial program 100.0%
flip3--50.3%
associate-/r/50.3%
+-commutative50.3%
distribute-rgt-out50.3%
+-commutative50.3%
fma-def50.3%
pow250.3%
Applied egg-rr50.3%
associate-*l/50.0%
associate-/l*50.3%
Simplified50.3%
Taylor expanded in x around inf 74.6%
Final simplification76.5%
(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%
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 46.0%
Final simplification46.0%
(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 2024019
(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)))