
(FPCore (f n) :precision binary64 (/ (- (+ f n)) (- f n)))
double code(double f, double n) {
return -(f + n) / (f - n);
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
code = -(f + n) / (f - n)
end function
public static double code(double f, double n) {
return -(f + n) / (f - n);
}
def code(f, n): return -(f + n) / (f - n)
function code(f, n) return Float64(Float64(-Float64(f + n)) / Float64(f - n)) end
function tmp = code(f, n) tmp = -(f + n) / (f - n); end
code[f_, n_] := N[((-N[(f + n), $MachinePrecision]) / N[(f - n), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-\left(f + n\right)}{f - n}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (f n) :precision binary64 (/ (- (+ f n)) (- f n)))
double code(double f, double n) {
return -(f + n) / (f - n);
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
code = -(f + n) / (f - n)
end function
public static double code(double f, double n) {
return -(f + n) / (f - n);
}
def code(f, n): return -(f + n) / (f - n)
function code(f, n) return Float64(Float64(-Float64(f + n)) / Float64(f - n)) end
function tmp = code(f, n) tmp = -(f + n) / (f - n); end
code[f_, n_] := N[((-N[(f + n), $MachinePrecision]) / N[(f - n), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-\left(f + n\right)}{f - n}
\end{array}
(FPCore (f n) :precision binary64 (pow (/ (- n f) (+ n f)) -1.0))
double code(double f, double n) {
return pow(((n - f) / (n + f)), -1.0);
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
code = ((n - f) / (n + f)) ** (-1.0d0)
end function
public static double code(double f, double n) {
return Math.pow(((n - f) / (n + f)), -1.0);
}
def code(f, n): return math.pow(((n - f) / (n + f)), -1.0)
function code(f, n) return Float64(Float64(n - f) / Float64(n + f)) ^ -1.0 end
function tmp = code(f, n) tmp = ((n - f) / (n + f)) ^ -1.0; end
code[f_, n_] := N[Power[N[(N[(n - f), $MachinePrecision] / N[(n + f), $MachinePrecision]), $MachinePrecision], -1.0], $MachinePrecision]
\begin{array}{l}
\\
{\left(\frac{n - f}{n + f}\right)}^{-1}
\end{array}
Initial program 100.0%
distribute-frac-neg100.0%
distribute-neg-frac2100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
sub-neg100.0%
Simplified100.0%
clear-num100.0%
inv-pow100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (f n) :precision binary64 (if (or (<= n -2.25e-7) (not (<= n 2.7e-10))) (+ 1.0 (/ (* f 2.0) n)) (/ f (- n f))))
double code(double f, double n) {
double tmp;
if ((n <= -2.25e-7) || !(n <= 2.7e-10)) {
tmp = 1.0 + ((f * 2.0) / n);
} else {
tmp = f / (n - f);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((n <= (-2.25d-7)) .or. (.not. (n <= 2.7d-10))) then
tmp = 1.0d0 + ((f * 2.0d0) / n)
else
tmp = f / (n - f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((n <= -2.25e-7) || !(n <= 2.7e-10)) {
tmp = 1.0 + ((f * 2.0) / n);
} else {
tmp = f / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -2.25e-7) or not (n <= 2.7e-10): tmp = 1.0 + ((f * 2.0) / n) else: tmp = f / (n - f) return tmp
function code(f, n) tmp = 0.0 if ((n <= -2.25e-7) || !(n <= 2.7e-10)) tmp = Float64(1.0 + Float64(Float64(f * 2.0) / n)); else tmp = Float64(f / Float64(n - f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((n <= -2.25e-7) || ~((n <= 2.7e-10))) tmp = 1.0 + ((f * 2.0) / n); else tmp = f / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -2.25e-7], N[Not[LessEqual[n, 2.7e-10]], $MachinePrecision]], N[(1.0 + N[(N[(f * 2.0), $MachinePrecision] / n), $MachinePrecision]), $MachinePrecision], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -2.25 \cdot 10^{-7} \lor \neg \left(n \leq 2.7 \cdot 10^{-10}\right):\\
\;\;\;\;1 + \frac{f \cdot 2}{n}\\
\mathbf{else}:\\
\;\;\;\;\frac{f}{n - f}\\
\end{array}
\end{array}
if n < -2.2499999999999999e-7 or 2.7e-10 < n Initial program 99.9%
distribute-frac-neg99.9%
distribute-neg-frac299.9%
sub-neg99.9%
+-commutative99.9%
distribute-neg-in99.9%
remove-double-neg99.9%
sub-neg99.9%
Simplified99.9%
Taylor expanded in f around 0 80.0%
associate-*r/80.0%
Simplified80.0%
if -2.2499999999999999e-7 < n < 2.7e-10Initial program 100.0%
distribute-frac-neg100.0%
distribute-neg-frac2100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in f around inf 81.5%
Final simplification80.8%
(FPCore (f n) :precision binary64 (if (or (<= n -1.3e-8) (not (<= n 6e-10))) (+ 1.0 (/ f n)) (/ f (- n f))))
double code(double f, double n) {
double tmp;
if ((n <= -1.3e-8) || !(n <= 6e-10)) {
tmp = 1.0 + (f / n);
} else {
tmp = f / (n - f);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((n <= (-1.3d-8)) .or. (.not. (n <= 6d-10))) then
tmp = 1.0d0 + (f / n)
else
tmp = f / (n - f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((n <= -1.3e-8) || !(n <= 6e-10)) {
tmp = 1.0 + (f / n);
} else {
tmp = f / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -1.3e-8) or not (n <= 6e-10): tmp = 1.0 + (f / n) else: tmp = f / (n - f) return tmp
function code(f, n) tmp = 0.0 if ((n <= -1.3e-8) || !(n <= 6e-10)) tmp = Float64(1.0 + Float64(f / n)); else tmp = Float64(f / Float64(n - f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((n <= -1.3e-8) || ~((n <= 6e-10))) tmp = 1.0 + (f / n); else tmp = f / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -1.3e-8], N[Not[LessEqual[n, 6e-10]], $MachinePrecision]], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -1.3 \cdot 10^{-8} \lor \neg \left(n \leq 6 \cdot 10^{-10}\right):\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;\frac{f}{n - f}\\
\end{array}
\end{array}
if n < -1.3000000000000001e-8 or 6e-10 < n Initial program 99.9%
distribute-frac-neg99.9%
distribute-neg-frac299.9%
sub-neg99.9%
+-commutative99.9%
distribute-neg-in99.9%
remove-double-neg99.9%
sub-neg99.9%
Simplified99.9%
Taylor expanded in f around 0 79.6%
Taylor expanded in n around inf 79.4%
if -1.3000000000000001e-8 < n < 6e-10Initial program 100.0%
distribute-frac-neg100.0%
distribute-neg-frac2100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in f around inf 81.5%
Final simplification80.4%
(FPCore (f n) :precision binary64 (if (or (<= n -1.95e-7) (not (<= n 7.5e-10))) (+ 1.0 (/ f n)) (+ -1.0 (/ n f))))
double code(double f, double n) {
double tmp;
if ((n <= -1.95e-7) || !(n <= 7.5e-10)) {
tmp = 1.0 + (f / n);
} else {
tmp = -1.0 + (n / f);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((n <= (-1.95d-7)) .or. (.not. (n <= 7.5d-10))) then
tmp = 1.0d0 + (f / n)
else
tmp = (-1.0d0) + (n / f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((n <= -1.95e-7) || !(n <= 7.5e-10)) {
tmp = 1.0 + (f / n);
} else {
tmp = -1.0 + (n / f);
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -1.95e-7) or not (n <= 7.5e-10): tmp = 1.0 + (f / n) else: tmp = -1.0 + (n / f) return tmp
function code(f, n) tmp = 0.0 if ((n <= -1.95e-7) || !(n <= 7.5e-10)) tmp = Float64(1.0 + Float64(f / n)); else tmp = Float64(-1.0 + Float64(n / f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((n <= -1.95e-7) || ~((n <= 7.5e-10))) tmp = 1.0 + (f / n); else tmp = -1.0 + (n / f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -1.95e-7], N[Not[LessEqual[n, 7.5e-10]], $MachinePrecision]], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision], N[(-1.0 + N[(n / f), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -1.95 \cdot 10^{-7} \lor \neg \left(n \leq 7.5 \cdot 10^{-10}\right):\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-1 + \frac{n}{f}\\
\end{array}
\end{array}
if n < -1.95000000000000012e-7 or 7.49999999999999995e-10 < n Initial program 99.9%
distribute-frac-neg99.9%
distribute-neg-frac299.9%
sub-neg99.9%
+-commutative99.9%
distribute-neg-in99.9%
remove-double-neg99.9%
sub-neg99.9%
Simplified99.9%
Taylor expanded in f around 0 79.6%
Taylor expanded in n around inf 79.4%
if -1.95000000000000012e-7 < n < 7.49999999999999995e-10Initial program 100.0%
distribute-frac-neg100.0%
distribute-neg-frac2100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in n around 0 80.9%
neg-mul-180.9%
Simplified80.9%
add-sqr-sqrt37.3%
add-sqr-sqrt0.0%
sqrt-unprod0.9%
sqr-neg0.9%
sqrt-unprod0.9%
add-sqr-sqrt0.9%
+-commutative0.9%
sub-neg0.9%
sqrt-unprod25.5%
sqr-neg25.5%
sqrt-unprod43.1%
add-sqr-sqrt81.5%
div-sub81.5%
Applied egg-rr81.5%
sub-neg81.5%
*-inverses81.5%
metadata-eval81.5%
Simplified81.5%
Final simplification80.4%
(FPCore (f n) :precision binary64 (if (or (<= n -1.15e-43) (not (<= n 4.9e-11))) (+ 1.0 (/ f n)) -1.0))
double code(double f, double n) {
double tmp;
if ((n <= -1.15e-43) || !(n <= 4.9e-11)) {
tmp = 1.0 + (f / n);
} else {
tmp = -1.0;
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((n <= (-1.15d-43)) .or. (.not. (n <= 4.9d-11))) then
tmp = 1.0d0 + (f / n)
else
tmp = -1.0d0
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((n <= -1.15e-43) || !(n <= 4.9e-11)) {
tmp = 1.0 + (f / n);
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -1.15e-43) or not (n <= 4.9e-11): tmp = 1.0 + (f / n) else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if ((n <= -1.15e-43) || !(n <= 4.9e-11)) tmp = Float64(1.0 + Float64(f / n)); else tmp = -1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((n <= -1.15e-43) || ~((n <= 4.9e-11))) tmp = 1.0 + (f / n); else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -1.15e-43], N[Not[LessEqual[n, 4.9e-11]], $MachinePrecision]], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision], -1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -1.15 \cdot 10^{-43} \lor \neg \left(n \leq 4.9 \cdot 10^{-11}\right):\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if n < -1.1499999999999999e-43 or 4.8999999999999999e-11 < n Initial program 99.9%
distribute-frac-neg99.9%
distribute-neg-frac299.9%
sub-neg99.9%
+-commutative99.9%
distribute-neg-in99.9%
remove-double-neg99.9%
sub-neg99.9%
Simplified99.9%
Taylor expanded in f around 0 78.3%
Taylor expanded in n around inf 78.1%
if -1.1499999999999999e-43 < n < 4.8999999999999999e-11Initial program 100.0%
distribute-frac-neg100.0%
distribute-neg-frac2100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in f around inf 82.4%
Final simplification80.1%
(FPCore (f n) :precision binary64 (if (<= n -5e-7) (/ n (- n f)) (if (<= n 3.2e-11) (/ f (- n f)) (+ 1.0 (/ f n)))))
double code(double f, double n) {
double tmp;
if (n <= -5e-7) {
tmp = n / (n - f);
} else if (n <= 3.2e-11) {
tmp = f / (n - f);
} else {
tmp = 1.0 + (f / n);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if (n <= (-5d-7)) then
tmp = n / (n - f)
else if (n <= 3.2d-11) then
tmp = f / (n - f)
else
tmp = 1.0d0 + (f / n)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if (n <= -5e-7) {
tmp = n / (n - f);
} else if (n <= 3.2e-11) {
tmp = f / (n - f);
} else {
tmp = 1.0 + (f / n);
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -5e-7: tmp = n / (n - f) elif n <= 3.2e-11: tmp = f / (n - f) else: tmp = 1.0 + (f / n) return tmp
function code(f, n) tmp = 0.0 if (n <= -5e-7) tmp = Float64(n / Float64(n - f)); elseif (n <= 3.2e-11) tmp = Float64(f / Float64(n - f)); else tmp = Float64(1.0 + Float64(f / n)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -5e-7) tmp = n / (n - f); elseif (n <= 3.2e-11) tmp = f / (n - f); else tmp = 1.0 + (f / n); end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -5e-7], N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision], If[LessEqual[n, 3.2e-11], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -5 \cdot 10^{-7}:\\
\;\;\;\;\frac{n}{n - f}\\
\mathbf{elif}\;n \leq 3.2 \cdot 10^{-11}:\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{f}{n}\\
\end{array}
\end{array}
if n < -4.99999999999999977e-7Initial program 99.9%
distribute-frac-neg99.9%
distribute-neg-frac299.9%
sub-neg99.9%
+-commutative99.9%
distribute-neg-in99.9%
remove-double-neg99.9%
sub-neg99.9%
Simplified99.9%
Taylor expanded in f around 0 77.9%
if -4.99999999999999977e-7 < n < 3.19999999999999994e-11Initial program 100.0%
distribute-frac-neg100.0%
distribute-neg-frac2100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in f around inf 81.5%
if 3.19999999999999994e-11 < n Initial program 99.9%
distribute-frac-neg99.9%
distribute-neg-frac299.9%
sub-neg99.9%
+-commutative99.9%
distribute-neg-in99.9%
remove-double-neg99.9%
sub-neg99.9%
Simplified99.9%
Taylor expanded in f around 0 81.8%
Taylor expanded in n around inf 81.9%
(FPCore (f n) :precision binary64 (if (<= n -4e-41) 1.0 (if (<= n 1.65e-10) -1.0 1.0)))
double code(double f, double n) {
double tmp;
if (n <= -4e-41) {
tmp = 1.0;
} else if (n <= 1.65e-10) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if (n <= (-4d-41)) then
tmp = 1.0d0
else if (n <= 1.65d-10) then
tmp = -1.0d0
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if (n <= -4e-41) {
tmp = 1.0;
} else if (n <= 1.65e-10) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -4e-41: tmp = 1.0 elif n <= 1.65e-10: tmp = -1.0 else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (n <= -4e-41) tmp = 1.0; elseif (n <= 1.65e-10) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -4e-41) tmp = 1.0; elseif (n <= 1.65e-10) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -4e-41], 1.0, If[LessEqual[n, 1.65e-10], -1.0, 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -4 \cdot 10^{-41}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 1.65 \cdot 10^{-10}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if n < -4.00000000000000002e-41 or 1.65e-10 < n Initial program 99.9%
distribute-frac-neg99.9%
distribute-neg-frac299.9%
sub-neg99.9%
+-commutative99.9%
distribute-neg-in99.9%
remove-double-neg99.9%
sub-neg99.9%
Simplified99.9%
Taylor expanded in f around 0 77.6%
if -4.00000000000000002e-41 < n < 1.65e-10Initial program 100.0%
distribute-frac-neg100.0%
distribute-neg-frac2100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in f around inf 82.4%
(FPCore (f n) :precision binary64 (/ (+ n f) (- n f)))
double code(double f, double n) {
return (n + f) / (n - f);
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
code = (n + f) / (n - f)
end function
public static double code(double f, double n) {
return (n + f) / (n - f);
}
def code(f, n): return (n + f) / (n - f)
function code(f, n) return Float64(Float64(n + f) / Float64(n - f)) end
function tmp = code(f, n) tmp = (n + f) / (n - f); end
code[f_, n_] := N[(N[(n + f), $MachinePrecision] / N[(n - f), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{n + f}{n - f}
\end{array}
Initial program 100.0%
distribute-frac-neg100.0%
distribute-neg-frac2100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
sub-neg100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (f n) :precision binary64 -1.0)
double code(double f, double n) {
return -1.0;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
code = -1.0d0
end function
public static double code(double f, double n) {
return -1.0;
}
def code(f, n): return -1.0
function code(f, n) return -1.0 end
function tmp = code(f, n) tmp = -1.0; end
code[f_, n_] := -1.0
\begin{array}{l}
\\
-1
\end{array}
Initial program 100.0%
distribute-frac-neg100.0%
distribute-neg-frac2100.0%
sub-neg100.0%
+-commutative100.0%
distribute-neg-in100.0%
remove-double-neg100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in f around inf 48.6%
herbie shell --seed 2024118
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))