
(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 5 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 (- 1.0 (/ x y))))
double code(double x, double y) {
return fabs((1.0 - (x / y)));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = abs((1.0d0 - (x / y)))
end function
public static double code(double x, double y) {
return Math.abs((1.0 - (x / y)));
}
def code(x, y): return math.fabs((1.0 - (x / y)))
function code(x, y) return abs(Float64(1.0 - Float64(x / y))) end
function tmp = code(x, y) tmp = abs((1.0 - (x / y))); end
code[x_, y_] := N[Abs[N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\left|1 - \frac{x}{y}\right|
\end{array}
Initial program 100.0%
Taylor expanded in x around -inf 100.0%
fabs-neg100.0%
mul-1-neg100.0%
sub-neg100.0%
fabs-div100.0%
div-sub100.0%
*-inverses100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(if (or (<= x -1.9e-115)
(not (or (<= x 2.4e-85) (and (not (<= x 1.95e+27)) (<= x 4.6e+79)))))
(fabs (/ x y))
1.0))
double code(double x, double y) {
double tmp;
if ((x <= -1.9e-115) || !((x <= 2.4e-85) || (!(x <= 1.95e+27) && (x <= 4.6e+79)))) {
tmp = fabs((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 <= (-1.9d-115)) .or. (.not. (x <= 2.4d-85) .or. (.not. (x <= 1.95d+27)) .and. (x <= 4.6d+79))) then
tmp = abs((x / y))
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -1.9e-115) || !((x <= 2.4e-85) || (!(x <= 1.95e+27) && (x <= 4.6e+79)))) {
tmp = Math.abs((x / y));
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -1.9e-115) or not ((x <= 2.4e-85) or (not (x <= 1.95e+27) and (x <= 4.6e+79))): tmp = math.fabs((x / y)) else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if ((x <= -1.9e-115) || !((x <= 2.4e-85) || (!(x <= 1.95e+27) && (x <= 4.6e+79)))) tmp = abs(Float64(x / y)); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -1.9e-115) || ~(((x <= 2.4e-85) || (~((x <= 1.95e+27)) && (x <= 4.6e+79))))) tmp = abs((x / y)); else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -1.9e-115], N[Not[Or[LessEqual[x, 2.4e-85], And[N[Not[LessEqual[x, 1.95e+27]], $MachinePrecision], LessEqual[x, 4.6e+79]]]], $MachinePrecision]], N[Abs[N[(x / y), $MachinePrecision]], $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.9 \cdot 10^{-115} \lor \neg \left(x \leq 2.4 \cdot 10^{-85} \lor \neg \left(x \leq 1.95 \cdot 10^{+27}\right) \land x \leq 4.6 \cdot 10^{+79}\right):\\
\;\;\;\;\left|\frac{x}{y}\right|\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -1.89999999999999996e-115 or 2.4000000000000001e-85 < x < 1.9499999999999999e27 or 4.6000000000000001e79 < x Initial program 100.0%
add-cbrt-cube59.3%
pow1/357.7%
pow357.7%
clear-num57.7%
inv-pow57.7%
pow-pow57.7%
add-sqr-sqrt22.2%
fabs-sqr22.2%
add-sqr-sqrt34.4%
add-sqr-sqrt12.7%
fabs-sqr12.7%
add-sqr-sqrt33.5%
metadata-eval33.5%
Applied egg-rr33.5%
unpow1/324.2%
Simplified24.2%
Taylor expanded in y around 0 23.8%
add-sqr-sqrt23.6%
sqrt-unprod41.9%
pow1/241.9%
pow1/333.5%
pow1/333.3%
pow-prod-up33.3%
pow-pow51.8%
metadata-eval51.8%
metadata-eval51.8%
Applied egg-rr51.8%
unpow1/251.8%
metadata-eval51.8%
pow-sqr51.8%
unpow-151.8%
associate-/r/51.9%
associate-*l/51.9%
*-lft-identity51.9%
unpow-151.9%
associate-/r/51.9%
associate-*l/51.9%
*-lft-identity51.9%
rem-sqrt-square82.7%
Simplified82.7%
if -1.89999999999999996e-115 < x < 2.4000000000000001e-85 or 1.9499999999999999e27 < x < 4.6000000000000001e79Initial program 100.0%
Taylor expanded in x around -inf 100.0%
fabs-neg100.0%
mul-1-neg100.0%
sub-neg100.0%
fabs-div100.0%
div-sub100.0%
*-inverses100.0%
Simplified100.0%
Taylor expanded in x around 0 83.6%
Final simplification83.0%
(FPCore (x y) :precision binary64 (if (<= y -1.45e-89) 1.0 (if (<= y 1.1e-59) (+ (+ 1.0 (/ x y)) -1.0) 1.0)))
double code(double x, double y) {
double tmp;
if (y <= -1.45e-89) {
tmp = 1.0;
} else if (y <= 1.1e-59) {
tmp = (1.0 + (x / y)) + -1.0;
} 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 (y <= (-1.45d-89)) then
tmp = 1.0d0
else if (y <= 1.1d-59) then
tmp = (1.0d0 + (x / y)) + (-1.0d0)
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -1.45e-89) {
tmp = 1.0;
} else if (y <= 1.1e-59) {
tmp = (1.0 + (x / y)) + -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -1.45e-89: tmp = 1.0 elif y <= 1.1e-59: tmp = (1.0 + (x / y)) + -1.0 else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -1.45e-89) tmp = 1.0; elseif (y <= 1.1e-59) tmp = Float64(Float64(1.0 + Float64(x / y)) + -1.0); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -1.45e-89) tmp = 1.0; elseif (y <= 1.1e-59) tmp = (1.0 + (x / y)) + -1.0; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -1.45e-89], 1.0, If[LessEqual[y, 1.1e-59], N[(N[(1.0 + N[(x / y), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.45 \cdot 10^{-89}:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq 1.1 \cdot 10^{-59}:\\
\;\;\;\;\left(1 + \frac{x}{y}\right) + -1\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if y < -1.44999999999999996e-89 or 1.0999999999999999e-59 < y Initial program 100.0%
Taylor expanded in x around -inf 100.0%
fabs-neg100.0%
mul-1-neg100.0%
sub-neg100.0%
fabs-div100.0%
div-sub100.0%
*-inverses100.0%
Simplified100.0%
Taylor expanded in x around 0 63.4%
if -1.44999999999999996e-89 < y < 1.0999999999999999e-59Initial program 100.0%
add-cbrt-cube60.4%
pow1/358.9%
pow358.9%
clear-num58.9%
inv-pow58.9%
pow-pow58.9%
add-sqr-sqrt23.9%
fabs-sqr23.9%
add-sqr-sqrt43.5%
add-sqr-sqrt15.9%
fabs-sqr15.9%
add-sqr-sqrt38.9%
metadata-eval38.9%
Applied egg-rr38.9%
unpow1/323.0%
Simplified23.0%
Taylor expanded in y around 0 23.1%
expm1-log1p-u22.4%
expm1-undefine22.4%
log1p-undefine22.4%
rem-exp-log23.1%
pow1/339.0%
pow-pow50.8%
metadata-eval50.8%
inv-pow50.8%
clear-num51.0%
Applied egg-rr51.0%
Final simplification58.3%
(FPCore (x y) :precision binary64 (/ x y))
double code(double x, double y) {
return x / y;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x / y
end function
public static double code(double x, double y) {
return x / y;
}
def code(x, y): return x / y
function code(x, y) return Float64(x / y) end
function tmp = code(x, y) tmp = x / y; end
code[x_, y_] := N[(x / y), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{y}
\end{array}
Initial program 100.0%
add-cbrt-cube71.9%
pow1/370.6%
pow370.6%
clear-num70.6%
inv-pow70.6%
pow-pow70.6%
add-sqr-sqrt30.0%
fabs-sqr30.0%
add-sqr-sqrt38.5%
add-sqr-sqrt9.0%
fabs-sqr9.0%
add-sqr-sqrt22.8%
metadata-eval22.8%
Applied egg-rr22.8%
unpow1/316.9%
Simplified16.9%
Taylor expanded in y around 0 33.1%
Final simplification33.1%
(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%
add-cbrt-cube71.9%
pow1/370.6%
pow370.6%
clear-num70.6%
inv-pow70.6%
pow-pow70.6%
add-sqr-sqrt30.0%
fabs-sqr30.0%
add-sqr-sqrt38.5%
add-sqr-sqrt9.0%
fabs-sqr9.0%
add-sqr-sqrt22.8%
metadata-eval22.8%
Applied egg-rr22.8%
unpow1/316.9%
Simplified16.9%
Taylor expanded in y around inf 1.2%
Final simplification1.2%
herbie shell --seed 2024055
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))