
(FPCore (x y) :precision binary64 (/ (fabs (- x y)) (fabs y)))
double code(double x, double y) {
return fabs((x - y)) / fabs(y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = abs((x - y)) / abs(y)
end function
public static double code(double x, double y) {
return Math.abs((x - y)) / Math.abs(y);
}
def code(x, y): return math.fabs((x - y)) / math.fabs(y)
function code(x, y) return Float64(abs(Float64(x - y)) / abs(y)) end
function tmp = code(x, y) tmp = abs((x - y)) / abs(y); end
code[x_, y_] := N[(N[Abs[N[(x - y), $MachinePrecision]], $MachinePrecision] / N[Abs[y], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left|x - y\right|}{\left|y\right|}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 4 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (/ (fabs (- x y)) (fabs y)))
double code(double x, double y) {
return fabs((x - y)) / fabs(y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = abs((x - y)) / abs(y)
end function
public static double code(double x, double y) {
return Math.abs((x - y)) / Math.abs(y);
}
def code(x, y): return math.fabs((x - y)) / math.fabs(y)
function code(x, y) return Float64(abs(Float64(x - y)) / abs(y)) end
function tmp = code(x, y) tmp = abs((x - y)) / abs(y); end
code[x_, y_] := N[(N[Abs[N[(x - y), $MachinePrecision]], $MachinePrecision] / N[Abs[y], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left|x - y\right|}{\left|y\right|}
\end{array}
(FPCore (x y) :precision binary64 (fabs (+ (/ x y) -1.0)))
double code(double x, double y) {
return fabs(((x / y) + -1.0));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = abs(((x / y) + (-1.0d0)))
end function
public static double code(double x, double y) {
return Math.abs(((x / y) + -1.0));
}
def code(x, y): return math.fabs(((x / y) + -1.0))
function code(x, y) return abs(Float64(Float64(x / y) + -1.0)) end
function tmp = code(x, y) tmp = abs(((x / y) + -1.0)); end
code[x_, y_] := N[Abs[N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\left|\frac{x}{y} + -1\right|
\end{array}
Initial program 100.0%
Taylor expanded in x around -inf 100.0%
Simplified100.0%
(FPCore (x y) :precision binary64 (if (or (<= x -6e-50) (not (<= x 1.52e-109))) (fabs (/ x y)) (- 1.0 x)))
double code(double x, double y) {
double tmp;
if ((x <= -6e-50) || !(x <= 1.52e-109)) {
tmp = fabs((x / y));
} else {
tmp = 1.0 - x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((x <= (-6d-50)) .or. (.not. (x <= 1.52d-109))) then
tmp = abs((x / y))
else
tmp = 1.0d0 - x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -6e-50) || !(x <= 1.52e-109)) {
tmp = Math.abs((x / y));
} else {
tmp = 1.0 - x;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -6e-50) or not (x <= 1.52e-109): tmp = math.fabs((x / y)) else: tmp = 1.0 - x return tmp
function code(x, y) tmp = 0.0 if ((x <= -6e-50) || !(x <= 1.52e-109)) tmp = abs(Float64(x / y)); else tmp = Float64(1.0 - x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -6e-50) || ~((x <= 1.52e-109))) tmp = abs((x / y)); else tmp = 1.0 - x; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -6e-50], N[Not[LessEqual[x, 1.52e-109]], $MachinePrecision]], N[Abs[N[(x / y), $MachinePrecision]], $MachinePrecision], N[(1.0 - x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6 \cdot 10^{-50} \lor \neg \left(x \leq 1.52 \cdot 10^{-109}\right):\\
\;\;\;\;\left|\frac{x}{y}\right|\\
\mathbf{else}:\\
\;\;\;\;1 - x\\
\end{array}
\end{array}
if x < -5.99999999999999981e-50 or 1.5199999999999999e-109 < x Initial program 100.0%
Taylor expanded in x around -inf 100.0%
Simplified100.0%
Taylor expanded in x around inf 75.2%
if -5.99999999999999981e-50 < x < 1.5199999999999999e-109Initial program 100.0%
add-sqr-sqrt49.5%
fabs-sqr49.5%
add-sqr-sqrt2.3%
fabs-sqr2.3%
add-sqr-sqrt3.0%
add-sqr-sqrt7.3%
div-sub7.3%
Applied egg-rr7.3%
Taylor expanded in y around 0 7.3%
Applied egg-rr88.8%
+-commutative88.8%
distribute-lft-in88.8%
metadata-eval88.8%
neg-mul-188.8%
sub-neg88.8%
Simplified88.8%
Final simplification79.8%
(FPCore (x y) :precision binary64 (if (or (<= x -3.7e+103) (not (<= x 1.5e+196))) (/ x y) 1.0))
double code(double x, double y) {
double tmp;
if ((x <= -3.7e+103) || !(x <= 1.5e+196)) {
tmp = x / y;
} else {
tmp = 1.0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((x <= (-3.7d+103)) .or. (.not. (x <= 1.5d+196))) then
tmp = x / y
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -3.7e+103) || !(x <= 1.5e+196)) {
tmp = x / y;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -3.7e+103) or not (x <= 1.5e+196): tmp = x / y else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if ((x <= -3.7e+103) || !(x <= 1.5e+196)) tmp = Float64(x / y); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -3.7e+103) || ~((x <= 1.5e+196))) tmp = x / y; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -3.7e+103], N[Not[LessEqual[x, 1.5e+196]], $MachinePrecision]], N[(x / y), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.7 \cdot 10^{+103} \lor \neg \left(x \leq 1.5 \cdot 10^{+196}\right):\\
\;\;\;\;\frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -3.70000000000000033e103 or 1.4999999999999999e196 < x Initial program 100.0%
div-inv99.7%
add-sqr-sqrt35.2%
fabs-sqr35.2%
add-sqr-sqrt35.5%
*-commutative35.5%
add-sqr-sqrt16.2%
fabs-sqr16.2%
add-sqr-sqrt53.0%
Applied egg-rr53.0%
Taylor expanded in x around inf 53.2%
Taylor expanded in y around 0 53.4%
if -3.70000000000000033e103 < x < 1.4999999999999999e196Initial program 100.0%
Taylor expanded in x around -inf 100.0%
Simplified100.0%
Taylor expanded in x around inf 40.6%
Applied egg-rr30.2%
*-inverses60.9%
Simplified60.9%
Final simplification58.9%
(FPCore (x y) :precision binary64 1.0)
double code(double x, double y) {
return 1.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0
end function
public static double code(double x, double y) {
return 1.0;
}
def code(x, y): return 1.0
function code(x, y) return 1.0 end
function tmp = code(x, y) tmp = 1.0; end
code[x_, y_] := 1.0
\begin{array}{l}
\\
1
\end{array}
Initial program 100.0%
Taylor expanded in x around -inf 100.0%
Simplified100.0%
Taylor expanded in x around inf 54.5%
Applied egg-rr22.5%
*-inverses47.5%
Simplified47.5%
herbie shell --seed 2024146
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))