
(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 5 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 -3.1e+64)
(not (or (<= n -5.1e+29) (and (not (<= n -3e-107)) (<= n 5e-72)))))
(+ 1.0 (* 2.0 (/ f n)))
(+ (* -2.0 (/ n f)) -1.0)))
double code(double f, double n) {
double tmp;
if ((n <= -3.1e+64) || !((n <= -5.1e+29) || (!(n <= -3e-107) && (n <= 5e-72)))) {
tmp = 1.0 + (2.0 * (f / 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 <= (-3.1d+64)) .or. (.not. (n <= (-5.1d+29)) .or. (.not. (n <= (-3d-107))) .and. (n <= 5d-72))) then
tmp = 1.0d0 + (2.0d0 * (f / 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 <= -3.1e+64) || !((n <= -5.1e+29) || (!(n <= -3e-107) && (n <= 5e-72)))) {
tmp = 1.0 + (2.0 * (f / n));
} else {
tmp = (-2.0 * (n / f)) + -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -3.1e+64) or not ((n <= -5.1e+29) or (not (n <= -3e-107) and (n <= 5e-72))): tmp = 1.0 + (2.0 * (f / n)) else: tmp = (-2.0 * (n / f)) + -1.0 return tmp
function code(f, n) tmp = 0.0 if ((n <= -3.1e+64) || !((n <= -5.1e+29) || (!(n <= -3e-107) && (n <= 5e-72)))) tmp = Float64(1.0 + Float64(2.0 * Float64(f / 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 <= -3.1e+64) || ~(((n <= -5.1e+29) || (~((n <= -3e-107)) && (n <= 5e-72))))) tmp = 1.0 + (2.0 * (f / n)); else tmp = (-2.0 * (n / f)) + -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -3.1e+64], N[Not[Or[LessEqual[n, -5.1e+29], And[N[Not[LessEqual[n, -3e-107]], $MachinePrecision], LessEqual[n, 5e-72]]]], $MachinePrecision]], N[(1.0 + N[(2.0 * N[(f / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(-2.0 * N[(n / f), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -3.1 \cdot 10^{+64} \lor \neg \left(n \leq -5.1 \cdot 10^{+29} \lor \neg \left(n \leq -3 \cdot 10^{-107}\right) \land n \leq 5 \cdot 10^{-72}\right):\\
\;\;\;\;1 + 2 \cdot \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-2 \cdot \frac{n}{f} + -1\\
\end{array}
\end{array}
if n < -3.0999999999999999e64 or -5.1000000000000001e29 < n < -2.9999999999999997e-107 or 4.9999999999999996e-72 < 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.7%
if -3.0999999999999999e64 < n < -5.1000000000000001e29 or -2.9999999999999997e-107 < n < 4.9999999999999996e-72Initial 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 87.5%
Final simplification79.0%
(FPCore (f n)
:precision binary64
(if (or (<= n -5.7e+63)
(and (not (<= n -7.5e+31)) (or (<= n -6e-113) (not (<= n 5.6e-72)))))
(+ 1.0 (* 2.0 (/ f n)))
-1.0))
double code(double f, double n) {
double tmp;
if ((n <= -5.7e+63) || (!(n <= -7.5e+31) && ((n <= -6e-113) || !(n <= 5.6e-72)))) {
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 ((n <= (-5.7d+63)) .or. (.not. (n <= (-7.5d+31))) .and. (n <= (-6d-113)) .or. (.not. (n <= 5.6d-72))) 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 ((n <= -5.7e+63) || (!(n <= -7.5e+31) && ((n <= -6e-113) || !(n <= 5.6e-72)))) {
tmp = 1.0 + (2.0 * (f / n));
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -5.7e+63) or (not (n <= -7.5e+31) and ((n <= -6e-113) or not (n <= 5.6e-72))): tmp = 1.0 + (2.0 * (f / n)) else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if ((n <= -5.7e+63) || (!(n <= -7.5e+31) && ((n <= -6e-113) || !(n <= 5.6e-72)))) 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 ((n <= -5.7e+63) || (~((n <= -7.5e+31)) && ((n <= -6e-113) || ~((n <= 5.6e-72))))) tmp = 1.0 + (2.0 * (f / n)); else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -5.7e+63], And[N[Not[LessEqual[n, -7.5e+31]], $MachinePrecision], Or[LessEqual[n, -6e-113], N[Not[LessEqual[n, 5.6e-72]], $MachinePrecision]]]], N[(1.0 + N[(2.0 * N[(f / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -5.7 \cdot 10^{+63} \lor \neg \left(n \leq -7.5 \cdot 10^{+31}\right) \land \left(n \leq -6 \cdot 10^{-113} \lor \neg \left(n \leq 5.6 \cdot 10^{-72}\right)\right):\\
\;\;\;\;1 + 2 \cdot \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if n < -5.7000000000000002e63 or -7.5e31 < n < -6.0000000000000002e-113 or 5.5999999999999996e-72 < 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.7%
if -5.7000000000000002e63 < n < -7.5e31 or -6.0000000000000002e-113 < n < 5.5999999999999996e-72Initial 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 86.7%
Final simplification78.6%
(FPCore (f n)
:precision binary64
(if (<= n -5.7e+63)
1.0
(if (<= n -7e+15)
-1.0
(if (<= n -3.4e-107) 1.0 (if (<= n 4e-84) -1.0 1.0)))))
double code(double f, double n) {
double tmp;
if (n <= -5.7e+63) {
tmp = 1.0;
} else if (n <= -7e+15) {
tmp = -1.0;
} else if (n <= -3.4e-107) {
tmp = 1.0;
} else if (n <= 4e-84) {
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 <= (-5.7d+63)) then
tmp = 1.0d0
else if (n <= (-7d+15)) then
tmp = -1.0d0
else if (n <= (-3.4d-107)) then
tmp = 1.0d0
else if (n <= 4d-84) 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 <= -5.7e+63) {
tmp = 1.0;
} else if (n <= -7e+15) {
tmp = -1.0;
} else if (n <= -3.4e-107) {
tmp = 1.0;
} else if (n <= 4e-84) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -5.7e+63: tmp = 1.0 elif n <= -7e+15: tmp = -1.0 elif n <= -3.4e-107: tmp = 1.0 elif n <= 4e-84: tmp = -1.0 else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (n <= -5.7e+63) tmp = 1.0; elseif (n <= -7e+15) tmp = -1.0; elseif (n <= -3.4e-107) tmp = 1.0; elseif (n <= 4e-84) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -5.7e+63) tmp = 1.0; elseif (n <= -7e+15) tmp = -1.0; elseif (n <= -3.4e-107) tmp = 1.0; elseif (n <= 4e-84) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -5.7e+63], 1.0, If[LessEqual[n, -7e+15], -1.0, If[LessEqual[n, -3.4e-107], 1.0, If[LessEqual[n, 4e-84], -1.0, 1.0]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -5.7 \cdot 10^{+63}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq -7 \cdot 10^{+15}:\\
\;\;\;\;-1\\
\mathbf{elif}\;n \leq -3.4 \cdot 10^{-107}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 4 \cdot 10^{-84}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if n < -5.7000000000000002e63 or -7e15 < n < -3.39999999999999994e-107 or 4.0000000000000001e-84 < 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.6%
if -5.7000000000000002e63 < n < -7e15 or -3.39999999999999994e-107 < n < 4.0000000000000001e-84Initial 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 85.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 49.3%
herbie shell --seed 2024106
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))