
(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 (/ (- n f) (+ n f))))
double code(double f, double n) {
return 1.0 / ((n - f) / (n + f));
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
code = 1.0d0 / ((n - f) / (n + f))
end function
public static double code(double f, double n) {
return 1.0 / ((n - f) / (n + f));
}
def code(f, n): return 1.0 / ((n - f) / (n + f))
function code(f, n) return Float64(1.0 / Float64(Float64(n - f) / Float64(n + f))) end
function tmp = code(f, n) tmp = 1.0 / ((n - f) / (n + f)); end
code[f_, n_] := N[(1.0 / N[(N[(n - f), $MachinePrecision] / N[(n + f), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{\frac{n - f}{n + f}}
\end{array}
Initial program 100.0%
clear-numN/A
/-lowering-/.f64N/A
frac-2negN/A
remove-double-negN/A
/-lowering-/.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
--lowering--.f64N/A
+-lowering-+.f64100.0
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (f n) :precision binary64 (if (<= (/ (+ n f) (- n f)) -0.5) (/ f (- n f)) (+ 1.0 (/ (+ f f) n))))
double code(double f, double n) {
double tmp;
if (((n + f) / (n - f)) <= -0.5) {
tmp = f / (n - f);
} else {
tmp = 1.0 + ((f + f) / n);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if (((n + f) / (n - f)) <= (-0.5d0)) then
tmp = f / (n - f)
else
tmp = 1.0d0 + ((f + f) / n)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if (((n + f) / (n - f)) <= -0.5) {
tmp = f / (n - f);
} else {
tmp = 1.0 + ((f + f) / n);
}
return tmp;
}
def code(f, n): tmp = 0 if ((n + f) / (n - f)) <= -0.5: tmp = f / (n - f) 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 = Float64(f / Float64(n - f)); else tmp = Float64(1.0 + Float64(Float64(f + f) / n)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (((n + f) / (n - f)) <= -0.5) tmp = f / (n - f); else tmp = 1.0 + ((f + f) / n); end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[N[(N[(n + f), $MachinePrecision] / N[(n - f), $MachinePrecision]), $MachinePrecision], -0.5], N[(f / N[(n - f), $MachinePrecision]), $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:\\
\;\;\;\;\frac{f}{n - f}\\
\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
mul-1-negN/A
neg-lowering-neg.f6497.4
Simplified97.4%
sub-negN/A
+-commutativeN/A
remove-double-negN/A
distribute-neg-inN/A
sub-negN/A
distribute-frac-neg2N/A
distribute-frac-negN/A
remove-double-negN/A
/-lowering-/.f64N/A
--lowering--.f6497.4
Applied egg-rr97.4%
if -0.5 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 99.9%
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
+-lowering-+.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
/-lowering-/.f64N/A
+-lowering-+.f6498.0
Simplified98.0%
Final simplification97.7%
(FPCore (f n) :precision binary64 (if (<= (/ (+ n f) (- n f)) -0.5) (/ f (- n f)) (+ 1.0 (/ f n))))
double code(double f, double n) {
double tmp;
if (((n + f) / (n - f)) <= -0.5) {
tmp = f / (n - f);
} else {
tmp = 1.0 + (f / n);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if (((n + f) / (n - f)) <= (-0.5d0)) then
tmp = f / (n - f)
else
tmp = 1.0d0 + (f / n)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if (((n + f) / (n - f)) <= -0.5) {
tmp = f / (n - f);
} else {
tmp = 1.0 + (f / n);
}
return tmp;
}
def code(f, n): tmp = 0 if ((n + f) / (n - f)) <= -0.5: tmp = f / (n - f) else: tmp = 1.0 + (f / n) return tmp
function code(f, n) tmp = 0.0 if (Float64(Float64(n + f) / Float64(n - f)) <= -0.5) tmp = Float64(f / Float64(n - f)); else tmp = Float64(1.0 + Float64(f / n)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (((n + f) / (n - f)) <= -0.5) tmp = f / (n - f); else tmp = 1.0 + (f / n); end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[N[(N[(n + f), $MachinePrecision] / N[(n - f), $MachinePrecision]), $MachinePrecision], -0.5], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{n + f}{n - f} \leq -0.5:\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{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
mul-1-negN/A
neg-lowering-neg.f6497.4
Simplified97.4%
sub-negN/A
+-commutativeN/A
remove-double-negN/A
distribute-neg-inN/A
sub-negN/A
distribute-frac-neg2N/A
distribute-frac-negN/A
remove-double-negN/A
/-lowering-/.f64N/A
--lowering--.f6497.4
Applied egg-rr97.4%
if -0.5 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 99.9%
frac-2negN/A
remove-double-negN/A
/-lowering-/.f64N/A
+-lowering-+.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
--lowering--.f6499.9
Applied egg-rr99.9%
Taylor expanded in n around inf
Simplified96.6%
Taylor expanded in f around 0
+-lowering-+.f64N/A
/-lowering-/.f6496.6
Simplified96.6%
Final simplification97.0%
(FPCore (f n) :precision binary64 (if (<= (/ (+ n f) (- n f)) -0.5) (- -1.0 (/ n f)) (+ 1.0 (/ f n))))
double code(double f, double n) {
double tmp;
if (((n + f) / (n - f)) <= -0.5) {
tmp = -1.0 - (n / f);
} else {
tmp = 1.0 + (f / n);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if (((n + f) / (n - f)) <= (-0.5d0)) then
tmp = (-1.0d0) - (n / f)
else
tmp = 1.0d0 + (f / n)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if (((n + f) / (n - f)) <= -0.5) {
tmp = -1.0 - (n / f);
} else {
tmp = 1.0 + (f / n);
}
return tmp;
}
def code(f, n): tmp = 0 if ((n + f) / (n - f)) <= -0.5: tmp = -1.0 - (n / f) else: tmp = 1.0 + (f / n) return tmp
function code(f, n) tmp = 0.0 if (Float64(Float64(n + f) / Float64(n - f)) <= -0.5) tmp = Float64(-1.0 - Float64(n / f)); else tmp = Float64(1.0 + Float64(f / n)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (((n + f) / (n - f)) <= -0.5) tmp = -1.0 - (n / f); else tmp = 1.0 + (f / n); end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[N[(N[(n + f), $MachinePrecision] / N[(n - f), $MachinePrecision]), $MachinePrecision], -0.5], N[(-1.0 - N[(n / f), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{n + f}{n - f} \leq -0.5:\\
\;\;\;\;-1 - \frac{n}{f}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{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
mul-1-negN/A
neg-lowering-neg.f6497.4
Simplified97.4%
Taylor expanded in f around inf
sub-negN/A
metadata-evalN/A
+-commutativeN/A
mul-1-negN/A
sub-negN/A
--lowering--.f64N/A
/-lowering-/.f6497.4
Simplified97.4%
if -0.5 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 99.9%
frac-2negN/A
remove-double-negN/A
/-lowering-/.f64N/A
+-lowering-+.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
--lowering--.f6499.9
Applied egg-rr99.9%
Taylor expanded in n around inf
Simplified96.6%
Taylor expanded in f around 0
+-lowering-+.f64N/A
/-lowering-/.f6496.6
Simplified96.6%
Final simplification97.0%
(FPCore (f n) :precision binary64 (if (<= (/ (+ n f) (- n f)) -0.5) -1.0 (+ 1.0 (/ f n))))
double code(double f, double n) {
double tmp;
if (((n + f) / (n - f)) <= -0.5) {
tmp = -1.0;
} else {
tmp = 1.0 + (f / n);
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: tmp
if (((n + f) / (n - f)) <= (-0.5d0)) then
tmp = -1.0d0
else
tmp = 1.0d0 + (f / n)
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if (((n + f) / (n - f)) <= -0.5) {
tmp = -1.0;
} else {
tmp = 1.0 + (f / n);
}
return tmp;
}
def code(f, n): tmp = 0 if ((n + f) / (n - f)) <= -0.5: tmp = -1.0 else: tmp = 1.0 + (f / n) return tmp
function code(f, n) tmp = 0.0 if (Float64(Float64(n + f) / Float64(n - f)) <= -0.5) tmp = -1.0; else tmp = Float64(1.0 + Float64(f / n)); end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (((n + f) / (n - f)) <= -0.5) tmp = -1.0; else tmp = 1.0 + (f / n); end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[N[(N[(n + f), $MachinePrecision] / N[(n - f), $MachinePrecision]), $MachinePrecision], -0.5], -1.0, N[(1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{n + f}{n - f} \leq -0.5:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{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
Simplified97.3%
if -0.5 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 99.9%
frac-2negN/A
remove-double-negN/A
/-lowering-/.f64N/A
+-lowering-+.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
--lowering--.f6499.9
Applied egg-rr99.9%
Taylor expanded in n around inf
Simplified96.6%
Taylor expanded in f around 0
+-lowering-+.f64N/A
/-lowering-/.f6496.6
Simplified96.6%
Final simplification97.0%
(FPCore (f n) :precision binary64 (if (<= (/ (+ n f) (- n f)) -5e-310) -1.0 1.0))
double code(double f, double n) {
double tmp;
if (((n + f) / (n - f)) <= -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 (((n + f) / (n - f)) <= (-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 (((n + f) / (n - f)) <= -5e-310) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if ((n + f) / (n - f)) <= -5e-310: tmp = -1.0 else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (Float64(Float64(n + f) / Float64(n - f)) <= -5e-310) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (((n + f) / (n - f)) <= -5e-310) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[N[(N[(n + f), $MachinePrecision] / N[(n - f), $MachinePrecision]), $MachinePrecision], -5e-310], -1.0, 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{n + f}{n - f} \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 100.0%
Taylor expanded in f around inf
Simplified97.3%
if -4.999999999999985e-310 < (/.f64 (neg.f64 (+.f64 f n)) (-.f64 f n)) Initial program 99.9%
Taylor expanded in f around 0
Simplified96.5%
Final simplification96.9%
(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)
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]
\begin{array}{l}
\\
\frac{n + f}{n - f}
\end{array}
Initial program 100.0%
frac-2negN/A
remove-double-negN/A
/-lowering-/.f64N/A
+-lowering-+.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
--lowering--.f64100.0
Applied egg-rr100.0%
Final simplification100.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%
Taylor expanded in f around inf
Simplified52.4%
herbie shell --seed 2024199
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))