
(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 8 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 (/ (+ f n) (- n f)))
double code(double f, double n) {
return (f + n) / (n - f);
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
code = (f + n) / (n - f)
end function
public static double code(double f, double n) {
return (f + n) / (n - f);
}
def code(f, n): return (f + n) / (n - f)
function code(f, n) return Float64(Float64(f + n) / Float64(n - f)) end
function tmp = code(f, n) tmp = (f + n) / (n - f); end
code[f_, n_] := N[(N[(f + n), $MachinePrecision] / N[(n - f), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{f + n}{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%
(FPCore (f n) :precision binary64 (if (or (<= f -3.5e-63) (not (<= f 3.5e-5))) (/ f (- n f)) (+ 1.0 (/ (* f 2.0) n))))
double code(double f, double n) {
double tmp;
if ((f <= -3.5e-63) || !(f <= 3.5e-5)) {
tmp = f / (n - f);
} else {
tmp = 1.0 + ((f * 2.0) / n);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((f <= (-3.5d-63)) .or. (.not. (f <= 3.5d-5))) then
tmp = f / (n - f)
else
tmp = 1.0d0 + ((f * 2.0d0) / n)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((f <= -3.5e-63) || !(f <= 3.5e-5)) {
tmp = f / (n - f);
} else {
tmp = 1.0 + ((f * 2.0) / n);
}
return tmp;
}
def code(f, n): tmp = 0 if (f <= -3.5e-63) or not (f <= 3.5e-5): tmp = f / (n - f) else: tmp = 1.0 + ((f * 2.0) / n) return tmp
function code(f, n) tmp = 0.0 if ((f <= -3.5e-63) || !(f <= 3.5e-5)) tmp = Float64(f / Float64(n - f)); else tmp = Float64(1.0 + Float64(Float64(f * 2.0) / n)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((f <= -3.5e-63) || ~((f <= 3.5e-5))) tmp = f / (n - f); else tmp = 1.0 + ((f * 2.0) / n); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[f, -3.5e-63], N[Not[LessEqual[f, 3.5e-5]], $MachinePrecision]], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(N[(f * 2.0), $MachinePrecision] / n), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -3.5 \cdot 10^{-63} \lor \neg \left(f \leq 3.5 \cdot 10^{-5}\right):\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{f \cdot 2}{n}\\
\end{array}
\end{array}
if f < -3.50000000000000003e-63 or 3.4999999999999997e-5 < f 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 80.4%
if -3.50000000000000003e-63 < f < 3.4999999999999997e-5Initial 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 0 84.0%
associate-*r/84.0%
Simplified84.0%
Final simplification82.0%
(FPCore (f n) :precision binary64 (if (or (<= f -3.1e-65) (not (<= f 4.3e-5))) (/ f (- n f)) (/ n (- n f))))
double code(double f, double n) {
double tmp;
if ((f <= -3.1e-65) || !(f <= 4.3e-5)) {
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 <= (-3.1d-65)) .or. (.not. (f <= 4.3d-5))) 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 <= -3.1e-65) || !(f <= 4.3e-5)) {
tmp = f / (n - f);
} else {
tmp = n / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if (f <= -3.1e-65) or not (f <= 4.3e-5): tmp = f / (n - f) else: tmp = n / (n - f) return tmp
function code(f, n) tmp = 0.0 if ((f <= -3.1e-65) || !(f <= 4.3e-5)) 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 <= -3.1e-65) || ~((f <= 4.3e-5))) tmp = f / (n - f); else tmp = n / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[f, -3.1e-65], N[Not[LessEqual[f, 4.3e-5]], $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 -3.1 \cdot 10^{-65} \lor \neg \left(f \leq 4.3 \cdot 10^{-5}\right):\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;\frac{n}{n - f}\\
\end{array}
\end{array}
if f < -3.10000000000000016e-65 or 4.3000000000000002e-5 < f 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 80.4%
if -3.10000000000000016e-65 < f < 4.3000000000000002e-5Initial 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 0 83.3%
Final simplification81.7%
(FPCore (f n) :precision binary64 (if (or (<= f -2.5e-63) (not (<= f 1.76e-6))) (/ f (- n f)) (+ 1.0 (/ f n))))
double code(double f, double n) {
double tmp;
if ((f <= -2.5e-63) || !(f <= 1.76e-6)) {
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 ((f <= (-2.5d-63)) .or. (.not. (f <= 1.76d-6))) 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 ((f <= -2.5e-63) || !(f <= 1.76e-6)) {
tmp = f / (n - f);
} else {
tmp = 1.0 + (f / n);
}
return tmp;
}
def code(f, n): tmp = 0 if (f <= -2.5e-63) or not (f <= 1.76e-6): tmp = f / (n - f) else: tmp = 1.0 + (f / n) return tmp
function code(f, n) tmp = 0.0 if ((f <= -2.5e-63) || !(f <= 1.76e-6)) 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 ((f <= -2.5e-63) || ~((f <= 1.76e-6))) tmp = f / (n - f); else tmp = 1.0 + (f / n); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[f, -2.5e-63], N[Not[LessEqual[f, 1.76e-6]], $MachinePrecision]], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -2.5 \cdot 10^{-63} \lor \neg \left(f \leq 1.76 \cdot 10^{-6}\right):\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{f}{n}\\
\end{array}
\end{array}
if f < -2.5000000000000001e-63 or 1.7600000000000001e-6 < f 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 80.4%
if -2.5000000000000001e-63 < f < 1.7600000000000001e-6Initial 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 0 83.3%
Taylor expanded in n around inf 82.9%
+-commutative82.9%
Simplified82.9%
Final simplification81.5%
(FPCore (f n) :precision binary64 (if (or (<= f -3e-67) (not (<= f 3.2e-5))) (- -1.0 (/ n f)) (+ 1.0 (/ f n))))
double code(double f, double n) {
double tmp;
if ((f <= -3e-67) || !(f <= 3.2e-5)) {
tmp = -1.0 - (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 ((f <= (-3d-67)) .or. (.not. (f <= 3.2d-5))) then
tmp = (-1.0d0) - (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 ((f <= -3e-67) || !(f <= 3.2e-5)) {
tmp = -1.0 - (n / f);
} else {
tmp = 1.0 + (f / n);
}
return tmp;
}
def code(f, n): tmp = 0 if (f <= -3e-67) or not (f <= 3.2e-5): tmp = -1.0 - (n / f) else: tmp = 1.0 + (f / n) return tmp
function code(f, n) tmp = 0.0 if ((f <= -3e-67) || !(f <= 3.2e-5)) tmp = Float64(-1.0 - 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 ((f <= -3e-67) || ~((f <= 3.2e-5))) tmp = -1.0 - (n / f); else tmp = 1.0 + (f / n); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[f, -3e-67], N[Not[LessEqual[f, 3.2e-5]], $MachinePrecision]], N[(-1.0 - N[(n / f), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -3 \cdot 10^{-67} \lor \neg \left(f \leq 3.2 \cdot 10^{-5}\right):\\
\;\;\;\;-1 - \frac{n}{f}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{f}{n}\\
\end{array}
\end{array}
if f < -3.00000000000000032e-67 or 3.19999999999999986e-5 < f 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 80.4%
Taylor expanded in f around inf 80.2%
sub-neg80.2%
metadata-eval80.2%
+-commutative80.2%
mul-1-neg80.2%
unsub-neg80.2%
Simplified80.2%
if -3.00000000000000032e-67 < f < 3.19999999999999986e-5Initial 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 0 83.3%
Taylor expanded in n around inf 82.9%
+-commutative82.9%
Simplified82.9%
Final simplification81.4%
(FPCore (f n) :precision binary64 (if (<= f -2.8e-62) -1.0 (if (<= f 6e-5) (+ 1.0 (/ f n)) -1.0)))
double code(double f, double n) {
double tmp;
if (f <= -2.8e-62) {
tmp = -1.0;
} else if (f <= 6e-5) {
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 (f <= (-2.8d-62)) then
tmp = -1.0d0
else if (f <= 6d-5) 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 (f <= -2.8e-62) {
tmp = -1.0;
} else if (f <= 6e-5) {
tmp = 1.0 + (f / n);
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if f <= -2.8e-62: tmp = -1.0 elif f <= 6e-5: tmp = 1.0 + (f / n) else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if (f <= -2.8e-62) tmp = -1.0; elseif (f <= 6e-5) 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 (f <= -2.8e-62) tmp = -1.0; elseif (f <= 6e-5) tmp = 1.0 + (f / n); else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[f, -2.8e-62], -1.0, If[LessEqual[f, 6e-5], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision], -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -2.8 \cdot 10^{-62}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq 6 \cdot 10^{-5}:\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if f < -2.80000000000000002e-62 or 6.00000000000000015e-5 < f 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 79.9%
if -2.80000000000000002e-62 < f < 6.00000000000000015e-5Initial 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 0 83.3%
Taylor expanded in n around inf 82.9%
+-commutative82.9%
Simplified82.9%
Final simplification81.3%
(FPCore (f n) :precision binary64 (if (<= f -3e-63) -1.0 (if (<= f 0.0002) 1.0 -1.0)))
double code(double f, double n) {
double tmp;
if (f <= -3e-63) {
tmp = -1.0;
} else if (f <= 0.0002) {
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 <= (-3d-63)) then
tmp = -1.0d0
else if (f <= 0.0002d0) 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 <= -3e-63) {
tmp = -1.0;
} else if (f <= 0.0002) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if f <= -3e-63: tmp = -1.0 elif f <= 0.0002: tmp = 1.0 else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if (f <= -3e-63) tmp = -1.0; elseif (f <= 0.0002) tmp = 1.0; else tmp = -1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (f <= -3e-63) tmp = -1.0; elseif (f <= 0.0002) tmp = 1.0; else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[f, -3e-63], -1.0, If[LessEqual[f, 0.0002], 1.0, -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -3 \cdot 10^{-63}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq 0.0002:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if f < -2.99999999999999979e-63 or 2.0000000000000001e-4 < f 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 79.9%
if -2.99999999999999979e-63 < f < 2.0000000000000001e-4Initial 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 0 82.6%
(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 51.5%
herbie shell --seed 2024133
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))