
(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 (<= n -1.02e+111) (+ (/ f n) 1.0) (if (<= n 9e-122) (+ (* -2.0 (/ n f)) -1.0) (/ n (- n f)))))
double code(double f, double n) {
double tmp;
if (n <= -1.02e+111) {
tmp = (f / n) + 1.0;
} else if (n <= 9e-122) {
tmp = (-2.0 * (n / f)) + -1.0;
} 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 <= (-1.02d+111)) then
tmp = (f / n) + 1.0d0
else if (n <= 9d-122) then
tmp = ((-2.0d0) * (n / f)) + (-1.0d0)
else
tmp = n / (n - f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if (n <= -1.02e+111) {
tmp = (f / n) + 1.0;
} else if (n <= 9e-122) {
tmp = (-2.0 * (n / f)) + -1.0;
} else {
tmp = n / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -1.02e+111: tmp = (f / n) + 1.0 elif n <= 9e-122: tmp = (-2.0 * (n / f)) + -1.0 else: tmp = n / (n - f) return tmp
function code(f, n) tmp = 0.0 if (n <= -1.02e+111) tmp = Float64(Float64(f / n) + 1.0); elseif (n <= 9e-122) tmp = Float64(Float64(-2.0 * Float64(n / f)) + -1.0); else tmp = Float64(n / Float64(n - f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -1.02e+111) tmp = (f / n) + 1.0; elseif (n <= 9e-122) tmp = (-2.0 * (n / f)) + -1.0; else tmp = n / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -1.02e+111], N[(N[(f / n), $MachinePrecision] + 1.0), $MachinePrecision], If[LessEqual[n, 9e-122], N[(N[(-2.0 * N[(n / f), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -1.02 \cdot 10^{+111}:\\
\;\;\;\;\frac{f}{n} + 1\\
\mathbf{elif}\;n \leq 9 \cdot 10^{-122}:\\
\;\;\;\;-2 \cdot \frac{n}{f} + -1\\
\mathbf{else}:\\
\;\;\;\;\frac{n}{n - f}\\
\end{array}
\end{array}
if n < -1.02e111Initial 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.3%
Taylor expanded in n around inf 82.4%
+-commutative82.4%
Simplified82.4%
if -1.02e111 < n < 8.99999999999999959e-122Initial 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.0%
if 8.99999999999999959e-122 < 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 72.6%
Final simplification79.2%
(FPCore (f n) :precision binary64 (if (or (<= n -9.8e+110) (not (<= n 2.4e-114))) (+ (/ f n) 1.0) (/ f (- n f))))
double code(double f, double n) {
double tmp;
if ((n <= -9.8e+110) || !(n <= 2.4e-114)) {
tmp = (f / n) + 1.0;
} 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 <= (-9.8d+110)) .or. (.not. (n <= 2.4d-114))) then
tmp = (f / n) + 1.0d0
else
tmp = f / (n - f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((n <= -9.8e+110) || !(n <= 2.4e-114)) {
tmp = (f / n) + 1.0;
} else {
tmp = f / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -9.8e+110) or not (n <= 2.4e-114): tmp = (f / n) + 1.0 else: tmp = f / (n - f) return tmp
function code(f, n) tmp = 0.0 if ((n <= -9.8e+110) || !(n <= 2.4e-114)) tmp = Float64(Float64(f / n) + 1.0); else tmp = Float64(f / Float64(n - f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((n <= -9.8e+110) || ~((n <= 2.4e-114))) tmp = (f / n) + 1.0; else tmp = f / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -9.8e+110], N[Not[LessEqual[n, 2.4e-114]], $MachinePrecision]], N[(N[(f / n), $MachinePrecision] + 1.0), $MachinePrecision], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -9.8 \cdot 10^{+110} \lor \neg \left(n \leq 2.4 \cdot 10^{-114}\right):\\
\;\;\;\;\frac{f}{n} + 1\\
\mathbf{else}:\\
\;\;\;\;\frac{f}{n - f}\\
\end{array}
\end{array}
if n < -9.80000000000000003e110 or 2.4000000000000001e-114 < 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 76.2%
Taylor expanded in n around inf 76.2%
+-commutative76.2%
Simplified76.2%
if -9.80000000000000003e110 < n < 2.4000000000000001e-114Initial 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 82.3%
Final simplification79.1%
(FPCore (f n) :precision binary64 (if (or (<= n -8e+110) (not (<= n 2.4e-114))) (+ (/ f n) 1.0) (- -1.0 (/ n f))))
double code(double f, double n) {
double tmp;
if ((n <= -8e+110) || !(n <= 2.4e-114)) {
tmp = (f / n) + 1.0;
} 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 <= (-8d+110)) .or. (.not. (n <= 2.4d-114))) then
tmp = (f / n) + 1.0d0
else
tmp = (-1.0d0) - (n / f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((n <= -8e+110) || !(n <= 2.4e-114)) {
tmp = (f / n) + 1.0;
} else {
tmp = -1.0 - (n / f);
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -8e+110) or not (n <= 2.4e-114): tmp = (f / n) + 1.0 else: tmp = -1.0 - (n / f) return tmp
function code(f, n) tmp = 0.0 if ((n <= -8e+110) || !(n <= 2.4e-114)) tmp = Float64(Float64(f / n) + 1.0); else tmp = Float64(-1.0 - Float64(n / f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((n <= -8e+110) || ~((n <= 2.4e-114))) tmp = (f / n) + 1.0; else tmp = -1.0 - (n / f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -8e+110], N[Not[LessEqual[n, 2.4e-114]], $MachinePrecision]], N[(N[(f / n), $MachinePrecision] + 1.0), $MachinePrecision], N[(-1.0 - N[(n / f), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -8 \cdot 10^{+110} \lor \neg \left(n \leq 2.4 \cdot 10^{-114}\right):\\
\;\;\;\;\frac{f}{n} + 1\\
\mathbf{else}:\\
\;\;\;\;-1 - \frac{n}{f}\\
\end{array}
\end{array}
if n < -8.0000000000000002e110 or 2.4000000000000001e-114 < 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 76.2%
Taylor expanded in n around inf 76.2%
+-commutative76.2%
Simplified76.2%
if -8.0000000000000002e110 < n < 2.4000000000000001e-114Initial 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 82.3%
Taylor expanded in f around inf 82.1%
sub-neg82.1%
metadata-eval82.1%
+-commutative82.1%
mul-1-neg82.1%
unsub-neg82.1%
Simplified82.1%
Final simplification79.1%
(FPCore (f n) :precision binary64 (if (or (<= n -1.86e+111) (not (<= n 2.4e-114))) (+ (/ f n) 1.0) -1.0))
double code(double f, double n) {
double tmp;
if ((n <= -1.86e+111) || !(n <= 2.4e-114)) {
tmp = (f / n) + 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.86d+111)) .or. (.not. (n <= 2.4d-114))) then
tmp = (f / n) + 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.86e+111) || !(n <= 2.4e-114)) {
tmp = (f / n) + 1.0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -1.86e+111) or not (n <= 2.4e-114): tmp = (f / n) + 1.0 else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if ((n <= -1.86e+111) || !(n <= 2.4e-114)) tmp = Float64(Float64(f / n) + 1.0); else tmp = -1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((n <= -1.86e+111) || ~((n <= 2.4e-114))) tmp = (f / n) + 1.0; else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -1.86e+111], N[Not[LessEqual[n, 2.4e-114]], $MachinePrecision]], N[(N[(f / n), $MachinePrecision] + 1.0), $MachinePrecision], -1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -1.86 \cdot 10^{+111} \lor \neg \left(n \leq 2.4 \cdot 10^{-114}\right):\\
\;\;\;\;\frac{f}{n} + 1\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if n < -1.85999999999999997e111 or 2.4000000000000001e-114 < 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 76.2%
Taylor expanded in n around inf 76.2%
+-commutative76.2%
Simplified76.2%
if -1.85999999999999997e111 < n < 2.4000000000000001e-114Initial 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.9%
Final simplification78.9%
(FPCore (f n) :precision binary64 (if (<= n -1.8e+111) (+ (/ f n) 1.0) (if (<= n 2.4e-114) (/ f (- n f)) (/ n (- n f)))))
double code(double f, double n) {
double tmp;
if (n <= -1.8e+111) {
tmp = (f / n) + 1.0;
} else if (n <= 2.4e-114) {
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 <= (-1.8d+111)) then
tmp = (f / n) + 1.0d0
else if (n <= 2.4d-114) 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 <= -1.8e+111) {
tmp = (f / n) + 1.0;
} else if (n <= 2.4e-114) {
tmp = f / (n - f);
} else {
tmp = n / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -1.8e+111: tmp = (f / n) + 1.0 elif n <= 2.4e-114: tmp = f / (n - f) else: tmp = n / (n - f) return tmp
function code(f, n) tmp = 0.0 if (n <= -1.8e+111) tmp = Float64(Float64(f / n) + 1.0); elseif (n <= 2.4e-114) 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 <= -1.8e+111) tmp = (f / n) + 1.0; elseif (n <= 2.4e-114) tmp = f / (n - f); else tmp = n / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -1.8e+111], N[(N[(f / n), $MachinePrecision] + 1.0), $MachinePrecision], If[LessEqual[n, 2.4e-114], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision], N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -1.8 \cdot 10^{+111}:\\
\;\;\;\;\frac{f}{n} + 1\\
\mathbf{elif}\;n \leq 2.4 \cdot 10^{-114}:\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;\frac{n}{n - f}\\
\end{array}
\end{array}
if n < -1.8000000000000001e111Initial 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.3%
Taylor expanded in n around inf 82.4%
+-commutative82.4%
Simplified82.4%
if -1.8000000000000001e111 < n < 2.4000000000000001e-114Initial 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 82.3%
if 2.4000000000000001e-114 < 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 73.0%
(FPCore (f n) :precision binary64 (if (<= n -9.8e+110) 1.0 (if (<= n 9e-122) -1.0 1.0)))
double code(double f, double n) {
double tmp;
if (n <= -9.8e+110) {
tmp = 1.0;
} else if (n <= 9e-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 (n <= (-9.8d+110)) then
tmp = 1.0d0
else if (n <= 9d-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 (n <= -9.8e+110) {
tmp = 1.0;
} else if (n <= 9e-122) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -9.8e+110: tmp = 1.0 elif n <= 9e-122: tmp = -1.0 else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (n <= -9.8e+110) tmp = 1.0; elseif (n <= 9e-122) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -9.8e+110) tmp = 1.0; elseif (n <= 9e-122) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -9.8e+110], 1.0, If[LessEqual[n, 9e-122], -1.0, 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -9.8 \cdot 10^{+110}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 9 \cdot 10^{-122}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if n < -9.80000000000000003e110 or 8.99999999999999959e-122 < 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 75.2%
if -9.80000000000000003e110 < n < 8.99999999999999959e-122Initial 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 82.4%
(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 51.7%
herbie shell --seed 2024185
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))