
(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 100.0%
sub-neg100.0%
remove-double-neg100.0%
distribute-neg-in100.0%
neg-mul-1100.0%
associate-/r*100.0%
neg-mul-1100.0%
*-commutative100.0%
associate-/l*100.0%
metadata-eval100.0%
/-rgt-identity100.0%
+-commutative100.0%
sub-neg100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (f n)
:precision binary64
(if (or (<= n -4.7e-24)
(not (or (<= n 6.5e-16) (and (not (<= n 6e+17)) (<= n 1.1e+50)))))
(+ 1.0 (* 2.0 (/ f n)))
-1.0))
double code(double f, double n) {
double tmp;
if ((n <= -4.7e-24) || !((n <= 6.5e-16) || (!(n <= 6e+17) && (n <= 1.1e+50)))) {
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 <= (-4.7d-24)) .or. (.not. (n <= 6.5d-16) .or. (.not. (n <= 6d+17)) .and. (n <= 1.1d+50))) 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 <= -4.7e-24) || !((n <= 6.5e-16) || (!(n <= 6e+17) && (n <= 1.1e+50)))) {
tmp = 1.0 + (2.0 * (f / n));
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (n <= -4.7e-24) or not ((n <= 6.5e-16) or (not (n <= 6e+17) and (n <= 1.1e+50))): tmp = 1.0 + (2.0 * (f / n)) else: tmp = -1.0 return tmp
function code(f, n) tmp = 0.0 if ((n <= -4.7e-24) || !((n <= 6.5e-16) || (!(n <= 6e+17) && (n <= 1.1e+50)))) 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 <= -4.7e-24) || ~(((n <= 6.5e-16) || (~((n <= 6e+17)) && (n <= 1.1e+50))))) tmp = 1.0 + (2.0 * (f / n)); else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := If[Or[LessEqual[n, -4.7e-24], N[Not[Or[LessEqual[n, 6.5e-16], And[N[Not[LessEqual[n, 6e+17]], $MachinePrecision], LessEqual[n, 1.1e+50]]]], $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 -4.7 \cdot 10^{-24} \lor \neg \left(n \leq 6.5 \cdot 10^{-16} \lor \neg \left(n \leq 6 \cdot 10^{+17}\right) \land n \leq 1.1 \cdot 10^{+50}\right):\\
\;\;\;\;1 + 2 \cdot \frac{f}{n}\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if n < -4.69999999999999992e-24 or 6.50000000000000011e-16 < n < 6e17 or 1.10000000000000008e50 < n Initial program 100.0%
sub-neg100.0%
remove-double-neg100.0%
distribute-neg-in100.0%
neg-mul-1100.0%
associate-/r*100.0%
neg-mul-1100.0%
*-commutative100.0%
associate-/l*100.0%
metadata-eval100.0%
/-rgt-identity100.0%
+-commutative100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in f around 0 79.7%
if -4.69999999999999992e-24 < n < 6.50000000000000011e-16 or 6e17 < n < 1.10000000000000008e50Initial program 100.0%
sub-neg100.0%
remove-double-neg100.0%
distribute-neg-in100.0%
neg-mul-1100.0%
associate-/r*100.0%
neg-mul-1100.0%
*-commutative100.0%
associate-/l*100.0%
metadata-eval100.0%
/-rgt-identity100.0%
+-commutative100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in f around inf 80.6%
Final simplification80.1%
(FPCore (f n)
:precision binary64
(let* ((t_0 (+ 1.0 (* 2.0 (/ f n)))))
(if (<= n -6.5e-24)
t_0
(if (<= n 4.2e-20)
(+ (* -2.0 (/ n f)) -1.0)
(if (or (<= n 2.1e+18) (not (<= n 1.5e+49))) t_0 -1.0)))))
double code(double f, double n) {
double t_0 = 1.0 + (2.0 * (f / n));
double tmp;
if (n <= -6.5e-24) {
tmp = t_0;
} else if (n <= 4.2e-20) {
tmp = (-2.0 * (n / f)) + -1.0;
} else if ((n <= 2.1e+18) || !(n <= 1.5e+49)) {
tmp = t_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) :: t_0
real(8) :: tmp
t_0 = 1.0d0 + (2.0d0 * (f / n))
if (n <= (-6.5d-24)) then
tmp = t_0
else if (n <= 4.2d-20) then
tmp = ((-2.0d0) * (n / f)) + (-1.0d0)
else if ((n <= 2.1d+18) .or. (.not. (n <= 1.5d+49))) then
tmp = t_0
else
tmp = -1.0d0
end if
code = tmp
end function
public static double code(double f, double n) {
double t_0 = 1.0 + (2.0 * (f / n));
double tmp;
if (n <= -6.5e-24) {
tmp = t_0;
} else if (n <= 4.2e-20) {
tmp = (-2.0 * (n / f)) + -1.0;
} else if ((n <= 2.1e+18) || !(n <= 1.5e+49)) {
tmp = t_0;
} else {
tmp = -1.0;
}
return tmp;
}
def code(f, n): t_0 = 1.0 + (2.0 * (f / n)) tmp = 0 if n <= -6.5e-24: tmp = t_0 elif n <= 4.2e-20: tmp = (-2.0 * (n / f)) + -1.0 elif (n <= 2.1e+18) or not (n <= 1.5e+49): tmp = t_0 else: tmp = -1.0 return tmp
function code(f, n) t_0 = Float64(1.0 + Float64(2.0 * Float64(f / n))) tmp = 0.0 if (n <= -6.5e-24) tmp = t_0; elseif (n <= 4.2e-20) tmp = Float64(Float64(-2.0 * Float64(n / f)) + -1.0); elseif ((n <= 2.1e+18) || !(n <= 1.5e+49)) tmp = t_0; else tmp = -1.0; end return tmp end
function tmp_2 = code(f, n) t_0 = 1.0 + (2.0 * (f / n)); tmp = 0.0; if (n <= -6.5e-24) tmp = t_0; elseif (n <= 4.2e-20) tmp = (-2.0 * (n / f)) + -1.0; elseif ((n <= 2.1e+18) || ~((n <= 1.5e+49))) tmp = t_0; else tmp = -1.0; end tmp_2 = tmp; end
code[f_, n_] := Block[{t$95$0 = N[(1.0 + N[(2.0 * N[(f / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[n, -6.5e-24], t$95$0, If[LessEqual[n, 4.2e-20], N[(N[(-2.0 * N[(n / f), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], If[Or[LessEqual[n, 2.1e+18], N[Not[LessEqual[n, 1.5e+49]], $MachinePrecision]], t$95$0, -1.0]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 1 + 2 \cdot \frac{f}{n}\\
\mathbf{if}\;n \leq -6.5 \cdot 10^{-24}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;n \leq 4.2 \cdot 10^{-20}:\\
\;\;\;\;-2 \cdot \frac{n}{f} + -1\\
\mathbf{elif}\;n \leq 2.1 \cdot 10^{+18} \lor \neg \left(n \leq 1.5 \cdot 10^{+49}\right):\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;-1\\
\end{array}
\end{array}
if n < -6.5e-24 or 4.1999999999999998e-20 < n < 2.1e18 or 1.5000000000000001e49 < n Initial program 100.0%
sub-neg100.0%
remove-double-neg100.0%
distribute-neg-in100.0%
neg-mul-1100.0%
associate-/r*100.0%
neg-mul-1100.0%
*-commutative100.0%
associate-/l*100.0%
metadata-eval100.0%
/-rgt-identity100.0%
+-commutative100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in f around 0 79.7%
if -6.5e-24 < n < 4.1999999999999998e-20Initial program 100.0%
sub-neg100.0%
remove-double-neg100.0%
distribute-neg-in100.0%
neg-mul-1100.0%
associate-/r*100.0%
neg-mul-1100.0%
*-commutative100.0%
associate-/l*100.0%
metadata-eval100.0%
/-rgt-identity100.0%
+-commutative100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in n around 0 80.4%
if 2.1e18 < n < 1.5000000000000001e49Initial program 100.0%
sub-neg100.0%
remove-double-neg100.0%
distribute-neg-in100.0%
neg-mul-1100.0%
associate-/r*100.0%
neg-mul-1100.0%
*-commutative100.0%
associate-/l*100.0%
metadata-eval100.0%
/-rgt-identity100.0%
+-commutative100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in f around inf 100.0%
Final simplification80.6%
(FPCore (f n)
:precision binary64
(if (<= n -3.15e-24)
1.0
(if (<= n 6.4e-12)
-1.0
(if (<= n 6.6e+17) 1.0 (if (<= n 1.5e+49) -1.0 1.0)))))
double code(double f, double n) {
double tmp;
if (n <= -3.15e-24) {
tmp = 1.0;
} else if (n <= 6.4e-12) {
tmp = -1.0;
} else if (n <= 6.6e+17) {
tmp = 1.0;
} else if (n <= 1.5e+49) {
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 <= (-3.15d-24)) then
tmp = 1.0d0
else if (n <= 6.4d-12) then
tmp = -1.0d0
else if (n <= 6.6d+17) then
tmp = 1.0d0
else if (n <= 1.5d+49) 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 <= -3.15e-24) {
tmp = 1.0;
} else if (n <= 6.4e-12) {
tmp = -1.0;
} else if (n <= 6.6e+17) {
tmp = 1.0;
} else if (n <= 1.5e+49) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -3.15e-24: tmp = 1.0 elif n <= 6.4e-12: tmp = -1.0 elif n <= 6.6e+17: tmp = 1.0 elif n <= 1.5e+49: tmp = -1.0 else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (n <= -3.15e-24) tmp = 1.0; elseif (n <= 6.4e-12) tmp = -1.0; elseif (n <= 6.6e+17) tmp = 1.0; elseif (n <= 1.5e+49) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -3.15e-24) tmp = 1.0; elseif (n <= 6.4e-12) tmp = -1.0; elseif (n <= 6.6e+17) tmp = 1.0; elseif (n <= 1.5e+49) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -3.15e-24], 1.0, If[LessEqual[n, 6.4e-12], -1.0, If[LessEqual[n, 6.6e+17], 1.0, If[LessEqual[n, 1.5e+49], -1.0, 1.0]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -3.15 \cdot 10^{-24}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 6.4 \cdot 10^{-12}:\\
\;\;\;\;-1\\
\mathbf{elif}\;n \leq 6.6 \cdot 10^{+17}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 1.5 \cdot 10^{+49}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if n < -3.1499999999999999e-24 or 6.4000000000000002e-12 < n < 6.6e17 or 1.5000000000000001e49 < n Initial program 100.0%
sub-neg100.0%
remove-double-neg100.0%
distribute-neg-in100.0%
neg-mul-1100.0%
associate-/r*100.0%
neg-mul-1100.0%
*-commutative100.0%
associate-/l*100.0%
metadata-eval100.0%
/-rgt-identity100.0%
+-commutative100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in f around 0 78.6%
if -3.1499999999999999e-24 < n < 6.4000000000000002e-12 or 6.6e17 < n < 1.5000000000000001e49Initial program 100.0%
sub-neg100.0%
remove-double-neg100.0%
distribute-neg-in100.0%
neg-mul-1100.0%
associate-/r*100.0%
neg-mul-1100.0%
*-commutative100.0%
associate-/l*100.0%
metadata-eval100.0%
/-rgt-identity100.0%
+-commutative100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in f around inf 80.6%
Final simplification79.6%
(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%
sub-neg100.0%
remove-double-neg100.0%
distribute-neg-in100.0%
neg-mul-1100.0%
associate-/r*100.0%
neg-mul-1100.0%
*-commutative100.0%
associate-/l*100.0%
metadata-eval100.0%
/-rgt-identity100.0%
+-commutative100.0%
sub-neg100.0%
Simplified100.0%
Taylor expanded in f around inf 50.5%
Final simplification50.5%
herbie shell --seed 2023271
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))