
(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 (+ (+ (/ (+ f n) (- n f)) 1.0) -1.0))
double code(double f, double n) {
return (((f + n) / (n - f)) + 1.0) + -1.0;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
code = (((f + n) / (n - f)) + 1.0d0) + (-1.0d0)
end function
public static double code(double f, double n) {
return (((f + n) / (n - f)) + 1.0) + -1.0;
}
def code(f, n): return (((f + n) / (n - f)) + 1.0) + -1.0
function code(f, n) return Float64(Float64(Float64(Float64(f + n) / Float64(n - f)) + 1.0) + -1.0) end
function tmp = code(f, n) tmp = (((f + n) / (n - f)) + 1.0) + -1.0; end
code[f_, n_] := N[(N[(N[(N[(f + n), $MachinePrecision] / N[(n - f), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision] + -1.0), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{f + n}{n - f} + 1\right) + -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%
expm1-log1p-u96.8%
expm1-undefine96.9%
Applied egg-rr96.9%
log1p-undefine96.9%
rem-exp-log100.0%
+-commutative100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (f n)
:precision binary64
(if (<= f -1.65e+39)
-1.0
(if (or (<= f -4.5e-7) (and (not (<= f -9e-80)) (<= f 2.3e+27)))
(+ 1.0 (* 2.0 (/ f n)))
-1.0)))
double code(double f, double n) {
double tmp;
if (f <= -1.65e+39) {
tmp = -1.0;
} else if ((f <= -4.5e-7) || (!(f <= -9e-80) && (f <= 2.3e+27))) {
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 <= (-1.65d+39)) then
tmp = -1.0d0
else if ((f <= (-4.5d-7)) .or. (.not. (f <= (-9d-80))) .and. (f <= 2.3d+27)) 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 <= -1.65e+39) {
tmp = -1.0;
} else if ((f <= -4.5e-7) || (!(f <= -9e-80) && (f <= 2.3e+27))) {
tmp = 1.0 + (2.0 * (f / n));
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if f <= -1.65e+39: tmp = -1.0 elif (f <= -4.5e-7) or (not (f <= -9e-80) and (f <= 2.3e+27)): tmp = 1.0 + (2.0 * (f / n)) else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if (f <= -1.65e+39) tmp = -1.0; elseif ((f <= -4.5e-7) || (!(f <= -9e-80) && (f <= 2.3e+27))) 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 <= -1.65e+39) tmp = -1.0; elseif ((f <= -4.5e-7) || (~((f <= -9e-80)) && (f <= 2.3e+27))) tmp = 1.0 + (2.0 * (f / n)); else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[f, -1.65e+39], -1.0, If[Or[LessEqual[f, -4.5e-7], And[N[Not[LessEqual[f, -9e-80]], $MachinePrecision], LessEqual[f, 2.3e+27]]], N[(1.0 + N[(2.0 * N[(f / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -1.65 \cdot 10^{+39}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq -4.5 \cdot 10^{-7} \lor \neg \left(f \leq -9 \cdot 10^{-80}\right) \land f \leq 2.3 \cdot 10^{+27}:\\
\;\;\;\;1 + 2 \cdot \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if f < -1.6500000000000001e39 or -4.4999999999999998e-7 < f < -9.0000000000000006e-80 or 2.3000000000000001e27 < 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 84.2%
if -1.6500000000000001e39 < f < -4.4999999999999998e-7 or -9.0000000000000006e-80 < f < 2.3000000000000001e27Initial 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 73.8%
Final simplification79.3%
(FPCore (f n)
:precision binary64
(let* ((t_0 (+ (* -2.0 (/ n f)) -1.0)) (t_1 (+ 1.0 (* 2.0 (/ f n)))))
(if (<= f -4.2e+33)
t_0
(if (<= f -1.18e-6)
t_1
(if (<= f -1.3e-79) -1.0 (if (<= f 6.5e+25) t_1 t_0))))))
double code(double f, double n) {
double t_0 = (-2.0 * (n / f)) + -1.0;
double t_1 = 1.0 + (2.0 * (f / n));
double tmp;
if (f <= -4.2e+33) {
tmp = t_0;
} else if (f <= -1.18e-6) {
tmp = t_1;
} else if (f <= -1.3e-79) {
tmp = -1.0;
} else if (f <= 6.5e+25) {
tmp = t_1;
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = ((-2.0d0) * (n / f)) + (-1.0d0)
t_1 = 1.0d0 + (2.0d0 * (f / n))
if (f <= (-4.2d+33)) then
tmp = t_0
else if (f <= (-1.18d-6)) then
tmp = t_1
else if (f <= (-1.3d-79)) then
tmp = -1.0d0
else if (f <= 6.5d+25) then
tmp = t_1
else
tmp = t_0
end if
code = tmp
end function
public static double code(double f, double n) {
double t_0 = (-2.0 * (n / f)) + -1.0;
double t_1 = 1.0 + (2.0 * (f / n));
double tmp;
if (f <= -4.2e+33) {
tmp = t_0;
} else if (f <= -1.18e-6) {
tmp = t_1;
} else if (f <= -1.3e-79) {
tmp = -1.0;
} else if (f <= 6.5e+25) {
tmp = t_1;
} else {
tmp = t_0;
}
return tmp;
}
def code(f, n): t_0 = (-2.0 * (n / f)) + -1.0 t_1 = 1.0 + (2.0 * (f / n)) tmp = 0 if f <= -4.2e+33: tmp = t_0 elif f <= -1.18e-6: tmp = t_1 elif f <= -1.3e-79: tmp = -1.0 elif f <= 6.5e+25: tmp = t_1 else: tmp = t_0 return tmp
function code(f, n) t_0 = Float64(Float64(-2.0 * Float64(n / f)) + -1.0) t_1 = Float64(1.0 + Float64(2.0 * Float64(f / n))) tmp = 0.0 if (f <= -4.2e+33) tmp = t_0; elseif (f <= -1.18e-6) tmp = t_1; elseif (f <= -1.3e-79) tmp = -1.0; elseif (f <= 6.5e+25) tmp = t_1; else tmp = t_0; end return tmp end
function tmp_2 = code(f, n) t_0 = (-2.0 * (n / f)) + -1.0; t_1 = 1.0 + (2.0 * (f / n)); tmp = 0.0; if (f <= -4.2e+33) tmp = t_0; elseif (f <= -1.18e-6) tmp = t_1; elseif (f <= -1.3e-79) tmp = -1.0; elseif (f <= 6.5e+25) tmp = t_1; else tmp = t_0; end tmp_2 = tmp; end
code[f_, n_] := Block[{t$95$0 = N[(N[(-2.0 * N[(n / f), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 + N[(2.0 * N[(f / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[f, -4.2e+33], t$95$0, If[LessEqual[f, -1.18e-6], t$95$1, If[LessEqual[f, -1.3e-79], -1.0, If[LessEqual[f, 6.5e+25], t$95$1, t$95$0]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := -2 \cdot \frac{n}{f} + -1\\
t_1 := 1 + 2 \cdot \frac{f}{n}\\
\mathbf{if}\;f \leq -4.2 \cdot 10^{+33}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;f \leq -1.18 \cdot 10^{-6}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;f \leq -1.3 \cdot 10^{-79}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq 6.5 \cdot 10^{+25}:\\
\;\;\;\;t\_1\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if f < -4.2000000000000001e33 or 6.50000000000000005e25 < 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 84.1%
if -4.2000000000000001e33 < f < -1.17999999999999993e-6 or -1.29999999999999997e-79 < f < 6.50000000000000005e25Initial 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 73.8%
if -1.17999999999999993e-6 < f < -1.29999999999999997e-79Initial 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 100.0%
Final simplification79.7%
(FPCore (f n)
:precision binary64
(if (<= f -1e+38)
-1.0
(if (<= f -100000000000.0)
1.0
(if (<= f -1.85e-115) -1.0 (if (<= f 6.2e+25) 1.0 -1.0)))))
double code(double f, double n) {
double tmp;
if (f <= -1e+38) {
tmp = -1.0;
} else if (f <= -100000000000.0) {
tmp = 1.0;
} else if (f <= -1.85e-115) {
tmp = -1.0;
} else if (f <= 6.2e+25) {
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 <= (-1d+38)) then
tmp = -1.0d0
else if (f <= (-100000000000.0d0)) then
tmp = 1.0d0
else if (f <= (-1.85d-115)) then
tmp = -1.0d0
else if (f <= 6.2d+25) 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 <= -1e+38) {
tmp = -1.0;
} else if (f <= -100000000000.0) {
tmp = 1.0;
} else if (f <= -1.85e-115) {
tmp = -1.0;
} else if (f <= 6.2e+25) {
tmp = 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if f <= -1e+38: tmp = -1.0 elif f <= -100000000000.0: tmp = 1.0 elif f <= -1.85e-115: tmp = -1.0 elif f <= 6.2e+25: tmp = 1.0 else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if (f <= -1e+38) tmp = -1.0; elseif (f <= -100000000000.0) tmp = 1.0; elseif (f <= -1.85e-115) tmp = -1.0; elseif (f <= 6.2e+25) tmp = 1.0; else tmp = -1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (f <= -1e+38) tmp = -1.0; elseif (f <= -100000000000.0) tmp = 1.0; elseif (f <= -1.85e-115) tmp = -1.0; elseif (f <= 6.2e+25) tmp = 1.0; else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[f, -1e+38], -1.0, If[LessEqual[f, -100000000000.0], 1.0, If[LessEqual[f, -1.85e-115], -1.0, If[LessEqual[f, 6.2e+25], 1.0, -1.0]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;f \leq -1 \cdot 10^{+38}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq -100000000000:\\
\;\;\;\;1\\
\mathbf{elif}\;f \leq -1.85 \cdot 10^{-115}:\\
\;\;\;\;-1\\
\mathbf{elif}\;f \leq 6.2 \cdot 10^{+25}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if f < -9.99999999999999977e37 or -1e11 < f < -1.85e-115 or 6.1999999999999996e25 < 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.0%
if -9.99999999999999977e37 < f < -1e11 or -1.85e-115 < f < 6.1999999999999996e25Initial 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 76.0%
Final simplification78.9%
(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 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 56.3%
Final simplification56.3%
herbie shell --seed 2024055
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))