
(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 6 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 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%
clear-num100.0%
inv-pow100.0%
Applied egg-rr100.0%
unpow-1100.0%
+-commutative100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (f n)
:precision binary64
(let* ((t_0 (+ (* -2.0 (/ n f)) -1.0)))
(if (<= f -2.7e+158)
t_0
(if (<= f -4.3e+102)
1.0
(if (or (<= f -9.5e-5)
(and (not (<= f 1.02e-16))
(or (<= f 1.1e+37) (not (<= f 1.35e+131)))))
t_0
(+ 1.0 (* 2.0 (/ f n))))))))
double code(double f, double n) {
double t_0 = (-2.0 * (n / f)) + -1.0;
double tmp;
if (f <= -2.7e+158) {
tmp = t_0;
} else if (f <= -4.3e+102) {
tmp = 1.0;
} else if ((f <= -9.5e-5) || (!(f <= 1.02e-16) && ((f <= 1.1e+37) || !(f <= 1.35e+131)))) {
tmp = t_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) :: t_0
real(8) :: tmp
t_0 = ((-2.0d0) * (n / f)) + (-1.0d0)
if (f <= (-2.7d+158)) then
tmp = t_0
else if (f <= (-4.3d+102)) then
tmp = 1.0d0
else if ((f <= (-9.5d-5)) .or. (.not. (f <= 1.02d-16)) .and. (f <= 1.1d+37) .or. (.not. (f <= 1.35d+131))) then
tmp = t_0
else
tmp = 1.0d0 + (2.0d0 * (f / n))
end if
code = tmp
end function
public static double code(double f, double n) {
double t_0 = (-2.0 * (n / f)) + -1.0;
double tmp;
if (f <= -2.7e+158) {
tmp = t_0;
} else if (f <= -4.3e+102) {
tmp = 1.0;
} else if ((f <= -9.5e-5) || (!(f <= 1.02e-16) && ((f <= 1.1e+37) || !(f <= 1.35e+131)))) {
tmp = t_0;
} else {
tmp = 1.0 + (2.0 * (f / n));
}
return tmp;
}
def code(f, n): t_0 = (-2.0 * (n / f)) + -1.0 tmp = 0 if f <= -2.7e+158: tmp = t_0 elif f <= -4.3e+102: tmp = 1.0 elif (f <= -9.5e-5) or (not (f <= 1.02e-16) and ((f <= 1.1e+37) or not (f <= 1.35e+131))): tmp = t_0 else: tmp = 1.0 + (2.0 * (f / n)) return tmp
function code(f, n) t_0 = Float64(Float64(-2.0 * Float64(n / f)) + -1.0) tmp = 0.0 if (f <= -2.7e+158) tmp = t_0; elseif (f <= -4.3e+102) tmp = 1.0; elseif ((f <= -9.5e-5) || (!(f <= 1.02e-16) && ((f <= 1.1e+37) || !(f <= 1.35e+131)))) tmp = t_0; else tmp = Float64(1.0 + Float64(2.0 * Float64(f / n))); end return tmp end
function tmp_2 = code(f, n) t_0 = (-2.0 * (n / f)) + -1.0; tmp = 0.0; if (f <= -2.7e+158) tmp = t_0; elseif (f <= -4.3e+102) tmp = 1.0; elseif ((f <= -9.5e-5) || (~((f <= 1.02e-16)) && ((f <= 1.1e+37) || ~((f <= 1.35e+131))))) tmp = t_0; else tmp = 1.0 + (2.0 * (f / n)); end tmp_2 = tmp; end
code[f_, n_] := Block[{t$95$0 = N[(N[(-2.0 * N[(n / f), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]}, If[LessEqual[f, -2.7e+158], t$95$0, If[LessEqual[f, -4.3e+102], 1.0, If[Or[LessEqual[f, -9.5e-5], And[N[Not[LessEqual[f, 1.02e-16]], $MachinePrecision], Or[LessEqual[f, 1.1e+37], N[Not[LessEqual[f, 1.35e+131]], $MachinePrecision]]]], t$95$0, N[(1.0 + N[(2.0 * N[(f / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := -2 \cdot \frac{n}{f} + -1\\
\mathbf{if}\;f \leq -2.7 \cdot 10^{+158}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;f \leq -4.3 \cdot 10^{+102}:\\
\;\;\;\;1\\
\mathbf{elif}\;f \leq -9.5 \cdot 10^{-5} \lor \neg \left(f \leq 1.02 \cdot 10^{-16}\right) \land \left(f \leq 1.1 \cdot 10^{+37} \lor \neg \left(f \leq 1.35 \cdot 10^{+131}\right)\right):\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;1 + 2 \cdot \frac{f}{n}\\
\end{array}
\end{array}
if f < -2.69999999999999979e158 or -4.3000000000000001e102 < f < -9.5000000000000005e-5 or 1.0200000000000001e-16 < f < 1.1e37 or 1.35000000000000002e131 < 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 83.2%
if -2.69999999999999979e158 < f < -4.3000000000000001e102Initial 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 100.0%
if -9.5000000000000005e-5 < f < 1.0200000000000001e-16 or 1.1e37 < f < 1.35000000000000002e131Initial 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.6%
Final simplification82.1%
(FPCore (f n)
:precision binary64
(if (<= f -2.7e+158)
-1.0
(if (<= f -4.3e+102)
1.0
(if (<= f -7.5e-10)
-1.0
(if (or (<= f 1.75e-16) (and (not (<= f 1.95e+36)) (<= f 1.35e+131)))
(+ 1.0 (* 2.0 (/ f n)))
-1.0)))))
double code(double f, double n) {
double tmp;
if (f <= -2.7e+158) {
tmp = -1.0;
} else if (f <= -4.3e+102) {
tmp = 1.0;
} else if (f <= -7.5e-10) {
tmp = -1.0;
} else if ((f <= 1.75e-16) || (!(f <= 1.95e+36) && (f <= 1.35e+131))) {
tmp = 1.0 + (2.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.7d+158)) then
tmp = -1.0d0
else if (f <= (-4.3d+102)) then
tmp = 1.0d0
else if (f <= (-7.5d-10)) then
tmp = -1.0d0
else if ((f <= 1.75d-16) .or. (.not. (f <= 1.95d+36)) .and. (f <= 1.35d+131)) then
tmp = 1.0d0 + (2.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.7e+158) {
tmp = -1.0;
} else if (f <= -4.3e+102) {
tmp = 1.0;
} else if (f <= -7.5e-10) {
tmp = -1.0;
} else if ((f <= 1.75e-16) || (!(f <= 1.95e+36) && (f <= 1.35e+131))) {
tmp = 1.0 + (2.0 * (f / n));
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if f <= -2.7e+158: tmp = -1.0 elif f <= -4.3e+102: tmp = 1.0 elif f <= -7.5e-10: tmp = -1.0 elif (f <= 1.75e-16) or (not (f <= 1.95e+36) and (f <= 1.35e+131)): tmp = 1.0 + (2.0 * (f / n)) else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if (f <= -2.7e+158) tmp = -1.0; elseif (f <= -4.3e+102) tmp = 1.0; elseif (f <= -7.5e-10) tmp = -1.0; elseif ((f <= 1.75e-16) || (!(f <= 1.95e+36) && (f <= 1.35e+131))) tmp = Float64(1.0 + Float64(2.0 * Float64(f / n))); else tmp = -1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (f <= -2.7e+158) tmp = -1.0; elseif (f <= -4.3e+102) tmp = 1.0; elseif (f <= -7.5e-10) tmp = -1.0; elseif ((f <= 1.75e-16) || (~((f <= 1.95e+36)) && (f <= 1.35e+131))) tmp = 1.0 + (2.0 * (f / n)); else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[f, -2.7e+158], -1.0, If[LessEqual[f, -4.3e+102], 1.0, If[LessEqual[f, -7.5e-10], -1.0, If[Or[LessEqual[f, 1.75e-16], And[N[Not[LessEqual[f, 1.95e+36]], $MachinePrecision], LessEqual[f, 1.35e+131]]], N[(1.0 + N[(2.0 * N[(f / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -1.0]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -2.7 \cdot 10^{+158}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq -4.3 \cdot 10^{+102}:\\
\;\;\;\;1\\
\mathbf{elif}\;f \leq -7.5 \cdot 10^{-10}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq 1.75 \cdot 10^{-16} \lor \neg \left(f \leq 1.95 \cdot 10^{+36}\right) \land f \leq 1.35 \cdot 10^{+131}:\\
\;\;\;\;1 + 2 \cdot \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if f < -2.69999999999999979e158 or -4.3000000000000001e102 < f < -7.49999999999999995e-10 or 1.75000000000000009e-16 < f < 1.9500000000000001e36 or 1.35000000000000002e131 < 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 81.6%
if -2.69999999999999979e158 < f < -4.3000000000000001e102Initial 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 100.0%
if -7.49999999999999995e-10 < f < 1.75000000000000009e-16 or 1.9500000000000001e36 < f < 1.35000000000000002e131Initial 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.0%
Final simplification81.8%
(FPCore (f n)
:precision binary64
(if (<= f -2.7e+158)
-1.0
(if (<= f -7.6e+101)
1.0
(if (<= f -4.1e-5)
-1.0
(if (<= f 4e-16)
1.0
(if (<= f 2.2e+75) -1.0 (if (<= f 2.55e+122) 1.0 -1.0)))))))
double code(double f, double n) {
double tmp;
if (f <= -2.7e+158) {
tmp = -1.0;
} else if (f <= -7.6e+101) {
tmp = 1.0;
} else if (f <= -4.1e-5) {
tmp = -1.0;
} else if (f <= 4e-16) {
tmp = 1.0;
} else if (f <= 2.2e+75) {
tmp = -1.0;
} else if (f <= 2.55e+122) {
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 <= (-2.7d+158)) then
tmp = -1.0d0
else if (f <= (-7.6d+101)) then
tmp = 1.0d0
else if (f <= (-4.1d-5)) then
tmp = -1.0d0
else if (f <= 4d-16) then
tmp = 1.0d0
else if (f <= 2.2d+75) then
tmp = -1.0d0
else if (f <= 2.55d+122) 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 <= -2.7e+158) {
tmp = -1.0;
} else if (f <= -7.6e+101) {
tmp = 1.0;
} else if (f <= -4.1e-5) {
tmp = -1.0;
} else if (f <= 4e-16) {
tmp = 1.0;
} else if (f <= 2.2e+75) {
tmp = -1.0;
} else if (f <= 2.55e+122) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if f <= -2.7e+158: tmp = -1.0 elif f <= -7.6e+101: tmp = 1.0 elif f <= -4.1e-5: tmp = -1.0 elif f <= 4e-16: tmp = 1.0 elif f <= 2.2e+75: tmp = -1.0 elif f <= 2.55e+122: tmp = 1.0 else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if (f <= -2.7e+158) tmp = -1.0; elseif (f <= -7.6e+101) tmp = 1.0; elseif (f <= -4.1e-5) tmp = -1.0; elseif (f <= 4e-16) tmp = 1.0; elseif (f <= 2.2e+75) tmp = -1.0; elseif (f <= 2.55e+122) tmp = 1.0; else tmp = -1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (f <= -2.7e+158) tmp = -1.0; elseif (f <= -7.6e+101) tmp = 1.0; elseif (f <= -4.1e-5) tmp = -1.0; elseif (f <= 4e-16) tmp = 1.0; elseif (f <= 2.2e+75) tmp = -1.0; elseif (f <= 2.55e+122) tmp = 1.0; else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[f, -2.7e+158], -1.0, If[LessEqual[f, -7.6e+101], 1.0, If[LessEqual[f, -4.1e-5], -1.0, If[LessEqual[f, 4e-16], 1.0, If[LessEqual[f, 2.2e+75], -1.0, If[LessEqual[f, 2.55e+122], 1.0, -1.0]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -2.7 \cdot 10^{+158}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq -7.6 \cdot 10^{+101}:\\
\;\;\;\;1\\
\mathbf{elif}\;f \leq -4.1 \cdot 10^{-5}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq 4 \cdot 10^{-16}:\\
\;\;\;\;1\\
\mathbf{elif}\;f \leq 2.2 \cdot 10^{+75}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq 2.55 \cdot 10^{+122}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if f < -2.69999999999999979e158 or -7.5999999999999996e101 < f < -4.10000000000000005e-5 or 3.9999999999999999e-16 < f < 2.20000000000000012e75 or 2.55e122 < 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.3%
if -2.69999999999999979e158 < f < -7.5999999999999996e101 or -4.10000000000000005e-5 < f < 3.9999999999999999e-16 or 2.20000000000000012e75 < f < 2.55e122Initial 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.9%
Final simplification80.8%
(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 44.0%
Final simplification44.0%
herbie shell --seed 2024045
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))