
(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 3 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)) (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}
Initial program 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= x 5.4e+172) (- 1.0 (/ x y)) (+ (/ x y) -1.0)))
double code(double x, double y) {
double tmp;
if (x <= 5.4e+172) {
tmp = 1.0 - (x / y);
} 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 <= 5.4d+172) then
tmp = 1.0d0 - (x / y)
else
tmp = (x / y) + (-1.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 5.4e+172) {
tmp = 1.0 - (x / y);
} else {
tmp = (x / y) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 5.4e+172: tmp = 1.0 - (x / y) else: tmp = (x / y) + -1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= 5.4e+172) tmp = Float64(1.0 - Float64(x / y)); else tmp = Float64(Float64(x / y) + -1.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 5.4e+172) tmp = 1.0 - (x / y); else tmp = (x / y) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 5.4e+172], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 5.4 \cdot 10^{+172}:\\
\;\;\;\;1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y} + -1\\
\end{array}
\end{array}
if x < 5.4e172Initial program 100.0%
fabs-sub100.0%
Simplified100.0%
Taylor expanded in y around -inf 100.0%
fabs-neg100.0%
neg-mul-1100.0%
sub-neg100.0%
fabs-sub100.0%
fabs-div100.0%
rem-square-sqrt74.5%
fabs-sqr74.5%
rem-square-sqrt74.8%
div-sub74.8%
*-inverses74.8%
Simplified74.8%
if 5.4e172 < x Initial program 100.0%
fabs-sub100.0%
Simplified100.0%
Taylor expanded in y around -inf 100.0%
fabs-neg100.0%
neg-mul-1100.0%
sub-neg100.0%
fabs-sub100.0%
fabs-div100.0%
rem-square-sqrt34.2%
fabs-sqr34.2%
rem-square-sqrt34.5%
rem-square-sqrt5.7%
fabs-sqr5.7%
rem-square-sqrt71.6%
fabs-sub71.6%
unpow171.6%
sqr-pow65.5%
fabs-sqr65.5%
sqr-pow66.0%
unpow166.0%
div-sub66.0%
*-inverses66.0%
sub-neg66.0%
metadata-eval66.0%
+-commutative66.0%
Simplified66.0%
Final simplification73.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%
fabs-sub100.0%
Simplified100.0%
Taylor expanded in y around -inf 100.0%
fabs-neg100.0%
neg-mul-1100.0%
sub-neg100.0%
fabs-sub100.0%
fabs-div100.0%
rem-square-sqrt69.0%
fabs-sqr69.0%
rem-square-sqrt69.3%
rem-square-sqrt32.7%
fabs-sqr32.7%
rem-square-sqrt50.1%
fabs-sub50.1%
unpow150.1%
sqr-pow17.1%
fabs-sqr17.1%
sqr-pow31.7%
unpow131.7%
div-sub31.7%
*-inverses31.7%
sub-neg31.7%
metadata-eval31.7%
+-commutative31.7%
Simplified31.7%
Final simplification31.7%
herbie shell --seed 2023318
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))