
(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 9 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 (+ (/ n (- n f)) (/ f (- n f))))
double code(double f, double n) {
return (n / (n - f)) + (f / (n - f));
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
code = (n / (n - f)) + (f / (n - f))
end function
public static double code(double f, double n) {
return (n / (n - f)) + (f / (n - f));
}
def code(f, n): return (n / (n - f)) + (f / (n - f))
function code(f, n) return Float64(Float64(n / Float64(n - f)) + Float64(f / Float64(n - f))) end
function tmp = code(f, n) tmp = (n / (n - f)) + (f / (n - f)); end
code[f_, n_] := N[(N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision] + N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{n}{n - f} + \frac{f}{n - f}
\end{array}
Initial program 100.0%
lift-/.f64N/A
lift-neg.f64N/A
lift-+.f64N/A
distribute-neg-inN/A
+-commutativeN/A
sub-negN/A
div-subN/A
lower--.f64N/A
lower-/.f64N/A
lower-neg.f64N/A
lower-/.f64100.0
Applied rewrites100.0%
lift-/.f64N/A
frac-2negN/A
lift-neg.f64N/A
remove-double-negN/A
lower-/.f64N/A
lift--.f64N/A
sub-negN/A
lift-neg.f64N/A
distribute-neg-inN/A
lift-neg.f64N/A
lift-neg.f64N/A
remove-double-negN/A
+-commutativeN/A
lift-neg.f64N/A
sub-negN/A
lift--.f64100.0
Applied rewrites100.0%
Final simplification100.0%
(FPCore (f n) :precision binary64 (if (<= (/ (+ n f) (- n f)) -0.5) (fma n (/ -2.0 f) -1.0) (+ 1.0 (/ (+ f f) n))))
double code(double f, double n) {
double tmp;
if (((n + f) / (n - f)) <= -0.5) {
tmp = fma(n, (-2.0 / f), -1.0);
} else {
tmp = 1.0 + ((f + f) / n);
}
return tmp;
}
function code(f, n) tmp = 0.0 if (Float64(Float64(n + f) / Float64(n - f)) <= -0.5) tmp = fma(n, Float64(-2.0 / f), -1.0); else tmp = Float64(1.0 + Float64(Float64(f + f) / n)); end return tmp end
code[f_, n_] := If[LessEqual[N[(N[(n + f), $MachinePrecision] / N[(n - f), $MachinePrecision]), $MachinePrecision], -0.5], N[(n * N[(-2.0 / f), $MachinePrecision] + -1.0), $MachinePrecision], N[(1.0 + N[(N[(f + f), $MachinePrecision] / n), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{n + f}{n - f} \leq -0.5:\\
\;\;\;\;\mathsf{fma}\left(n, \frac{-2}{f}, -1\right)\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{f + f}{n}\\
\end{array}
\end{array}
if (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) < -0.5Initial program 100.0%
Taylor expanded in f around inf
sub-negN/A
mul-1-negN/A
distribute-neg-frac2N/A
cancel-sign-sub-invN/A
metadata-evalN/A
distribute-rgt1-inN/A
metadata-evalN/A
mul-1-negN/A
associate-*l/N/A
mul-1-negN/A
distribute-neg-frac2N/A
metadata-evalN/A
associate-*r/N/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
distribute-neg-fracN/A
metadata-evalN/A
lower-/.f6499.1
Applied rewrites99.1%
if -0.5 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 100.0%
Taylor expanded in f around 0
associate-*r/N/A
count-2N/A
remove-double-negN/A
distribute-neg-outN/A
mul-1-negN/A
sub-negN/A
distribute-neg-fracN/A
mul-1-negN/A
lower-+.f64N/A
associate-*r/N/A
mul-1-negN/A
sub-negN/A
mul-1-negN/A
distribute-neg-outN/A
remove-double-negN/A
lower-/.f64N/A
lower-+.f6498.8
Applied rewrites98.8%
Final simplification98.9%
herbie shell --seed 2024228
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))