
(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 4 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) -1.0)))
double code(double x, double y) {
return fabs(((x / y) + -1.0));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = abs(((x / y) + (-1.0d0)))
end function
public static double code(double x, double y) {
return Math.abs(((x / y) + -1.0));
}
def code(x, y): return math.fabs(((x / y) + -1.0))
function code(x, y) return abs(Float64(Float64(x / y) + -1.0)) end
function tmp = code(x, y) tmp = abs(((x / y) + -1.0)); end
code[x_, y_] := N[Abs[N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\left|\frac{x}{y} + -1\right|
\end{array}
Initial program 100.0%
fabs-sub100.0%
Simplified100.0%
Taylor expanded in y around -inf 100.0%
mul-1-neg100.0%
sub-neg100.0%
fabs-neg100.0%
fabs-div100.0%
div-sub100.0%
*-inverses100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(if (<= y -0.0028)
1.0
(if (or (<= y 1e-111) (and (not (<= y 3.8e-81)) (<= y 1e-32)))
(/ (- x) y)
1.0)))
double code(double x, double y) {
double tmp;
if (y <= -0.0028) {
tmp = 1.0;
} else if ((y <= 1e-111) || (!(y <= 3.8e-81) && (y <= 1e-32))) {
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 <= (-0.0028d0)) then
tmp = 1.0d0
else if ((y <= 1d-111) .or. (.not. (y <= 3.8d-81)) .and. (y <= 1d-32)) 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 <= -0.0028) {
tmp = 1.0;
} else if ((y <= 1e-111) || (!(y <= 3.8e-81) && (y <= 1e-32))) {
tmp = -x / y;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -0.0028: tmp = 1.0 elif (y <= 1e-111) or (not (y <= 3.8e-81) and (y <= 1e-32)): tmp = -x / y else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -0.0028) tmp = 1.0; elseif ((y <= 1e-111) || (!(y <= 3.8e-81) && (y <= 1e-32))) tmp = Float64(Float64(-x) / y); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -0.0028) tmp = 1.0; elseif ((y <= 1e-111) || (~((y <= 3.8e-81)) && (y <= 1e-32))) tmp = -x / y; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -0.0028], 1.0, If[Or[LessEqual[y, 1e-111], And[N[Not[LessEqual[y, 3.8e-81]], $MachinePrecision], LessEqual[y, 1e-32]]], N[((-x) / y), $MachinePrecision], 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -0.0028:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq 10^{-111} \lor \neg \left(y \leq 3.8 \cdot 10^{-81}\right) \land y \leq 10^{-32}:\\
\;\;\;\;\frac{-x}{y}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if y < -0.00279999999999999997 or 1.00000000000000009e-111 < y < 3.7999999999999999e-81 or 1.00000000000000006e-32 < y Initial program 99.9%
fabs-sub99.9%
Simplified99.9%
div-inv99.7%
*-commutative99.7%
add-sqr-sqrt54.1%
fabs-sqr54.1%
add-sqr-sqrt55.0%
add-sqr-sqrt42.4%
fabs-sqr42.4%
add-sqr-sqrt80.4%
Applied egg-rr80.4%
Taylor expanded in y around inf 69.8%
if -0.00279999999999999997 < y < 1.00000000000000009e-111 or 3.7999999999999999e-81 < y < 1.00000000000000006e-32Initial program 100.0%
fabs-sub100.0%
Simplified100.0%
div-inv99.7%
*-commutative99.7%
add-sqr-sqrt51.0%
fabs-sqr51.0%
add-sqr-sqrt51.5%
add-sqr-sqrt32.6%
fabs-sqr32.6%
add-sqr-sqrt59.3%
Applied egg-rr59.3%
Taylor expanded in y around 0 46.0%
associate-*r/46.0%
neg-mul-146.0%
Simplified46.0%
Final simplification59.3%
(FPCore (x y) :precision binary64 (- 1.0 (/ x y)))
double code(double x, double y) {
return 1.0 - (x / y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0 - (x / y)
end function
public static double code(double x, double y) {
return 1.0 - (x / y);
}
def code(x, y): return 1.0 - (x / y)
function code(x, y) return Float64(1.0 - Float64(x / y)) end
function tmp = code(x, y) tmp = 1.0 - (x / y); end
code[x_, y_] := N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
1 - \frac{x}{y}
\end{array}
Initial program 100.0%
fabs-sub100.0%
Simplified100.0%
Taylor expanded in y around -inf 100.0%
mul-1-neg100.0%
sub-neg100.0%
fabs-neg100.0%
fabs-sub100.0%
fabs-div100.0%
rem-square-sqrt70.9%
fabs-sqr70.9%
rem-square-sqrt71.2%
div-sub71.3%
*-inverses71.3%
Simplified71.3%
Final simplification71.3%
(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%
fabs-sub100.0%
Simplified100.0%
div-inv99.7%
*-commutative99.7%
add-sqr-sqrt52.7%
fabs-sqr52.7%
add-sqr-sqrt53.5%
add-sqr-sqrt38.1%
fabs-sqr38.1%
add-sqr-sqrt71.0%
Applied egg-rr71.0%
Taylor expanded in y around inf 46.4%
Final simplification46.4%
herbie shell --seed 2023301
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))