
(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 6 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 (/ (- y x) y)))
double code(double x, double y) {
return fabs(((y - x) / y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = abs(((y - x) / y))
end function
public static double code(double x, double y) {
return Math.abs(((y - x) / y));
}
def code(x, y): return math.fabs(((y - x) / y))
function code(x, y) return abs(Float64(Float64(y - x) / y)) end
function tmp = code(x, y) tmp = abs(((y - x) / y)); end
code[x_, y_] := N[Abs[N[(N[(y - x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\left|\frac{y - x}{y}\right|
\end{array}
Initial program 100.0%
fabs-sub100.0%
div-fabs100.0%
Applied egg-rr100.0%
(FPCore (x y) :precision binary64 (if (or (<= x -6e-69) (not (<= x 26000000000.0))) (fabs (/ x y)) 1.0))
double code(double x, double y) {
double tmp;
if ((x <= -6e-69) || !(x <= 26000000000.0)) {
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 <= (-6d-69)) .or. (.not. (x <= 26000000000.0d0))) 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 <= -6e-69) || !(x <= 26000000000.0)) {
tmp = Math.abs((x / y));
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -6e-69) or not (x <= 26000000000.0): tmp = math.fabs((x / y)) else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if ((x <= -6e-69) || !(x <= 26000000000.0)) tmp = abs(Float64(x / y)); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -6e-69) || ~((x <= 26000000000.0))) tmp = abs((x / y)); else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -6e-69], N[Not[LessEqual[x, 26000000000.0]], $MachinePrecision]], N[Abs[N[(x / y), $MachinePrecision]], $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6 \cdot 10^{-69} \lor \neg \left(x \leq 26000000000\right):\\
\;\;\;\;\left|\frac{x}{y}\right|\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -5.99999999999999978e-69 or 2.6e10 < x Initial program 100.0%
fabs-sub100.0%
div-fabs100.0%
Applied egg-rr100.0%
Taylor expanded in y around 0 75.6%
neg-mul-175.6%
distribute-neg-frac275.6%
Simplified75.6%
if -5.99999999999999978e-69 < x < 2.6e10Initial program 100.0%
fabs-sub100.0%
div-fabs100.0%
Applied egg-rr100.0%
Taylor expanded in y around inf 81.6%
Final simplification78.4%
(FPCore (x y) :precision binary64 (if (<= x -1e+124) (/ x y) (if (<= x 1.5e+96) 1.0 (+ (/ x y) -1.0))))
double code(double x, double y) {
double tmp;
if (x <= -1e+124) {
tmp = x / y;
} else if (x <= 1.5e+96) {
tmp = 1.0;
} else {
tmp = (x / y) + -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 <= (-1d+124)) then
tmp = x / y
else if (x <= 1.5d+96) then
tmp = 1.0d0
else
tmp = (x / y) + (-1.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1e+124) {
tmp = x / y;
} else if (x <= 1.5e+96) {
tmp = 1.0;
} else {
tmp = (x / y) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1e+124: tmp = x / y elif x <= 1.5e+96: tmp = 1.0 else: tmp = (x / y) + -1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= -1e+124) tmp = Float64(x / y); elseif (x <= 1.5e+96) tmp = 1.0; else tmp = Float64(Float64(x / y) + -1.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1e+124) tmp = x / y; elseif (x <= 1.5e+96) tmp = 1.0; else tmp = (x / y) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1e+124], N[(x / y), $MachinePrecision], If[LessEqual[x, 1.5e+96], 1.0, N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \cdot 10^{+124}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;x \leq 1.5 \cdot 10^{+96}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y} + -1\\
\end{array}
\end{array}
if x < -9.99999999999999948e123Initial program 100.0%
div-inv99.9%
add-sqr-sqrt5.6%
fabs-sqr5.6%
add-sqr-sqrt6.1%
*-commutative6.1%
add-sqr-sqrt0.2%
fabs-sqr0.2%
add-sqr-sqrt43.1%
Applied egg-rr43.1%
Taylor expanded in y around 0 43.7%
if -9.99999999999999948e123 < x < 1.5e96Initial program 100.0%
fabs-sub100.0%
div-fabs100.0%
Applied egg-rr100.0%
Taylor expanded in y around inf 68.5%
if 1.5e96 < x Initial program 100.0%
Taylor expanded in x around -inf 100.0%
fabs-neg100.0%
mul-1-neg100.0%
sub-neg100.0%
fabs-sub100.0%
fabs-div100.0%
rem-square-sqrt51.1%
fabs-sqr51.1%
rem-square-sqrt51.6%
div-sub51.6%
sub-neg51.6%
*-inverses51.6%
metadata-eval51.6%
+-commutative51.6%
Simplified51.6%
Final simplification62.6%
(FPCore (x y) :precision binary64 (+ (/ x y) -1.0))
double code(double x, double y) {
return (x / y) + -1.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x / y) + (-1.0d0)
end function
public static double code(double x, double y) {
return (x / y) + -1.0;
}
def code(x, y): return (x / y) + -1.0
function code(x, y) return Float64(Float64(x / y) + -1.0) end
function tmp = code(x, y) tmp = (x / y) + -1.0; end
code[x_, y_] := N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{y} + -1
\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-sub100.0%
fabs-div100.0%
rem-square-sqrt23.7%
fabs-sqr23.7%
rem-square-sqrt24.8%
div-sub24.8%
sub-neg24.8%
*-inverses24.8%
metadata-eval24.8%
+-commutative24.8%
Simplified24.8%
Final simplification24.8%
(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-sqrt54.2%
fabs-sqr54.2%
add-sqr-sqrt55.0%
*-commutative55.0%
add-sqr-sqrt14.0%
fabs-sqr14.0%
add-sqr-sqrt24.7%
Applied egg-rr24.7%
Taylor expanded in y around 0 24.7%
(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-sqrt54.2%
fabs-sqr54.2%
add-sqr-sqrt55.0%
*-commutative55.0%
add-sqr-sqrt14.0%
fabs-sqr14.0%
add-sqr-sqrt24.7%
Applied egg-rr24.7%
Taylor expanded in y around inf 1.3%
herbie shell --seed 2024086
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))