
(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 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%
(FPCore (f n) :precision binary64 (if (or (<= n -8e-36) (not (<= n 2.3e+27))) (+ 1.0 (/ (* f 2.0) n)) (+ (* -2.0 (/ n f)) -1.0)))
double code(double f, double n) {
double tmp;
if ((n <= -8e-36) || !(n <= 2.3e+27)) {
tmp = 1.0 + ((f * 2.0) / n);
} 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 <= (-8d-36)) .or. (.not. (n <= 2.3d+27))) then
tmp = 1.0d0 + ((f * 2.0d0) / n)
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 <= -8e-36) || !(n <= 2.3e+27)) {
tmp = 1.0 + ((f * 2.0) / n);
} else {
tmp = (-2.0 * (n / f)) + -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -8e-36) or not (n <= 2.3e+27): tmp = 1.0 + ((f * 2.0) / n) else: tmp = (-2.0 * (n / f)) + -1.0 return tmp
function code(f, n) tmp = 0.0 if ((n <= -8e-36) || !(n <= 2.3e+27)) tmp = Float64(1.0 + Float64(Float64(f * 2.0) / n)); 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 <= -8e-36) || ~((n <= 2.3e+27))) tmp = 1.0 + ((f * 2.0) / n); else tmp = (-2.0 * (n / f)) + -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -8e-36], N[Not[LessEqual[n, 2.3e+27]], $MachinePrecision]], N[(1.0 + N[(N[(f * 2.0), $MachinePrecision] / n), $MachinePrecision]), $MachinePrecision], N[(N[(-2.0 * N[(n / f), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -8 \cdot 10^{-36} \lor \neg \left(n \leq 2.3 \cdot 10^{+27}\right):\\
\;\;\;\;1 + \frac{f \cdot 2}{n}\\
\mathbf{else}:\\
\;\;\;\;-2 \cdot \frac{n}{f} + -1\\
\end{array}
\end{array}
if n < -7.9999999999999995e-36 or 2.3000000000000001e27 < n 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 0 74.6%
associate-*r/74.6%
Simplified74.6%
if -7.9999999999999995e-36 < n < 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 n around 0 78.6%
Final simplification76.8%
(FPCore (f n) :precision binary64 (if (or (<= n -2.5e-36) (not (<= n 2.1e+26))) (+ 1.0 (/ (* f 2.0) n)) (/ f (- n f))))
double code(double f, double n) {
double tmp;
if ((n <= -2.5e-36) || !(n <= 2.1e+26)) {
tmp = 1.0 + ((f * 2.0) / n);
} 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 <= (-2.5d-36)) .or. (.not. (n <= 2.1d+26))) then
tmp = 1.0d0 + ((f * 2.0d0) / n)
else
tmp = f / (n - f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((n <= -2.5e-36) || !(n <= 2.1e+26)) {
tmp = 1.0 + ((f * 2.0) / n);
} else {
tmp = f / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -2.5e-36) or not (n <= 2.1e+26): tmp = 1.0 + ((f * 2.0) / n) else: tmp = f / (n - f) return tmp
function code(f, n) tmp = 0.0 if ((n <= -2.5e-36) || !(n <= 2.1e+26)) tmp = Float64(1.0 + Float64(Float64(f * 2.0) / n)); else tmp = Float64(f / Float64(n - f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((n <= -2.5e-36) || ~((n <= 2.1e+26))) tmp = 1.0 + ((f * 2.0) / n); else tmp = f / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -2.5e-36], N[Not[LessEqual[n, 2.1e+26]], $MachinePrecision]], N[(1.0 + N[(N[(f * 2.0), $MachinePrecision] / n), $MachinePrecision]), $MachinePrecision], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -2.5 \cdot 10^{-36} \lor \neg \left(n \leq 2.1 \cdot 10^{+26}\right):\\
\;\;\;\;1 + \frac{f \cdot 2}{n}\\
\mathbf{else}:\\
\;\;\;\;\frac{f}{n - f}\\
\end{array}
\end{array}
if n < -2.50000000000000002e-36 or 2.1000000000000001e26 < n 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 0 74.6%
associate-*r/74.6%
Simplified74.6%
if -2.50000000000000002e-36 < n < 2.1000000000000001e26Initial 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 78.3%
Final simplification76.6%
(FPCore (f n) :precision binary64 (if (or (<= n -5.3e-36) (not (<= n 3.7e+28))) (+ 1.0 (/ f n)) (/ f (- n f))))
double code(double f, double n) {
double tmp;
if ((n <= -5.3e-36) || !(n <= 3.7e+28)) {
tmp = 1.0 + (f / n);
} 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 <= (-5.3d-36)) .or. (.not. (n <= 3.7d+28))) then
tmp = 1.0d0 + (f / n)
else
tmp = f / (n - f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((n <= -5.3e-36) || !(n <= 3.7e+28)) {
tmp = 1.0 + (f / n);
} else {
tmp = f / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -5.3e-36) or not (n <= 3.7e+28): tmp = 1.0 + (f / n) else: tmp = f / (n - f) return tmp
function code(f, n) tmp = 0.0 if ((n <= -5.3e-36) || !(n <= 3.7e+28)) tmp = Float64(1.0 + Float64(f / n)); else tmp = Float64(f / Float64(n - f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((n <= -5.3e-36) || ~((n <= 3.7e+28))) tmp = 1.0 + (f / n); else tmp = f / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -5.3e-36], N[Not[LessEqual[n, 3.7e+28]], $MachinePrecision]], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -5.3 \cdot 10^{-36} \lor \neg \left(n \leq 3.7 \cdot 10^{+28}\right):\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;\frac{f}{n - f}\\
\end{array}
\end{array}
if n < -5.2999999999999998e-36 or 3.6999999999999999e28 < n 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 0 73.5%
Taylor expanded in n around inf 73.6%
if -5.2999999999999998e-36 < n < 3.6999999999999999e28Initial 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 78.3%
Final simplification76.1%
(FPCore (f n) :precision binary64 (if (or (<= n -1.95e-36) (not (<= n 4e+28))) (+ 1.0 (/ f n)) -1.0))
double code(double f, double n) {
double tmp;
if ((n <= -1.95e-36) || !(n <= 4e+28)) {
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.95d-36)) .or. (.not. (n <= 4d+28))) 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.95e-36) || !(n <= 4e+28)) {
tmp = 1.0 + (f / n);
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -1.95e-36) or not (n <= 4e+28): tmp = 1.0 + (f / n) else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if ((n <= -1.95e-36) || !(n <= 4e+28)) 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.95e-36) || ~((n <= 4e+28))) tmp = 1.0 + (f / n); else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -1.95e-36], N[Not[LessEqual[n, 4e+28]], $MachinePrecision]], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision], -1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -1.95 \cdot 10^{-36} \lor \neg \left(n \leq 4 \cdot 10^{+28}\right):\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if n < -1.95e-36 or 3.99999999999999983e28 < n 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 0 73.5%
Taylor expanded in n around inf 73.6%
if -1.95e-36 < n < 3.99999999999999983e28Initial 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 77.5%
Final simplification75.7%
(FPCore (f n) :precision binary64 (if (<= n -9.2e-39) (+ 1.0 (/ f n)) (if (<= n 1.4e+26) (/ f (- n f)) (/ n (- n f)))))
double code(double f, double n) {
double tmp;
if (n <= -9.2e-39) {
tmp = 1.0 + (f / n);
} else if (n <= 1.4e+26) {
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 (n <= (-9.2d-39)) then
tmp = 1.0d0 + (f / n)
else if (n <= 1.4d+26) 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 (n <= -9.2e-39) {
tmp = 1.0 + (f / n);
} else if (n <= 1.4e+26) {
tmp = f / (n - f);
} else {
tmp = n / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -9.2e-39: tmp = 1.0 + (f / n) elif n <= 1.4e+26: tmp = f / (n - f) else: tmp = n / (n - f) return tmp
function code(f, n) tmp = 0.0 if (n <= -9.2e-39) tmp = Float64(1.0 + Float64(f / n)); elseif (n <= 1.4e+26) 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 (n <= -9.2e-39) tmp = 1.0 + (f / n); elseif (n <= 1.4e+26) tmp = f / (n - f); else tmp = n / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -9.2e-39], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision], If[LessEqual[n, 1.4e+26], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision], N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -9.2 \cdot 10^{-39}:\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{elif}\;n \leq 1.4 \cdot 10^{+26}:\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;\frac{n}{n - f}\\
\end{array}
\end{array}
if n < -9.20000000000000033e-39Initial 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 71.6%
Taylor expanded in n around inf 71.8%
if -9.20000000000000033e-39 < n < 1.4e26Initial 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 78.3%
if 1.4e26 < n Initial program 99.8%
distribute-frac-neg99.8%
distribute-neg-frac299.8%
sub-neg99.8%
+-commutative99.8%
distribute-neg-in99.8%
remove-double-neg99.8%
sub-neg99.8%
Simplified99.8%
Taylor expanded in f around 0 75.5%
(FPCore (f n) :precision binary64 (if (<= n -1e-35) 1.0 (if (<= n 2.5e+27) -1.0 1.0)))
double code(double f, double n) {
double tmp;
if (n <= -1e-35) {
tmp = 1.0;
} else if (n <= 2.5e+27) {
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 <= (-1d-35)) then
tmp = 1.0d0
else if (n <= 2.5d+27) 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 <= -1e-35) {
tmp = 1.0;
} else if (n <= 2.5e+27) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -1e-35: tmp = 1.0 elif n <= 2.5e+27: tmp = -1.0 else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (n <= -1e-35) tmp = 1.0; elseif (n <= 2.5e+27) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -1e-35) tmp = 1.0; elseif (n <= 2.5e+27) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -1e-35], 1.0, If[LessEqual[n, 2.5e+27], -1.0, 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -1 \cdot 10^{-35}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 2.5 \cdot 10^{+27}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if n < -1.00000000000000001e-35 or 2.4999999999999999e27 < n 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 0 72.8%
if -1.00000000000000001e-35 < n < 2.4999999999999999e27Initial 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 77.5%
(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 52.5%
herbie shell --seed 2024110
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))