
(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 (<= y -2800000000000.0)
1.0
(if (or (<= y -6.2e-53) (and (not (<= y -2e-70)) (<= y 9.5e-45)))
(fabs (/ x y))
1.0)))
double code(double x, double y) {
double tmp;
if (y <= -2800000000000.0) {
tmp = 1.0;
} else if ((y <= -6.2e-53) || (!(y <= -2e-70) && (y <= 9.5e-45))) {
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 (y <= (-2800000000000.0d0)) then
tmp = 1.0d0
else if ((y <= (-6.2d-53)) .or. (.not. (y <= (-2d-70))) .and. (y <= 9.5d-45)) 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 (y <= -2800000000000.0) {
tmp = 1.0;
} else if ((y <= -6.2e-53) || (!(y <= -2e-70) && (y <= 9.5e-45))) {
tmp = Math.abs((x / y));
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -2800000000000.0: tmp = 1.0 elif (y <= -6.2e-53) or (not (y <= -2e-70) and (y <= 9.5e-45)): tmp = math.fabs((x / y)) else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -2800000000000.0) tmp = 1.0; elseif ((y <= -6.2e-53) || (!(y <= -2e-70) && (y <= 9.5e-45))) tmp = abs(Float64(x / y)); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -2800000000000.0) tmp = 1.0; elseif ((y <= -6.2e-53) || (~((y <= -2e-70)) && (y <= 9.5e-45))) tmp = abs((x / y)); else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -2800000000000.0], 1.0, If[Or[LessEqual[y, -6.2e-53], And[N[Not[LessEqual[y, -2e-70]], $MachinePrecision], LessEqual[y, 9.5e-45]]], N[Abs[N[(x / y), $MachinePrecision]], $MachinePrecision], 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2800000000000:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq -6.2 \cdot 10^{-53} \lor \neg \left(y \leq -2 \cdot 10^{-70}\right) \land y \leq 9.5 \cdot 10^{-45}:\\
\;\;\;\;\left|\frac{x}{y}\right|\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if y < -2.8e12 or -6.20000000000000031e-53 < y < -1.99999999999999999e-70 or 9.5000000000000002e-45 < 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 81.4%
if -2.8e12 < y < -6.20000000000000031e-53 or -1.99999999999999999e-70 < y < 9.5000000000000002e-45Initial 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 inf 85.8%
associate-*r/85.8%
neg-mul-185.8%
Simplified85.8%
Final simplification83.3%
(FPCore (x y) :precision binary64 (if (<= y -4.8e-145) 1.0 (if (<= y 2.5e-61) (/ x y) 1.0)))
double code(double x, double y) {
double tmp;
if (y <= -4.8e-145) {
tmp = 1.0;
} else if (y <= 2.5e-61) {
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 (y <= (-4.8d-145)) then
tmp = 1.0d0
else if (y <= 2.5d-61) 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 (y <= -4.8e-145) {
tmp = 1.0;
} else if (y <= 2.5e-61) {
tmp = x / y;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -4.8e-145: tmp = 1.0 elif y <= 2.5e-61: tmp = x / y else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -4.8e-145) tmp = 1.0; elseif (y <= 2.5e-61) tmp = Float64(x / y); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -4.8e-145) tmp = 1.0; elseif (y <= 2.5e-61) tmp = x / y; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -4.8e-145], 1.0, If[LessEqual[y, 2.5e-61], N[(x / y), $MachinePrecision], 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -4.8 \cdot 10^{-145}:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq 2.5 \cdot 10^{-61}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if y < -4.8000000000000003e-145 or 2.4999999999999999e-61 < 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 72.8%
if -4.8000000000000003e-145 < y < 2.4999999999999999e-61Initial program 100.0%
div-inv99.8%
add-sqr-sqrt41.0%
fabs-sqr41.0%
add-sqr-sqrt41.4%
*-commutative41.4%
add-sqr-sqrt21.2%
fabs-sqr21.2%
add-sqr-sqrt44.9%
Applied egg-rr44.9%
Taylor expanded in y around 0 45.2%
Final simplification63.6%
(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%
div-inv99.7%
add-sqr-sqrt46.1%
fabs-sqr46.1%
add-sqr-sqrt46.9%
*-commutative46.9%
add-sqr-sqrt9.8%
fabs-sqr9.8%
add-sqr-sqrt24.3%
Applied egg-rr24.3%
Taylor expanded in y around 0 25.2%
Final simplification25.2%
(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%
div-inv99.7%
add-sqr-sqrt46.1%
fabs-sqr46.1%
add-sqr-sqrt46.9%
*-commutative46.9%
add-sqr-sqrt9.8%
fabs-sqr9.8%
add-sqr-sqrt24.3%
Applied egg-rr24.3%
Taylor expanded in y around inf 1.3%
Final simplification1.3%
herbie shell --seed 2024078
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))