
(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 7 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 -5.4e+25) (+ 1.0 (/ f n)) (if (<= n 1.05e-54) (+ (* -2.0 (/ n f)) -1.0) (/ n (- n f)))))
double code(double f, double n) {
double tmp;
if (n <= -5.4e+25) {
tmp = 1.0 + (f / n);
} else if (n <= 1.05e-54) {
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 <= (-5.4d+25)) then
tmp = 1.0d0 + (f / n)
else if (n <= 1.05d-54) 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 <= -5.4e+25) {
tmp = 1.0 + (f / n);
} else if (n <= 1.05e-54) {
tmp = (-2.0 * (n / f)) + -1.0;
} else {
tmp = n / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -5.4e+25: tmp = 1.0 + (f / n) elif n <= 1.05e-54: tmp = (-2.0 * (n / f)) + -1.0 else: tmp = n / (n - f) return tmp
function code(f, n) tmp = 0.0 if (n <= -5.4e+25) tmp = Float64(1.0 + Float64(f / n)); elseif (n <= 1.05e-54) 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 <= -5.4e+25) tmp = 1.0 + (f / n); elseif (n <= 1.05e-54) tmp = (-2.0 * (n / f)) + -1.0; else tmp = n / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -5.4e+25], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision], If[LessEqual[n, 1.05e-54], 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 -5.4 \cdot 10^{+25}:\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{elif}\;n \leq 1.05 \cdot 10^{-54}:\\
\;\;\;\;-2 \cdot \frac{n}{f} + -1\\
\mathbf{else}:\\
\;\;\;\;\frac{n}{n - f}\\
\end{array}
\end{array}
if n < -5.4e25Initial 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.0%
Taylor expanded in n around inf 72.3%
if -5.4e25 < n < 1.05e-54Initial 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 80.6%
if 1.05e-54 < 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.5%
Final simplification77.6%
(FPCore (f n) :precision binary64 (if (or (<= n -8.2e+18) (not (<= n 1.05e-54))) (+ 1.0 (/ f n)) (- -1.0 (/ n f))))
double code(double f, double n) {
double tmp;
if ((n <= -8.2e+18) || !(n <= 1.05e-54)) {
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 <= (-8.2d+18)) .or. (.not. (n <= 1.05d-54))) 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 <= -8.2e+18) || !(n <= 1.05e-54)) {
tmp = 1.0 + (f / n);
} else {
tmp = -1.0 - (n / f);
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -8.2e+18) or not (n <= 1.05e-54): tmp = 1.0 + (f / n) else: tmp = -1.0 - (n / f) return tmp
function code(f, n) tmp = 0.0 if ((n <= -8.2e+18) || !(n <= 1.05e-54)) 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 <= -8.2e+18) || ~((n <= 1.05e-54))) tmp = 1.0 + (f / n); else tmp = -1.0 - (n / f); end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -8.2e+18], N[Not[LessEqual[n, 1.05e-54]], $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 -8.2 \cdot 10^{+18} \lor \neg \left(n \leq 1.05 \cdot 10^{-54}\right):\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-1 - \frac{n}{f}\\
\end{array}
\end{array}
if n < -8.2e18 or 1.05e-54 < 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 74.5%
Taylor expanded in n around inf 74.5%
if -8.2e18 < n < 1.05e-54Initial 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 79.6%
Taylor expanded in f around inf 79.7%
neg-mul-179.7%
neg-sub079.7%
associate--r+79.7%
+-commutative79.7%
associate--r+79.7%
metadata-eval79.7%
Simplified79.7%
Final simplification77.1%
(FPCore (f n) :precision binary64 (if (or (<= n -5e+30) (not (<= n 4.8e-55))) (+ 1.0 (/ f n)) -1.0))
double code(double f, double n) {
double tmp;
if ((n <= -5e+30) || !(n <= 4.8e-55)) {
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 <= (-5d+30)) .or. (.not. (n <= 4.8d-55))) 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 <= -5e+30) || !(n <= 4.8e-55)) {
tmp = 1.0 + (f / n);
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -5e+30) or not (n <= 4.8e-55): tmp = 1.0 + (f / n) else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if ((n <= -5e+30) || !(n <= 4.8e-55)) 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 <= -5e+30) || ~((n <= 4.8e-55))) tmp = 1.0 + (f / n); else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -5e+30], N[Not[LessEqual[n, 4.8e-55]], $MachinePrecision]], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision], -1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -5 \cdot 10^{+30} \lor \neg \left(n \leq 4.8 \cdot 10^{-55}\right):\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if n < -4.9999999999999998e30 or 4.79999999999999983e-55 < 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 74.5%
Taylor expanded in n around inf 74.5%
if -4.9999999999999998e30 < n < 4.79999999999999983e-55Initial 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 79.0%
Final simplification76.7%
(FPCore (f n) :precision binary64 (if (<= n -3.8e+28) (+ 1.0 (/ f n)) (if (<= n 6e-57) (- -1.0 (/ n f)) (/ n (- n f)))))
double code(double f, double n) {
double tmp;
if (n <= -3.8e+28) {
tmp = 1.0 + (f / n);
} else if (n <= 6e-57) {
tmp = -1.0 - (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 <= (-3.8d+28)) then
tmp = 1.0d0 + (f / n)
else if (n <= 6d-57) then
tmp = (-1.0d0) - (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 <= -3.8e+28) {
tmp = 1.0 + (f / n);
} else if (n <= 6e-57) {
tmp = -1.0 - (n / f);
} else {
tmp = n / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -3.8e+28: tmp = 1.0 + (f / n) elif n <= 6e-57: tmp = -1.0 - (n / f) else: tmp = n / (n - f) return tmp
function code(f, n) tmp = 0.0 if (n <= -3.8e+28) tmp = Float64(1.0 + Float64(f / n)); elseif (n <= 6e-57) tmp = Float64(-1.0 - 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 <= -3.8e+28) tmp = 1.0 + (f / n); elseif (n <= 6e-57) tmp = -1.0 - (n / f); else tmp = n / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -3.8e+28], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision], If[LessEqual[n, 6e-57], N[(-1.0 - N[(n / f), $MachinePrecision]), $MachinePrecision], N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -3.8 \cdot 10^{+28}:\\
\;\;\;\;1 + \frac{f}{n}\\
\mathbf{elif}\;n \leq 6 \cdot 10^{-57}:\\
\;\;\;\;-1 - \frac{n}{f}\\
\mathbf{else}:\\
\;\;\;\;\frac{n}{n - f}\\
\end{array}
\end{array}
if n < -3.7999999999999999e28Initial 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.0%
Taylor expanded in n around inf 72.3%
if -3.7999999999999999e28 < n < 6.00000000000000001e-57Initial 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 79.6%
Taylor expanded in f around inf 79.7%
neg-mul-179.7%
neg-sub079.7%
associate--r+79.7%
+-commutative79.7%
associate--r+79.7%
metadata-eval79.7%
Simplified79.7%
if 6.00000000000000001e-57 < 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.5%
(FPCore (f n) :precision binary64 (if (<= n -1.15e+23) 1.0 (if (<= n 6e-57) -1.0 1.0)))
double code(double f, double n) {
double tmp;
if (n <= -1.15e+23) {
tmp = 1.0;
} else if (n <= 6e-57) {
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.15d+23)) then
tmp = 1.0d0
else if (n <= 6d-57) 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.15e+23) {
tmp = 1.0;
} else if (n <= 6e-57) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -1.15e+23: tmp = 1.0 elif n <= 6e-57: tmp = -1.0 else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (n <= -1.15e+23) tmp = 1.0; elseif (n <= 6e-57) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -1.15e+23) tmp = 1.0; elseif (n <= 6e-57) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -1.15e+23], 1.0, If[LessEqual[n, 6e-57], -1.0, 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -1.15 \cdot 10^{+23}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 6 \cdot 10^{-57}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if n < -1.15e23 or 6.00000000000000001e-57 < 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.8%
if -1.15e23 < n < 6.00000000000000001e-57Initial 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 79.0%
(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 2024136
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))