
(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 -1.65e-35) (not (<= f 3.8e-73))) (+ (* -2.0 (/ n f)) -1.0) (+ 1.0 (* 2.0 (/ f n)))))
double code(double f, double n) {
double tmp;
if ((f <= -1.65e-35) || !(f <= 3.8e-73)) {
tmp = (-2.0 * (n / f)) + -1.0;
} else {
tmp = 1.0 + (2.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 <= (-1.65d-35)) .or. (.not. (f <= 3.8d-73))) then
tmp = ((-2.0d0) * (n / f)) + (-1.0d0)
else
tmp = 1.0d0 + (2.0d0 * (f / n))
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((f <= -1.65e-35) || !(f <= 3.8e-73)) {
tmp = (-2.0 * (n / f)) + -1.0;
} else {
tmp = 1.0 + (2.0 * (f / n));
}
return tmp;
}
def code(f, n): tmp = 0 if (f <= -1.65e-35) or not (f <= 3.8e-73): tmp = (-2.0 * (n / f)) + -1.0 else: tmp = 1.0 + (2.0 * (f / n)) return tmp
function code(f, n) tmp = 0.0 if ((f <= -1.65e-35) || !(f <= 3.8e-73)) tmp = Float64(Float64(-2.0 * Float64(n / f)) + -1.0); else tmp = Float64(1.0 + Float64(2.0 * Float64(f / n))); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((f <= -1.65e-35) || ~((f <= 3.8e-73))) tmp = (-2.0 * (n / f)) + -1.0; else tmp = 1.0 + (2.0 * (f / n)); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[f, -1.65e-35], N[Not[LessEqual[f, 3.8e-73]], $MachinePrecision]], N[(N[(-2.0 * N[(n / f), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], N[(1.0 + N[(2.0 * N[(f / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -1.65 \cdot 10^{-35} \lor \neg \left(f \leq 3.8 \cdot 10^{-73}\right):\\
\;\;\;\;-2 \cdot \frac{n}{f} + -1\\
\mathbf{else}:\\
\;\;\;\;1 + 2 \cdot \frac{f}{n}\\
\end{array}
\end{array}
if f < -1.65e-35 or 3.8000000000000003e-73 < 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 n around 0 77.9%
if -1.65e-35 < f < 3.8000000000000003e-73Initial 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.1%
Final simplification80.5%
(FPCore (f n) :precision binary64 (if (or (<= f -3.5e-37) (not (<= f 1.5e-69))) (/ f (- n f)) (+ 1.0 (* 2.0 (/ f n)))))
double code(double f, double n) {
double tmp;
if ((f <= -3.5e-37) || !(f <= 1.5e-69)) {
tmp = f / (n - f);
} else {
tmp = 1.0 + (2.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 <= (-3.5d-37)) .or. (.not. (f <= 1.5d-69))) then
tmp = f / (n - f)
else
tmp = 1.0d0 + (2.0d0 * (f / n))
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((f <= -3.5e-37) || !(f <= 1.5e-69)) {
tmp = f / (n - f);
} else {
tmp = 1.0 + (2.0 * (f / n));
}
return tmp;
}
def code(f, n): tmp = 0 if (f <= -3.5e-37) or not (f <= 1.5e-69): tmp = f / (n - f) else: tmp = 1.0 + (2.0 * (f / n)) return tmp
function code(f, n) tmp = 0.0 if ((f <= -3.5e-37) || !(f <= 1.5e-69)) tmp = Float64(f / Float64(n - f)); else tmp = Float64(1.0 + Float64(2.0 * Float64(f / n))); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((f <= -3.5e-37) || ~((f <= 1.5e-69))) tmp = f / (n - f); else tmp = 1.0 + (2.0 * (f / n)); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[f, -3.5e-37], N[Not[LessEqual[f, 1.5e-69]], $MachinePrecision]], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(2.0 * N[(f / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -3.5 \cdot 10^{-37} \lor \neg \left(f \leq 1.5 \cdot 10^{-69}\right):\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;1 + 2 \cdot \frac{f}{n}\\
\end{array}
\end{array}
if f < -3.5000000000000001e-37 or 1.49999999999999995e-69 < 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 77.3%
if -3.5000000000000001e-37 < f < 1.49999999999999995e-69Initial 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.1%
Final simplification80.1%
(FPCore (f n) :precision binary64 (if (or (<= f -4.5e-36) (not (<= f 5.1e-70))) (/ f (- n f)) (/ n (- n f))))
double code(double f, double n) {
double tmp;
if ((f <= -4.5e-36) || !(f <= 5.1e-70)) {
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 <= (-4.5d-36)) .or. (.not. (f <= 5.1d-70))) 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 <= -4.5e-36) || !(f <= 5.1e-70)) {
tmp = f / (n - f);
} else {
tmp = n / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if (f <= -4.5e-36) or not (f <= 5.1e-70): tmp = f / (n - f) else: tmp = n / (n - f) return tmp
function code(f, n) tmp = 0.0 if ((f <= -4.5e-36) || !(f <= 5.1e-70)) 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 <= -4.5e-36) || ~((f <= 5.1e-70))) tmp = f / (n - f); else tmp = n / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[f, -4.5e-36], N[Not[LessEqual[f, 5.1e-70]], $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 -4.5 \cdot 10^{-36} \lor \neg \left(f \leq 5.1 \cdot 10^{-70}\right):\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;\frac{n}{n - f}\\
\end{array}
\end{array}
if f < -4.50000000000000024e-36 or 5.10000000000000025e-70 < 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 77.3%
if -4.50000000000000024e-36 < f < 5.10000000000000025e-70Initial 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 simplification79.7%
(FPCore (f n) :precision binary64 (if (or (<= f -9.5e-37) (not (<= f 7.4e-75))) (/ f (- n f)) (+ 1.0 (/ f n))))
double code(double f, double n) {
double tmp;
if ((f <= -9.5e-37) || !(f <= 7.4e-75)) {
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 <= (-9.5d-37)) .or. (.not. (f <= 7.4d-75))) 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 <= -9.5e-37) || !(f <= 7.4e-75)) {
tmp = f / (n - f);
} else {
tmp = 1.0 + (f / n);
}
return tmp;
}
def code(f, n): tmp = 0 if (f <= -9.5e-37) or not (f <= 7.4e-75): tmp = f / (n - f) else: tmp = 1.0 + (f / n) return tmp
function code(f, n) tmp = 0.0 if ((f <= -9.5e-37) || !(f <= 7.4e-75)) 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 <= -9.5e-37) || ~((f <= 7.4e-75))) tmp = f / (n - f); else tmp = 1.0 + (f / n); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[f, -9.5e-37], N[Not[LessEqual[f, 7.4e-75]], $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 -9.5 \cdot 10^{-37} \lor \neg \left(f \leq 7.4 \cdot 10^{-75}\right):\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{f}{n}\\
\end{array}
\end{array}
if f < -9.49999999999999927e-37 or 7.40000000000000047e-75 < 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 77.3%
if -9.49999999999999927e-37 < f < 7.40000000000000047e-75Initial 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 83.2%
Final simplification79.7%
(FPCore (f n) :precision binary64 (if (<= f -1.06e-35) -1.0 (if (<= f 2.1e-71) (+ 1.0 (/ f n)) -1.0)))
double code(double f, double n) {
double tmp;
if (f <= -1.06e-35) {
tmp = -1.0;
} else if (f <= 2.1e-71) {
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 <= (-1.06d-35)) then
tmp = -1.0d0
else if (f <= 2.1d-71) 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 <= -1.06e-35) {
tmp = -1.0;
} else if (f <= 2.1e-71) {
tmp = 1.0 + (f / n);
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if f <= -1.06e-35: tmp = -1.0 elif f <= 2.1e-71: tmp = 1.0 + (f / n) else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if (f <= -1.06e-35) tmp = -1.0; elseif (f <= 2.1e-71) 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 <= -1.06e-35) tmp = -1.0; elseif (f <= 2.1e-71) tmp = 1.0 + (f / n); else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[f, -1.06e-35], -1.0, If[LessEqual[f, 2.1e-71], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision], -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -1.06 \cdot 10^{-35}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq 2.1 \cdot 10^{-71}:\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if f < -1.06e-35 or 2.1000000000000001e-71 < 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 76.7%
if -1.06e-35 < f < 2.1000000000000001e-71Initial 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 83.2%
(FPCore (f n) :precision binary64 (if (<= f -6e-36) -1.0 (if (<= f 1.45e-69) 1.0 -1.0)))
double code(double f, double n) {
double tmp;
if (f <= -6e-36) {
tmp = -1.0;
} else if (f <= 1.45e-69) {
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 <= (-6d-36)) then
tmp = -1.0d0
else if (f <= 1.45d-69) 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 <= -6e-36) {
tmp = -1.0;
} else if (f <= 1.45e-69) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if f <= -6e-36: tmp = -1.0 elif f <= 1.45e-69: tmp = 1.0 else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if (f <= -6e-36) tmp = -1.0; elseif (f <= 1.45e-69) tmp = 1.0; else tmp = -1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (f <= -6e-36) tmp = -1.0; elseif (f <= 1.45e-69) tmp = 1.0; else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[f, -6e-36], -1.0, If[LessEqual[f, 1.45e-69], 1.0, -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -6 \cdot 10^{-36}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq 1.45 \cdot 10^{-69}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if f < -6.0000000000000003e-36 or 1.4499999999999999e-69 < 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 76.7%
if -6.0000000000000003e-36 < f < 1.4499999999999999e-69Initial 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.8%
(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 52.0%
herbie shell --seed 2024154
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))