
(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 (- y x)) (fabs y)))
double code(double x, double y) {
return fabs((y - x)) / fabs(y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = abs((y - x)) / abs(y)
end function
public static double code(double x, double y) {
return Math.abs((y - x)) / Math.abs(y);
}
def code(x, y): return math.fabs((y - x)) / math.fabs(y)
function code(x, y) return Float64(abs(Float64(y - x)) / abs(y)) end
function tmp = code(x, y) tmp = abs((y - x)) / abs(y); end
code[x_, y_] := N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / N[Abs[y], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left|y - x\right|}{\left|y\right|}
\end{array}
Initial program 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (or (<= x -1.08e+287) (not (<= x -4.5e+256))) (- 1.0 (/ x y)) (+ (/ x y) -1.0)))
double code(double x, double y) {
double tmp;
if ((x <= -1.08e+287) || !(x <= -4.5e+256)) {
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 <= (-1.08d+287)) .or. (.not. (x <= (-4.5d+256)))) 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 <= -1.08e+287) || !(x <= -4.5e+256)) {
tmp = 1.0 - (x / y);
} else {
tmp = (x / y) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -1.08e+287) or not (x <= -4.5e+256): tmp = 1.0 - (x / y) else: tmp = (x / y) + -1.0 return tmp
function code(x, y) tmp = 0.0 if ((x <= -1.08e+287) || !(x <= -4.5e+256)) 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 <= -1.08e+287) || ~((x <= -4.5e+256))) tmp = 1.0 - (x / y); else tmp = (x / y) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -1.08e+287], N[Not[LessEqual[x, -4.5e+256]], $MachinePrecision]], 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 -1.08 \cdot 10^{+287} \lor \neg \left(x \leq -4.5 \cdot 10^{+256}\right):\\
\;\;\;\;1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y} + -1\\
\end{array}
\end{array}
if x < -1.07999999999999993e287 or -4.4999999999999998e256 < 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-sqrt78.3%
fabs-sqr78.3%
rem-square-sqrt78.7%
div-sub78.7%
*-inverses78.7%
Simplified78.7%
if -1.07999999999999993e287 < x < -4.4999999999999998e256Initial 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-sqrt21.9%
fabs-sqr21.9%
rem-square-sqrt22.5%
rem-square-sqrt22.3%
fabs-sqr22.3%
rem-square-sqrt22.5%
fabs-sub22.5%
unpow122.5%
sqr-pow0.0%
fabs-sqr0.0%
sqr-pow77.9%
unpow177.9%
div-sub77.9%
*-inverses77.9%
sub-neg77.9%
metadata-eval77.9%
+-commutative77.9%
Simplified77.9%
Final simplification78.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-sqrt76.4%
fabs-sqr76.4%
rem-square-sqrt76.7%
rem-square-sqrt38.4%
fabs-sqr38.4%
rem-square-sqrt50.1%
fabs-sub50.1%
unpow150.1%
sqr-pow11.3%
fabs-sqr11.3%
sqr-pow24.3%
unpow124.3%
div-sub24.3%
*-inverses24.3%
sub-neg24.3%
metadata-eval24.3%
+-commutative24.3%
Simplified24.3%
Final simplification24.3%
herbie shell --seed 2023310
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))