
(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 (log1p (expm1 (/ (+ f n) (- n f)))))
double code(double f, double n) {
return log1p(expm1(((f + n) / (n - f))));
}
public static double code(double f, double n) {
return Math.log1p(Math.expm1(((f + n) / (n - f))));
}
def code(f, n): return math.log1p(math.expm1(((f + n) / (n - f))))
function code(f, n) return log1p(expm1(Float64(Float64(f + n) / Float64(n - f)))) end
code[f_, n_] := N[Log[1 + N[(Exp[N[(N[(f + n), $MachinePrecision] / N[(n - f), $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{f + n}{n - f}\right)\right)
\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%
log1p-expm1-u100.0%
Applied egg-rr100.0%
(FPCore (f n)
:precision binary64
(if (<= n -4.5e+45)
1.0
(if (<= n 2.7e-12)
(+ (* -2.0 (/ n f)) -1.0)
(if (<= n 62000.0) 1.0 (if (<= n 200000000.0) -1.0 1.0)))))
double code(double f, double n) {
double tmp;
if (n <= -4.5e+45) {
tmp = 1.0;
} else if (n <= 2.7e-12) {
tmp = (-2.0 * (n / f)) + -1.0;
} else if (n <= 62000.0) {
tmp = 1.0;
} else if (n <= 200000000.0) {
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 <= (-4.5d+45)) then
tmp = 1.0d0
else if (n <= 2.7d-12) then
tmp = ((-2.0d0) * (n / f)) + (-1.0d0)
else if (n <= 62000.0d0) then
tmp = 1.0d0
else if (n <= 200000000.0d0) 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 <= -4.5e+45) {
tmp = 1.0;
} else if (n <= 2.7e-12) {
tmp = (-2.0 * (n / f)) + -1.0;
} else if (n <= 62000.0) {
tmp = 1.0;
} else if (n <= 200000000.0) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -4.5e+45: tmp = 1.0 elif n <= 2.7e-12: tmp = (-2.0 * (n / f)) + -1.0 elif n <= 62000.0: tmp = 1.0 elif n <= 200000000.0: tmp = -1.0 else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (n <= -4.5e+45) tmp = 1.0; elseif (n <= 2.7e-12) tmp = Float64(Float64(-2.0 * Float64(n / f)) + -1.0); elseif (n <= 62000.0) tmp = 1.0; elseif (n <= 200000000.0) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -4.5e+45) tmp = 1.0; elseif (n <= 2.7e-12) tmp = (-2.0 * (n / f)) + -1.0; elseif (n <= 62000.0) tmp = 1.0; elseif (n <= 200000000.0) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -4.5e+45], 1.0, If[LessEqual[n, 2.7e-12], N[(N[(-2.0 * N[(n / f), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], If[LessEqual[n, 62000.0], 1.0, If[LessEqual[n, 200000000.0], -1.0, 1.0]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -4.5 \cdot 10^{+45}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 2.7 \cdot 10^{-12}:\\
\;\;\;\;-2 \cdot \frac{n}{f} + -1\\
\mathbf{elif}\;n \leq 62000:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 200000000:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if n < -4.4999999999999998e45 or 2.6999999999999998e-12 < n < 62000 or 2e8 < 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 83.9%
if -4.4999999999999998e45 < n < 2.6999999999999998e-12Initial 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.7%
if 62000 < n < 2e8Initial 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 100.0%
Final simplification82.7%
(FPCore (f n)
:precision binary64
(if (<= n -2e+43)
1.0
(if (<= n 8e-13)
-1.0
(if (<= n 59000.0) 1.0 (if (<= n 64000000.0) -1.0 1.0)))))
double code(double f, double n) {
double tmp;
if (n <= -2e+43) {
tmp = 1.0;
} else if (n <= 8e-13) {
tmp = -1.0;
} else if (n <= 59000.0) {
tmp = 1.0;
} else if (n <= 64000000.0) {
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 <= (-2d+43)) then
tmp = 1.0d0
else if (n <= 8d-13) then
tmp = -1.0d0
else if (n <= 59000.0d0) then
tmp = 1.0d0
else if (n <= 64000000.0d0) 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 <= -2e+43) {
tmp = 1.0;
} else if (n <= 8e-13) {
tmp = -1.0;
} else if (n <= 59000.0) {
tmp = 1.0;
} else if (n <= 64000000.0) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -2e+43: tmp = 1.0 elif n <= 8e-13: tmp = -1.0 elif n <= 59000.0: tmp = 1.0 elif n <= 64000000.0: tmp = -1.0 else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (n <= -2e+43) tmp = 1.0; elseif (n <= 8e-13) tmp = -1.0; elseif (n <= 59000.0) tmp = 1.0; elseif (n <= 64000000.0) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -2e+43) tmp = 1.0; elseif (n <= 8e-13) tmp = -1.0; elseif (n <= 59000.0) tmp = 1.0; elseif (n <= 64000000.0) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -2e+43], 1.0, If[LessEqual[n, 8e-13], -1.0, If[LessEqual[n, 59000.0], 1.0, If[LessEqual[n, 64000000.0], -1.0, 1.0]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -2 \cdot 10^{+43}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 8 \cdot 10^{-13}:\\
\;\;\;\;-1\\
\mathbf{elif}\;n \leq 59000:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 64000000:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if n < -2.00000000000000003e43 or 8.0000000000000002e-13 < n < 59000 or 6.4e7 < 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 83.3%
if -2.00000000000000003e43 < n < 8.0000000000000002e-13 or 59000 < n < 6.4e7Initial 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 80.0%
(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 -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 47.2%
herbie shell --seed 2024107
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))