
(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 (/ 1.0 (- (/ n (+ n f)) (/ f (+ n f)))))
double code(double f, double n) {
return 1.0 / ((n / (n + f)) - (f / (n + f)));
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
code = 1.0d0 / ((n / (n + f)) - (f / (n + f)))
end function
public static double code(double f, double n) {
return 1.0 / ((n / (n + f)) - (f / (n + f)));
}
def code(f, n): return 1.0 / ((n / (n + f)) - (f / (n + f)))
function code(f, n) return Float64(1.0 / Float64(Float64(n / Float64(n + f)) - Float64(f / Float64(n + f)))) end
function tmp = code(f, n) tmp = 1.0 / ((n / (n + f)) - (f / (n + f))); end
code[f_, n_] := N[(1.0 / N[(N[(n / N[(n + f), $MachinePrecision]), $MachinePrecision] - N[(f / N[(n + f), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{n}{n + f} - \frac{f}{n + f}}
\end{array}
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 n around inf 86.3%
clear-num86.3%
inv-pow86.3%
Applied egg-rr86.3%
unpow-186.3%
distribute-lft-in86.2%
*-rgt-identity86.2%
associate-*r/83.9%
associate-*l/99.9%
*-inverses99.9%
*-lft-identity99.9%
Simplified99.9%
div-sub100.0%
Applied egg-rr100.0%
(FPCore (f n) :precision binary64 (if (or (<= f -1.35e-47) (not (<= f 5e+32))) (+ (* -2.0 (/ n f)) -1.0) (/ n (- n f))))
double code(double f, double n) {
double tmp;
if ((f <= -1.35e-47) || !(f <= 5e+32)) {
tmp = (-2.0 * (n / f)) + -1.0;
} else {
tmp = n / (n - f);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((f <= (-1.35d-47)) .or. (.not. (f <= 5d+32))) then
tmp = ((-2.0d0) * (n / f)) + (-1.0d0)
else
tmp = n / (n - f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((f <= -1.35e-47) || !(f <= 5e+32)) {
tmp = (-2.0 * (n / f)) + -1.0;
} else {
tmp = n / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if (f <= -1.35e-47) or not (f <= 5e+32): tmp = (-2.0 * (n / f)) + -1.0 else: tmp = n / (n - f) return tmp
function code(f, n) tmp = 0.0 if ((f <= -1.35e-47) || !(f <= 5e+32)) tmp = Float64(Float64(-2.0 * Float64(n / f)) + -1.0); else tmp = Float64(n / Float64(n - f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((f <= -1.35e-47) || ~((f <= 5e+32))) tmp = (-2.0 * (n / f)) + -1.0; else tmp = n / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[f, -1.35e-47], N[Not[LessEqual[f, 5e+32]], $MachinePrecision]], N[(N[(-2.0 * N[(n / f), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -1.35 \cdot 10^{-47} \lor \neg \left(f \leq 5 \cdot 10^{+32}\right):\\
\;\;\;\;-2 \cdot \frac{n}{f} + -1\\
\mathbf{else}:\\
\;\;\;\;\frac{n}{n - f}\\
\end{array}
\end{array}
if f < -1.3499999999999999e-47 or 4.9999999999999997e32 < f 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 n around 0 81.1%
if -1.3499999999999999e-47 < f < 4.9999999999999997e32Initial 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.5%
Final simplification80.0%
(FPCore (f n) :precision binary64 (if (or (<= f -2.45e-48) (not (<= f 5e+32))) (/ (+ n f) (- f)) (/ n (- n f))))
double code(double f, double n) {
double tmp;
if ((f <= -2.45e-48) || !(f <= 5e+32)) {
tmp = (n + f) / -f;
} else {
tmp = n / (n - f);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((f <= (-2.45d-48)) .or. (.not. (f <= 5d+32))) then
tmp = (n + f) / -f
else
tmp = n / (n - f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((f <= -2.45e-48) || !(f <= 5e+32)) {
tmp = (n + f) / -f;
} else {
tmp = n / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if (f <= -2.45e-48) or not (f <= 5e+32): tmp = (n + f) / -f else: tmp = n / (n - f) return tmp
function code(f, n) tmp = 0.0 if ((f <= -2.45e-48) || !(f <= 5e+32)) tmp = Float64(Float64(n + f) / Float64(-f)); else tmp = Float64(n / Float64(n - f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((f <= -2.45e-48) || ~((f <= 5e+32))) tmp = (n + f) / -f; else tmp = n / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[f, -2.45e-48], N[Not[LessEqual[f, 5e+32]], $MachinePrecision]], N[(N[(n + f), $MachinePrecision] / (-f)), $MachinePrecision], N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -2.45 \cdot 10^{-48} \lor \neg \left(f \leq 5 \cdot 10^{+32}\right):\\
\;\;\;\;\frac{n + f}{-f}\\
\mathbf{else}:\\
\;\;\;\;\frac{n}{n - f}\\
\end{array}
\end{array}
if f < -2.4500000000000001e-48 or 4.9999999999999997e32 < f 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 n around 0 80.3%
neg-mul-180.3%
Simplified80.3%
if -2.4500000000000001e-48 < f < 4.9999999999999997e32Initial 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.5%
Final simplification79.6%
(FPCore (f n) :precision binary64 (if (or (<= f -9e-49) (not (<= f 5e+32))) (/ f (- n f)) (/ n (- n f))))
double code(double f, double n) {
double tmp;
if ((f <= -9e-49) || !(f <= 5e+32)) {
tmp = f / (n - f);
} else {
tmp = n / (n - f);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((f <= (-9d-49)) .or. (.not. (f <= 5d+32))) then
tmp = f / (n - f)
else
tmp = n / (n - f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((f <= -9e-49) || !(f <= 5e+32)) {
tmp = f / (n - f);
} else {
tmp = n / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if (f <= -9e-49) or not (f <= 5e+32): tmp = f / (n - f) else: tmp = n / (n - f) return tmp
function code(f, n) tmp = 0.0 if ((f <= -9e-49) || !(f <= 5e+32)) tmp = Float64(f / Float64(n - f)); else tmp = Float64(n / Float64(n - f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((f <= -9e-49) || ~((f <= 5e+32))) tmp = f / (n - f); else tmp = n / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[f, -9e-49], N[Not[LessEqual[f, 5e+32]], $MachinePrecision]], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision], N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -9 \cdot 10^{-49} \lor \neg \left(f \leq 5 \cdot 10^{+32}\right):\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;\frac{n}{n - f}\\
\end{array}
\end{array}
if f < -9.0000000000000004e-49 or 4.9999999999999997e32 < f 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 inf 80.2%
if -9.0000000000000004e-49 < f < 4.9999999999999997e32Initial 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.5%
Final simplification79.5%
(FPCore (f n) :precision binary64 (if (or (<= f -2.3e-55) (not (<= f 5.1e+32))) (/ f (- n f)) 1.0))
double code(double f, double n) {
double tmp;
if ((f <= -2.3e-55) || !(f <= 5.1e+32)) {
tmp = f / (n - f);
} 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 ((f <= (-2.3d-55)) .or. (.not. (f <= 5.1d+32))) then
tmp = f / (n - f)
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((f <= -2.3e-55) || !(f <= 5.1e+32)) {
tmp = f / (n - f);
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (f <= -2.3e-55) or not (f <= 5.1e+32): tmp = f / (n - f) else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if ((f <= -2.3e-55) || !(f <= 5.1e+32)) tmp = Float64(f / Float64(n - f)); else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((f <= -2.3e-55) || ~((f <= 5.1e+32))) tmp = f / (n - f); else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[f, -2.3e-55], N[Not[LessEqual[f, 5.1e+32]], $MachinePrecision]], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -2.3 \cdot 10^{-55} \lor \neg \left(f \leq 5.1 \cdot 10^{+32}\right):\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if f < -2.30000000000000011e-55 or 5.10000000000000004e32 < f 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 inf 80.2%
if -2.30000000000000011e-55 < f < 5.10000000000000004e32Initial 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.8%
Final simplification79.2%
(FPCore (f n) :precision binary64 (if (<= f -1.35e-47) -1.0 (if (<= f 5e+32) 1.0 -1.0)))
double code(double f, double n) {
double tmp;
if (f <= -1.35e-47) {
tmp = -1.0;
} else if (f <= 5e+32) {
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 (f <= (-1.35d-47)) then
tmp = -1.0d0
else if (f <= 5d+32) 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 (f <= -1.35e-47) {
tmp = -1.0;
} else if (f <= 5e+32) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if f <= -1.35e-47: tmp = -1.0 elif f <= 5e+32: tmp = 1.0 else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if (f <= -1.35e-47) tmp = -1.0; elseif (f <= 5e+32) tmp = 1.0; else tmp = -1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (f <= -1.35e-47) tmp = -1.0; elseif (f <= 5e+32) tmp = 1.0; else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[f, -1.35e-47], -1.0, If[LessEqual[f, 5e+32], 1.0, -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -1.35 \cdot 10^{-47}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq 5 \cdot 10^{+32}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if f < -1.3499999999999999e-47 or 4.9999999999999997e32 < f 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 inf 79.7%
if -1.3499999999999999e-47 < f < 4.9999999999999997e32Initial 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.8%
(FPCore (f n) :precision binary64 (/ 1.0 (/ (- n f) (+ n f))))
double code(double f, double n) {
return 1.0 / ((n - f) / (n + f));
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
code = 1.0d0 / ((n - f) / (n + f))
end function
public static double code(double f, double n) {
return 1.0 / ((n - f) / (n + f));
}
def code(f, n): return 1.0 / ((n - f) / (n + f))
function code(f, n) return Float64(1.0 / Float64(Float64(n - f) / Float64(n + f))) end
function tmp = code(f, n) tmp = 1.0 / ((n - f) / (n + f)); end
code[f_, n_] := N[(1.0 / N[(N[(n - f), $MachinePrecision] / N[(n + f), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{n - f}{n + f}}
\end{array}
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 n around inf 86.3%
clear-num86.3%
inv-pow86.3%
Applied egg-rr86.3%
unpow-186.3%
distribute-lft-in86.2%
*-rgt-identity86.2%
associate-*r/83.9%
associate-*l/99.9%
*-inverses99.9%
*-lft-identity99.9%
Simplified99.9%
(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 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%
Final simplification99.9%
(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 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 inf 55.6%
herbie shell --seed 2024146
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))