
(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)
use fmin_fmax_functions
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]
\frac{-\left(f + n\right)}{f - n}
Herbie found 6 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)
use fmin_fmax_functions
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]
\frac{-\left(f + n\right)}{f - n}
(FPCore (f n) :precision binary64 (/ (+ n f) (- n f)))
double code(double f, double n) {
return (n + f) / (n - f);
}
real(8) function code(f, n)
use fmin_fmax_functions
real(8), intent (in) :: f
real(8), intent (in) :: n
code = (n + f) / (n - f)
end function
public static double code(double f, double n) {
return (n + f) / (n - f);
}
def code(f, n): return (n + f) / (n - f)
function code(f, n) return Float64(Float64(n + f) / Float64(n - f)) end
function tmp = code(f, n) tmp = (n + f) / (n - f); end
code[f_, n_] := N[(N[(n + f), $MachinePrecision] / N[(n - f), $MachinePrecision]), $MachinePrecision]
\frac{n + f}{n - f}
Initial program 100.0%
lift-/.f64N/A
lift-neg.f64N/A
distribute-frac-negN/A
distribute-neg-frac2N/A
lower-/.f64N/A
lift-+.f64N/A
+-commutativeN/A
lower-+.f64N/A
lift--.f64N/A
sub-negate-revN/A
lower--.f64100.0%
Applied rewrites100.0%
(FPCore (f n) :precision binary64 (if (<= (/ (- (+ f n)) (- f n)) -0.5) (fma (/ n f) -2.0 -1.0) (fma (/ f n) 2.0 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((f / n), 2.0, 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(f / n), 2.0, 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[(f / n), $MachinePrecision] * 2.0 + 1.0), $MachinePrecision]]
\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{f}{n}, 2, 1\right)\\
\end{array}
if (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) < -0.5Initial program 100.0%
Taylor expanded in n around 0
lower--.f64N/A
lower-*.f64N/A
lower-/.f6450.9%
Applied rewrites50.9%
lift--.f64N/A
sub-flipN/A
lift-*.f64N/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f6450.9%
Applied rewrites50.9%
if -0.5 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 100.0%
Taylor expanded in f around inf
Applied rewrites49.6%
Taylor expanded in f around 0
lower-+.f64N/A
lower-*.f64N/A
lower-/.f6451.1%
Applied rewrites51.1%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6451.1%
Applied rewrites51.1%
(FPCore (f n) :precision binary64 (if (<= (/ (- (+ f n)) (- f n)) -0.5) -1.0 (fma (/ f n) 2.0 1.0)))
double code(double f, double n) {
double tmp;
if ((-(f + n) / (f - n)) <= -0.5) {
tmp = -1.0;
} else {
tmp = fma((f / n), 2.0, 1.0);
}
return tmp;
}
function code(f, n) tmp = 0.0 if (Float64(Float64(-Float64(f + n)) / Float64(f - n)) <= -0.5) tmp = -1.0; else tmp = fma(Float64(f / n), 2.0, 1.0); end return tmp end
code[f_, n_] := If[LessEqual[N[((-N[(f + n), $MachinePrecision]) / N[(f - n), $MachinePrecision]), $MachinePrecision], -0.5], -1.0, N[(N[(f / n), $MachinePrecision] * 2.0 + 1.0), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\frac{-\left(f + n\right)}{f - n} \leq -0.5:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{f}{n}, 2, 1\right)\\
\end{array}
if (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) < -0.5Initial program 100.0%
Taylor expanded in f around inf
Applied rewrites49.6%
if -0.5 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 100.0%
Taylor expanded in f around inf
Applied rewrites49.6%
Taylor expanded in f around 0
lower-+.f64N/A
lower-*.f64N/A
lower-/.f6451.1%
Applied rewrites51.1%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6451.1%
Applied rewrites51.1%
(FPCore (f n) :precision binary64 (if (<= (/ (- (+ f n)) (- f n)) -0.5) -1.0 (/ n (- n f))))
double code(double f, double n) {
double tmp;
if ((-(f + n) / (f - n)) <= -0.5) {
tmp = -1.0;
} else {
tmp = n / (n - f);
}
return tmp;
}
real(8) function code(f, n)
use fmin_fmax_functions
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((-(f + n) / (f - n)) <= (-0.5d0)) then
tmp = -1.0d0
else
tmp = n / (n - f)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if ((-(f + n) / (f - n)) <= -0.5) {
tmp = -1.0;
} else {
tmp = n / (n - f);
}
return tmp;
}
def code(f, n): tmp = 0 if (-(f + n) / (f - n)) <= -0.5: tmp = -1.0 else: tmp = n / (n - f) return tmp
function code(f, n) tmp = 0.0 if (Float64(Float64(-Float64(f + n)) / Float64(f - n)) <= -0.5) tmp = -1.0; else tmp = Float64(n / Float64(n - f)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if ((-(f + n) / (f - n)) <= -0.5) tmp = -1.0; else tmp = n / (n - f); end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[N[((-N[(f + n), $MachinePrecision]) / N[(f - n), $MachinePrecision]), $MachinePrecision], -0.5], -1.0, N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\frac{-\left(f + n\right)}{f - n} \leq -0.5:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;\frac{n}{n - f}\\
\end{array}
if (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) < -0.5Initial program 100.0%
Taylor expanded in f around inf
Applied rewrites49.6%
if -0.5 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 100.0%
Taylor expanded in f around 0
Applied rewrites50.9%
lift-/.f64N/A
lift-neg.f64N/A
lift--.f64N/A
sub-negate-revN/A
lift--.f64N/A
frac-2neg-revN/A
lower-/.f6450.9%
Applied rewrites50.9%
(FPCore (f n) :precision binary64 (if (<= (/ (- (+ f n)) (- f n)) -1.7e-228) -1.0 1.0))
double code(double f, double n) {
double tmp;
if ((-(f + n) / (f - n)) <= -1.7e-228) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
real(8) function code(f, n)
use fmin_fmax_functions
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if ((-(f + n) / (f - n)) <= (-1.7d-228)) 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)) <= -1.7e-228) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if (-(f + n) / (f - n)) <= -1.7e-228: 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)) <= -1.7e-228) 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)) <= -1.7e-228) 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], -1.7e-228], -1.0, 1.0]
\begin{array}{l}
\mathbf{if}\;\frac{-\left(f + n\right)}{f - n} \leq -1.7 \cdot 10^{-228}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
if (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) < -1.7e-228Initial program 100.0%
Taylor expanded in f around inf
Applied rewrites49.6%
if -1.7e-228 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 100.0%
Taylor expanded in f around 0
Applied rewrites49.8%
(FPCore (f n) :precision binary64 -1.0)
double code(double f, double n) {
return -1.0;
}
real(8) function code(f, n)
use fmin_fmax_functions
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
-1
Initial program 100.0%
Taylor expanded in f around inf
Applied rewrites49.6%
herbie shell --seed 2025326
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))