
(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 7 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 (+ (/ f (- n f)) (/ -1.0 (+ -1.0 (/ f n)))))
double code(double f, double n) {
return (f / (n - f)) + (-1.0 / (-1.0 + (f / n)));
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
code = (f / (n - f)) + ((-1.0d0) / ((-1.0d0) + (f / n)))
end function
public static double code(double f, double n) {
return (f / (n - f)) + (-1.0 / (-1.0 + (f / n)));
}
def code(f, n): return (f / (n - f)) + (-1.0 / (-1.0 + (f / n)))
function code(f, n) return Float64(Float64(f / Float64(n - f)) + Float64(-1.0 / Float64(-1.0 + Float64(f / n)))) end
function tmp = code(f, n) tmp = (f / (n - f)) + (-1.0 / (-1.0 + (f / n))); end
code[f_, n_] := N[(N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / N[(-1.0 + N[(f / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{f}{n - f} + \frac{-1}{-1 + \frac{f}{n}}
\end{array}
Initial program 99.9%
flip--N/A
associate-/r/N/A
distribute-lft-inN/A
accelerator-lowering-fma.f64N/A
clear-numN/A
distribute-neg-frac2N/A
flip--N/A
/-lowering-/.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
--lowering--.f64N/A
*-lowering-*.f64N/A
Applied egg-rr99.7%
associate-/r/N/A
/-lowering-/.f64N/A
div-subN/A
*-inversesN/A
--lowering--.f64N/A
/-lowering-/.f6499.9%
Applied egg-rr99.9%
+-lowering-+.f64N/A
*-commutativeN/A
un-div-invN/A
/-lowering-/.f64N/A
--lowering--.f64N/A
frac-2negN/A
metadata-evalN/A
/-lowering-/.f64N/A
sub-negN/A
distribute-neg-inN/A
metadata-evalN/A
distribute-neg-frac2N/A
distribute-frac-negN/A
frac-2negN/A
+-lowering-+.f64N/A
/-lowering-/.f64100.0%
Applied egg-rr100.0%
(FPCore (f n) :precision binary64 (let* ((t_0 (/ n (- n f)))) (if (<= n -1.35e-32) t_0 (if (<= n 1.8e-29) (/ f (- n f)) t_0))))
double code(double f, double n) {
double t_0 = n / (n - f);
double tmp;
if (n <= -1.35e-32) {
tmp = t_0;
} else if (n <= 1.8e-29) {
tmp = f / (n - f);
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(f, n)
real(8), intent (in) :: f
real(8), intent (in) :: n
real(8) :: t_0
real(8) :: tmp
t_0 = n / (n - f)
if (n <= (-1.35d-32)) then
tmp = t_0
else if (n <= 1.8d-29) then
tmp = f / (n - f)
else
tmp = t_0
end if
code = tmp
end function
public static double code(double f, double n) {
double t_0 = n / (n - f);
double tmp;
if (n <= -1.35e-32) {
tmp = t_0;
} else if (n <= 1.8e-29) {
tmp = f / (n - f);
} else {
tmp = t_0;
}
return tmp;
}
def code(f, n): t_0 = n / (n - f) tmp = 0 if n <= -1.35e-32: tmp = t_0 elif n <= 1.8e-29: tmp = f / (n - f) else: tmp = t_0 return tmp
function code(f, n) t_0 = Float64(n / Float64(n - f)) tmp = 0.0 if (n <= -1.35e-32) tmp = t_0; elseif (n <= 1.8e-29) tmp = Float64(f / Float64(n - f)); else tmp = t_0; end return tmp end
function tmp_2 = code(f, n) t_0 = n / (n - f); tmp = 0.0; if (n <= -1.35e-32) tmp = t_0; elseif (n <= 1.8e-29) tmp = f / (n - f); else tmp = t_0; end tmp_2 = tmp; end
code[f_, n_] := Block[{t$95$0 = N[(n / N[(n - f), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[n, -1.35e-32], t$95$0, If[LessEqual[n, 1.8e-29], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{n}{n - f}\\
\mathbf{if}\;n \leq -1.35 \cdot 10^{-32}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;n \leq 1.8 \cdot 10^{-29}:\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if n < -1.3499999999999999e-32 or 1.79999999999999987e-29 < 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 f around 0
Simplified81.1%
if -1.3499999999999999e-32 < n < 1.79999999999999987e-29Initial program 99.9%
Taylor expanded in f around inf
Simplified79.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--.f6479.4%
Applied egg-rr79.4%
(FPCore (f n) :precision binary64 (if (<= n -2.3e-32) 1.0 (if (<= n 9.5e-27) (/ f (- n f)) 1.0)))
double code(double f, double n) {
double tmp;
if (n <= -2.3e-32) {
tmp = 1.0;
} else if (n <= 9.5e-27) {
tmp = f / (n - f);
} 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 <= (-2.3d-32)) then
tmp = 1.0d0
else if (n <= 9.5d-27) then
tmp = f / (n - f)
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if (n <= -2.3e-32) {
tmp = 1.0;
} else if (n <= 9.5e-27) {
tmp = f / (n - f);
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -2.3e-32: tmp = 1.0 elif n <= 9.5e-27: tmp = f / (n - f) else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (n <= -2.3e-32) tmp = 1.0; elseif (n <= 9.5e-27) tmp = Float64(f / Float64(n - f)); else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -2.3e-32) tmp = 1.0; elseif (n <= 9.5e-27) tmp = f / (n - f); else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -2.3e-32], 1.0, If[LessEqual[n, 9.5e-27], N[(f / N[(n - f), $MachinePrecision]), $MachinePrecision], 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -2.3 \cdot 10^{-32}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 9.5 \cdot 10^{-27}:\\
\;\;\;\;\frac{f}{n - f}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if n < -2.3000000000000001e-32 or 9.50000000000000037e-27 < n Initial program 99.9%
Taylor expanded in f around 0
Simplified80.7%
if -2.3000000000000001e-32 < n < 9.50000000000000037e-27Initial program 99.9%
Taylor expanded in f around inf
Simplified79.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--.f6479.4%
Applied egg-rr79.4%
(FPCore (f n) :precision binary64 (if (<= n -8.2e-55) 1.0 (if (<= n 4.7e-27) (- -1.0 (/ n f)) 1.0)))
double code(double f, double n) {
double tmp;
if (n <= -8.2e-55) {
tmp = 1.0;
} else if (n <= 4.7e-27) {
tmp = -1.0 - (n / f);
} 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 <= (-8.2d-55)) then
tmp = 1.0d0
else if (n <= 4.7d-27) then
tmp = (-1.0d0) - (n / f)
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double f, double n) {
double tmp;
if (n <= -8.2e-55) {
tmp = 1.0;
} else if (n <= 4.7e-27) {
tmp = -1.0 - (n / f);
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -8.2e-55: tmp = 1.0 elif n <= 4.7e-27: tmp = -1.0 - (n / f) else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (n <= -8.2e-55) tmp = 1.0; elseif (n <= 4.7e-27) tmp = Float64(-1.0 - Float64(n / f)); else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -8.2e-55) tmp = 1.0; elseif (n <= 4.7e-27) tmp = -1.0 - (n / f); else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -8.2e-55], 1.0, If[LessEqual[n, 4.7e-27], N[(-1.0 - N[(n / f), $MachinePrecision]), $MachinePrecision], 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -8.2 \cdot 10^{-55}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 4.7 \cdot 10^{-27}:\\
\;\;\;\;-1 - \frac{n}{f}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if n < -8.1999999999999996e-55 or 4.70000000000000032e-27 < n Initial program 100.0%
Taylor expanded in f around 0
Simplified79.5%
if -8.1999999999999996e-55 < n < 4.70000000000000032e-27Initial program 99.9%
Taylor expanded in f around inf
Simplified80.8%
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-/.f6480.7%
Simplified80.7%
(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 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%
(FPCore (f n) :precision binary64 (if (<= n -3e-32) 1.0 (if (<= n 2e-30) -1.0 1.0)))
double code(double f, double n) {
double tmp;
if (n <= -3e-32) {
tmp = 1.0;
} else if (n <= 2e-30) {
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 <= (-3d-32)) then
tmp = 1.0d0
else if (n <= 2d-30) 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 <= -3e-32) {
tmp = 1.0;
} else if (n <= 2e-30) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(f, n): tmp = 0 if n <= -3e-32: tmp = 1.0 elif n <= 2e-30: tmp = -1.0 else: tmp = 1.0 return tmp
function code(f, n) tmp = 0.0 if (n <= -3e-32) tmp = 1.0; elseif (n <= 2e-30) tmp = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(f, n) tmp = 0.0; if (n <= -3e-32) tmp = 1.0; elseif (n <= 2e-30) tmp = -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[f_, n_] := If[LessEqual[n, -3e-32], 1.0, If[LessEqual[n, 2e-30], -1.0, 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;n \leq -3 \cdot 10^{-32}:\\
\;\;\;\;1\\
\mathbf{elif}\;n \leq 2 \cdot 10^{-30}:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if n < -3e-32 or 2e-30 < n Initial program 99.9%
Taylor expanded in f around 0
Simplified80.7%
if -3e-32 < n < 2e-30Initial program 99.9%
Taylor expanded in f around inf
Simplified78.6%
(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
Simplified46.0%
herbie shell --seed 2024193
(FPCore (f n)
:name "subtraction fraction"
:precision binary64
(/ (- (+ f n)) (- f n)))