
(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 (<= n -2e+70) (not (<= n 4.8e-73))) (/ n (- n f)) (+ (* -2.0 (/ n f)) -1.0)))
double code(double f, double n) {
double tmp;
if ((n <= -2e+70) || !(n <= 4.8e-73)) {
tmp = n / (n - f);
} else {
tmp = (-2.0 * (n / f)) + -1.0;
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((n <= (-2d+70)) .or. (.not. (n <= 4.8d-73))) then
tmp = n / (n - f)
else
tmp = ((-2.0d0) * (n / f)) + (-1.0d0)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((n <= -2e+70) || !(n <= 4.8e-73)) {
tmp = n / (n - f);
} else {
tmp = (-2.0 * (n / f)) + -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -2e+70) or not (n <= 4.8e-73): tmp = n / (n - f) else: tmp = (-2.0 * (n / f)) + -1.0 return tmp
function code(f, n) tmp = 0.0 if ((n <= -2e+70) || !(n <= 4.8e-73)) tmp = Float64(n / Float64(n - f)); else tmp = Float64(Float64(-2.0 * Float64(n / f)) + -1.0); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((n <= -2e+70) || ~((n <= 4.8e-73))) tmp = n / (n - f); else tmp = (-2.0 * (n / f)) + -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -2e+70], N[Not[LessEqual[n, 4.8e-73]], $MachinePrecision]], N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision], N[(N[(-2.0 * N[(n / f), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -2 \cdot 10^{+70} \lor \neg \left(n \leq 4.8 \cdot 10^{-73}\right):\\
\;\;\;\;\frac{n}{n - f}\\
\mathbf{else}:\\
\;\;\;\;-2 \cdot \frac{n}{f} + -1\\
\end{array}
\end{array}
if n < -2.00000000000000015e70 or 4.80000000000000011e-73 < n 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 0 78.7%
if -2.00000000000000015e70 < n < 4.80000000000000011e-73Initial 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 83.2%
Final simplification81.0%
(FPCore (f n) :precision binary64 (if (or (<= n -1.75e+70) (not (<= n 5.4e-73))) (/ n (- n f)) (/ (+ f n) (- f))))
double code(double f, double n) {
double tmp;
if ((n <= -1.75e+70) || !(n <= 5.4e-73)) {
tmp = n / (n - f);
} else {
tmp = (f + n) / -f;
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((n <= (-1.75d+70)) .or. (.not. (n <= 5.4d-73))) then
tmp = n / (n - f)
else
tmp = (f + n) / -f
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((n <= -1.75e+70) || !(n <= 5.4e-73)) {
tmp = n / (n - f);
} else {
tmp = (f + n) / -f;
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -1.75e+70) or not (n <= 5.4e-73): tmp = n / (n - f) else: tmp = (f + n) / -f return tmp
function code(f, n) tmp = 0.0 if ((n <= -1.75e+70) || !(n <= 5.4e-73)) tmp = Float64(n / Float64(n - f)); else tmp = Float64(Float64(f + n) / Float64(-f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((n <= -1.75e+70) || ~((n <= 5.4e-73))) tmp = n / (n - f); else tmp = (f + n) / -f; end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -1.75e+70], N[Not[LessEqual[n, 5.4e-73]], $MachinePrecision]], N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision], N[(N[(f + n), $MachinePrecision] / (-f)), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -1.75 \cdot 10^{+70} \lor \neg \left(n \leq 5.4 \cdot 10^{-73}\right):\\
\;\;\;\;\frac{n}{n - f}\\
\mathbf{else}:\\
\;\;\;\;\frac{f + n}{-f}\\
\end{array}
\end{array}
if n < -1.75000000000000001e70 or 5.39999999999999989e-73 < n 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 0 78.7%
if -1.75000000000000001e70 < n < 5.39999999999999989e-73Initial 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 82.5%
neg-mul-182.5%
Simplified82.5%
Final simplification80.6%
(FPCore (f n) :precision binary64 (if (or (<= n -1.9e+70) (not (<= n 2e-72))) (/ n (- n f)) (- -1.0 (/ n f))))
double code(double f, double n) {
double tmp;
if ((n <= -1.9e+70) || !(n <= 2e-72)) {
tmp = n / (n - f);
} else {
tmp = -1.0 - (n / f);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((n <= (-1.9d+70)) .or. (.not. (n <= 2d-72))) then
tmp = n / (n - f)
else
tmp = (-1.0d0) - (n / f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((n <= -1.9e+70) || !(n <= 2e-72)) {
tmp = n / (n - f);
} else {
tmp = -1.0 - (n / f);
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -1.9e+70) or not (n <= 2e-72): tmp = n / (n - f) else: tmp = -1.0 - (n / f) return tmp
function code(f, n) tmp = 0.0 if ((n <= -1.9e+70) || !(n <= 2e-72)) tmp = Float64(n / Float64(n - f)); else tmp = Float64(-1.0 - Float64(n / f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((n <= -1.9e+70) || ~((n <= 2e-72))) tmp = n / (n - f); else tmp = -1.0 - (n / f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -1.9e+70], N[Not[LessEqual[n, 2e-72]], $MachinePrecision]], N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision], N[(-1.0 - N[(n / f), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -1.9 \cdot 10^{+70} \lor \neg \left(n \leq 2 \cdot 10^{-72}\right):\\
\;\;\;\;\frac{n}{n - f}\\
\mathbf{else}:\\
\;\;\;\;-1 - \frac{n}{f}\\
\end{array}
\end{array}
if n < -1.8999999999999999e70 or 1.9999999999999999e-72 < n 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 0 78.7%
if -1.8999999999999999e70 < n < 1.9999999999999999e-72Initial 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 82.4%
Taylor expanded in f around inf 82.5%
neg-mul-182.5%
neg-sub082.5%
associate--r+82.5%
+-commutative82.5%
associate--r+82.5%
metadata-eval82.5%
Simplified82.5%
Final simplification80.6%
(FPCore (f n) :precision binary64 (if (or (<= n -5.2e+70) (not (<= n 2.3e-72))) (+ 1.0 (/ f n)) (- -1.0 (/ n f))))
double code(double f, double n) {
double tmp;
if ((n <= -5.2e+70) || !(n <= 2.3e-72)) {
tmp = 1.0 + (f / n);
} else {
tmp = -1.0 - (n / f);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((n <= (-5.2d+70)) .or. (.not. (n <= 2.3d-72))) then
tmp = 1.0d0 + (f / n)
else
tmp = (-1.0d0) - (n / f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((n <= -5.2e+70) || !(n <= 2.3e-72)) {
tmp = 1.0 + (f / n);
} else {
tmp = -1.0 - (n / f);
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -5.2e+70) or not (n <= 2.3e-72): tmp = 1.0 + (f / n) else: tmp = -1.0 - (n / f) return tmp
function code(f, n) tmp = 0.0 if ((n <= -5.2e+70) || !(n <= 2.3e-72)) tmp = Float64(1.0 + Float64(f / n)); else tmp = Float64(-1.0 - Float64(n / f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((n <= -5.2e+70) || ~((n <= 2.3e-72))) tmp = 1.0 + (f / n); else tmp = -1.0 - (n / f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -5.2e+70], N[Not[LessEqual[n, 2.3e-72]], $MachinePrecision]], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision], N[(-1.0 - N[(n / f), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -5.2 \cdot 10^{+70} \lor \neg \left(n \leq 2.3 \cdot 10^{-72}\right):\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-1 - \frac{n}{f}\\
\end{array}
\end{array}
if n < -5.2000000000000001e70 or 2.29999999999999995e-72 < n 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 0 78.7%
Taylor expanded in n around inf 78.4%
if -5.2000000000000001e70 < n < 2.29999999999999995e-72Initial 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 82.4%
Taylor expanded in f around inf 82.5%
neg-mul-182.5%
neg-sub082.5%
associate--r+82.5%
+-commutative82.5%
associate--r+82.5%
metadata-eval82.5%
Simplified82.5%
Final simplification80.5%
(FPCore (f n) :precision binary64 (if (or (<= n -1.75e+70) (not (<= n 2.65e-74))) (+ 1.0 (/ f n)) -1.0))
double code(double f, double n) {
double tmp;
if ((n <= -1.75e+70) || !(n <= 2.65e-74)) {
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 ((n <= (-1.75d+70)) .or. (.not. (n <= 2.65d-74))) 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 ((n <= -1.75e+70) || !(n <= 2.65e-74)) {
tmp = 1.0 + (f / n);
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -1.75e+70) or not (n <= 2.65e-74): tmp = 1.0 + (f / n) else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if ((n <= -1.75e+70) || !(n <= 2.65e-74)) 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 ((n <= -1.75e+70) || ~((n <= 2.65e-74))) tmp = 1.0 + (f / n); else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -1.75e+70], N[Not[LessEqual[n, 2.65e-74]], $MachinePrecision]], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision], -1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -1.75 \cdot 10^{+70} \lor \neg \left(n \leq 2.65 \cdot 10^{-74}\right):\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if n < -1.75000000000000001e70 or 2.64999999999999994e-74 < n 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 0 78.7%
Taylor expanded in n around inf 78.4%
if -1.75000000000000001e70 < n < 2.64999999999999994e-74Initial 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 81.9%
Final simplification80.2%
(FPCore (f n) :precision binary64 (if (<= n -1.75e+70) 1.0 (if (<= n 1e-72) -1.0 1.0)))
double code(double f, double n) {
double tmp;
if (n <= -1.75e+70) {
tmp = 1.0;
} else if (n <= 1e-72) {
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 (n <= (-1.75d+70)) then
tmp = 1.0d0
else if (n <= 1d-72) 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 (n <= -1.75e+70) {
tmp = 1.0;
} else if (n <= 1e-72) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -1.75e+70: tmp = 1.0 elif n <= 1e-72: tmp = -1.0 else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (n <= -1.75e+70) tmp = 1.0; elseif (n <= 1e-72) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -1.75e+70) tmp = 1.0; elseif (n <= 1e-72) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -1.75e+70], 1.0, If[LessEqual[n, 1e-72], -1.0, 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -1.75 \cdot 10^{+70}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 10^{-72}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if n < -1.75000000000000001e70 or 9.9999999999999997e-73 < n 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 0 78.0%
if -1.75000000000000001e70 < n < 9.9999999999999997e-73Initial 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 81.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 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.6%
herbie shell --seed 2024123
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))