
(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 8 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 (/ -1.0 (/ (- f n) (+ f n))))
double code(double f, double n) {
return -1.0 / ((f - n) / (f + n));
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
code = (-1.0d0) / ((f - n) / (f + n))
end function
public static double code(double f, double n) {
return -1.0 / ((f - n) / (f + n));
}
def code(f, n): return -1.0 / ((f - n) / (f + n))
function code(f, n) return Float64(-1.0 / Float64(Float64(f - n) / Float64(f + n))) end
function tmp = code(f, n) tmp = -1.0 / ((f - n) / (f + n)); end
code[f_, n_] := N[(-1.0 / N[(N[(f - n), $MachinePrecision] / N[(f + n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{\frac{f - n}{f + n}}
\end{array}
Initial program 99.9%
lift--.f64N/A
flip--N/A
difference-of-squaresN/A
lift-+.f64N/A
lift--.f64N/A
lift-+.f64N/A
associate-/l*N/A
lower-*.f64N/A
lift-+.f64N/A
+-commutativeN/A
lower-+.f64N/A
lower-/.f6499.9
lift-+.f64N/A
+-commutativeN/A
lower-+.f6499.9
Applied rewrites99.9%
lift-/.f64N/A
lift-*.f64N/A
associate-/r*N/A
frac-2negN/A
frac-2negN/A
distribute-frac-negN/A
lift-neg.f64N/A
lift-+.f64N/A
+-commutativeN/A
lift-+.f64N/A
remove-double-negN/A
*-inversesN/A
metadata-evalN/A
lift-/.f64N/A
distribute-frac-negN/A
distribute-frac-neg2N/A
frac-2negN/A
lift-/.f64N/A
Applied rewrites99.9%
(FPCore (f n) :precision binary64 (if (<= (/ (- (+ f n)) (- f n)) -0.5) (fma (/ n f) -2.0 -1.0) (fma (/ 2.0 n) f 1.0)))
double code(double f, double n) {
double tmp;
if ((-(f + n) / (f - n)) <= -0.5) {
tmp = fma((n / f), -2.0, -1.0);
} else {
tmp = fma((2.0 / n), f, 1.0);
}
return tmp;
}
function code(f, n) tmp = 0.0 if (Float64(Float64(-Float64(f + n)) / Float64(f - n)) <= -0.5) tmp = fma(Float64(n / f), -2.0, -1.0); else tmp = fma(Float64(2.0 / n), f, 1.0); end return tmp end
code[f_, n_] := If[LessEqual[N[((-N[(f + n), $MachinePrecision]) / N[(f - n), $MachinePrecision]), $MachinePrecision], -0.5], N[(N[(n / f), $MachinePrecision] * -2.0 + -1.0), $MachinePrecision], N[(N[(2.0 / n), $MachinePrecision] * f + 1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{-\left(f + n\right)}{f - n} \leq -0.5:\\
\;\;\;\;\mathsf{fma}\left(\frac{n}{f}, -2, -1\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{2}{n}, f, 1\right)\\
\end{array}
\end{array}
if (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) < -0.5Initial program 99.9%
Taylor expanded in f around 0
Applied rewrites1.6%
Taylor expanded in f around -inf
+-commutativeN/A
associate--r+N/A
*-lft-identityN/A
fp-cancel-sub-sign-invN/A
metadata-evalN/A
mul-1-negN/A
mul-1-negN/A
distribute-neg-inN/A
count-2-revN/A
*-commutativeN/A
associate-*l/N/A
associate-*r/N/A
metadata-evalN/A
associate-*r/N/A
distribute-lft-neg-inN/A
mul-1-negN/A
rgt-mult-inverseN/A
fp-cancel-sub-signN/A
mul-1-negN/A
Applied rewrites97.0%
if -0.5 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 100.0%
Taylor expanded in f around 0
+-commutativeN/A
*-lft-identityN/A
associate-*l/N/A
associate-*l*N/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6499.7
Applied rewrites99.7%
(FPCore (f n) :precision binary64 (if (<= (/ (- (+ f n)) (- f n)) -0.5) (/ (- f) (- f n)) (fma (/ 2.0 n) f 1.0)))
double code(double f, double n) {
double tmp;
if ((-(f + n) / (f - n)) <= -0.5) {
tmp = -f / (f - n);
} else {
tmp = fma((2.0 / n), f, 1.0);
}
return tmp;
}
function code(f, n) tmp = 0.0 if (Float64(Float64(-Float64(f + n)) / Float64(f - n)) <= -0.5) tmp = Float64(Float64(-f) / Float64(f - n)); else tmp = fma(Float64(2.0 / n), f, 1.0); end return tmp end
code[f_, n_] := If[LessEqual[N[((-N[(f + n), $MachinePrecision]) / N[(f - n), $MachinePrecision]), $MachinePrecision], -0.5], N[((-f) / N[(f - n), $MachinePrecision]), $MachinePrecision], N[(N[(2.0 / n), $MachinePrecision] * f + 1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{-\left(f + n\right)}{f - n} \leq -0.5:\\
\;\;\;\;\frac{-f}{f - n}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{2}{n}, f, 1\right)\\
\end{array}
\end{array}
if (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) < -0.5Initial program 99.9%
Taylor expanded in f around inf
mul-1-negN/A
lower-neg.f6495.5
Applied rewrites95.5%
if -0.5 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 100.0%
Taylor expanded in f around 0
+-commutativeN/A
*-lft-identityN/A
associate-*l/N/A
associate-*l*N/A
lower-fma.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f6499.7
Applied rewrites99.7%
(FPCore (f n) :precision binary64 (if (<= (/ (- (+ f n)) (- f n)) -0.5) (/ (- f) (- f n)) (/ (- n) (- f n))))
double code(double f, double n) {
double tmp;
if ((-(f + n) / (f - n)) <= -0.5) {
tmp = -f / (f - n);
} else {
tmp = -n / (f - n);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((-(f + n) / (f - n)) <= (-0.5d0)) then
tmp = -f / (f - n)
else
tmp = -n / (f - n)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((-(f + n) / (f - n)) <= -0.5) {
tmp = -f / (f - n);
} else {
tmp = -n / (f - n);
}
return tmp;
}
def code(f, n): tmp = 0 if (-(f + n) / (f - n)) <= -0.5: tmp = -f / (f - n) else: tmp = -n / (f - n) return tmp
function code(f, n) tmp = 0.0 if (Float64(Float64(-Float64(f + n)) / Float64(f - n)) <= -0.5) tmp = Float64(Float64(-f) / Float64(f - n)); else tmp = Float64(Float64(-n) / Float64(f - n)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((-(f + n) / (f - n)) <= -0.5) tmp = -f / (f - n); else tmp = -n / (f - n); end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[N[((-N[(f + n), $MachinePrecision]) / N[(f - n), $MachinePrecision]), $MachinePrecision], -0.5], N[((-f) / N[(f - n), $MachinePrecision]), $MachinePrecision], N[((-n) / N[(f - n), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{-\left(f + n\right)}{f - n} \leq -0.5:\\
\;\;\;\;\frac{-f}{f - n}\\
\mathbf{else}:\\
\;\;\;\;\frac{-n}{f - n}\\
\end{array}
\end{array}
if (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) < -0.5Initial program 99.9%
Taylor expanded in f around inf
mul-1-negN/A
lower-neg.f6495.5
Applied rewrites95.5%
if -0.5 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 100.0%
Taylor expanded in f around 0
mul-1-negN/A
lower-neg.f6499.2
Applied rewrites99.2%
(FPCore (f n) :precision binary64 (if (<= (/ (- (+ f n)) (- f n)) -0.5) (/ (- f) (- f n)) 1.0))
double code(double f, double n) {
double tmp;
if ((-(f + n) / (f - n)) <= -0.5) {
tmp = -f / (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 ((-(f + n) / (f - n)) <= (-0.5d0)) then
tmp = -f / (f - n)
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((-(f + n) / (f - n)) <= -0.5) {
tmp = -f / (f - n);
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (-(f + n) / (f - n)) <= -0.5: tmp = -f / (f - n) else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (Float64(Float64(-Float64(f + n)) / Float64(f - n)) <= -0.5) tmp = Float64(Float64(-f) / Float64(f - n)); else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((-(f + n) / (f - n)) <= -0.5) tmp = -f / (f - n); else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[N[((-N[(f + n), $MachinePrecision]) / N[(f - n), $MachinePrecision]), $MachinePrecision], -0.5], N[((-f) / N[(f - n), $MachinePrecision]), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{-\left(f + n\right)}{f - n} \leq -0.5:\\
\;\;\;\;\frac{-f}{f - n}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) < -0.5Initial program 99.9%
Taylor expanded in f around inf
mul-1-negN/A
lower-neg.f6495.5
Applied rewrites95.5%
if -0.5 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 100.0%
Taylor expanded in f around 0
Applied rewrites99.1%
(FPCore (f n) :precision binary64 (if (<= (/ (- (+ f n)) (- f n)) -5e-310) -1.0 1.0))
double code(double f, double n) {
double tmp;
if ((-(f + n) / (f - n)) <= -5e-310) {
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 ((-(f + n) / (f - n)) <= (-5d-310)) 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 ((-(f + n) / (f - n)) <= -5e-310) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (-(f + n) / (f - n)) <= -5e-310: tmp = -1.0 else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (Float64(Float64(-Float64(f + n)) / Float64(f - n)) <= -5e-310) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((-(f + n) / (f - n)) <= -5e-310) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[N[((-N[(f + n), $MachinePrecision]) / N[(f - n), $MachinePrecision]), $MachinePrecision], -5e-310], -1.0, 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{-\left(f + n\right)}{f - n} \leq -5 \cdot 10^{-310}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) < -4.999999999999985e-310Initial program 99.9%
Taylor expanded in f around inf
Applied rewrites95.3%
if -4.999999999999985e-310 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 100.0%
Taylor expanded in f around 0
Applied rewrites99.1%
(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}
Initial program 99.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%
Taylor expanded in f around inf
Applied rewrites48.8%
herbie shell --seed 2024337
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))