
(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
(let* ((t_0 (* x (/ (/ x y) (- y x)))) (t_1 (/ y (+ x y))))
(if (<= x -9.5e+108)
t_0
(if (<= x 1.55e-8)
t_1
(if (<= x 25000000000000.0) t_0 (if (<= x 1.8e+107) t_1 (/ x y)))))))
double code(double x, double y) {
double t_0 = x * ((x / y) / (y - x));
double t_1 = y / (x + y);
double tmp;
if (x <= -9.5e+108) {
tmp = t_0;
} else if (x <= 1.55e-8) {
tmp = t_1;
} else if (x <= 25000000000000.0) {
tmp = t_0;
} else if (x <= 1.8e+107) {
tmp = t_1;
} else {
tmp = x / y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = x * ((x / y) / (y - x))
t_1 = y / (x + y)
if (x <= (-9.5d+108)) then
tmp = t_0
else if (x <= 1.55d-8) then
tmp = t_1
else if (x <= 25000000000000.0d0) then
tmp = t_0
else if (x <= 1.8d+107) then
tmp = t_1
else
tmp = x / y
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = x * ((x / y) / (y - x));
double t_1 = y / (x + y);
double tmp;
if (x <= -9.5e+108) {
tmp = t_0;
} else if (x <= 1.55e-8) {
tmp = t_1;
} else if (x <= 25000000000000.0) {
tmp = t_0;
} else if (x <= 1.8e+107) {
tmp = t_1;
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): t_0 = x * ((x / y) / (y - x)) t_1 = y / (x + y) tmp = 0 if x <= -9.5e+108: tmp = t_0 elif x <= 1.55e-8: tmp = t_1 elif x <= 25000000000000.0: tmp = t_0 elif x <= 1.8e+107: tmp = t_1 else: tmp = x / y return tmp
function code(x, y) t_0 = Float64(x * Float64(Float64(x / y) / Float64(y - x))) t_1 = Float64(y / Float64(x + y)) tmp = 0.0 if (x <= -9.5e+108) tmp = t_0; elseif (x <= 1.55e-8) tmp = t_1; elseif (x <= 25000000000000.0) tmp = t_0; elseif (x <= 1.8e+107) tmp = t_1; else tmp = Float64(x / y); end return tmp end
function tmp_2 = code(x, y) t_0 = x * ((x / y) / (y - x)); t_1 = y / (x + y); tmp = 0.0; if (x <= -9.5e+108) tmp = t_0; elseif (x <= 1.55e-8) tmp = t_1; elseif (x <= 25000000000000.0) tmp = t_0; elseif (x <= 1.8e+107) tmp = t_1; else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(x * N[(N[(x / y), $MachinePrecision] / N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -9.5e+108], t$95$0, If[LessEqual[x, 1.55e-8], t$95$1, If[LessEqual[x, 25000000000000.0], t$95$0, If[LessEqual[x, 1.8e+107], t$95$1, N[(x / y), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \frac{\frac{x}{y}}{y - x}\\
t_1 := \frac{y}{x + y}\\
\mathbf{if}\;x \leq -9.5 \cdot 10^{+108}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \leq 1.55 \cdot 10^{-8}:\\
\;\;\;\;t_1\\
\mathbf{elif}\;x \leq 25000000000000:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \leq 1.8 \cdot 10^{+107}:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < -9.50000000000000097e108 or 1.55e-8 < x < 2.5e13Initial program 100.0%
div-inv99.7%
add-sqr-sqrt19.5%
fabs-sqr19.5%
add-sqr-sqrt20.1%
*-commutative20.1%
add-sqr-sqrt1.9%
fabs-sqr1.9%
add-sqr-sqrt31.6%
Applied egg-rr31.6%
flip--18.9%
associate-*r/14.4%
+-commutative14.4%
Applied egg-rr14.4%
Taylor expanded in y around 0 15.0%
unpow215.0%
associate-/l*16.8%
associate-/r/16.8%
Simplified16.8%
associate-*l/15.0%
*-un-lft-identity15.0%
associate-*l/15.0%
frac-2neg15.0%
div-inv15.0%
associate-*l/15.0%
*-un-lft-identity15.0%
distribute-neg-frac15.0%
add-sqr-sqrt1.8%
sqrt-unprod5.4%
sqr-neg5.4%
sqrt-unprod8.3%
add-sqr-sqrt22.1%
frac-2neg22.1%
associate-*r/30.2%
distribute-neg-in30.2%
add-sqr-sqrt8.5%
sqrt-unprod25.5%
sqr-neg25.5%
sqrt-unprod21.8%
add-sqr-sqrt30.6%
sub-neg30.6%
Applied egg-rr30.6%
associate-*l*57.9%
associate-*r/58.0%
*-rgt-identity58.0%
Simplified58.0%
if -9.50000000000000097e108 < x < 1.55e-8 or 2.5e13 < x < 1.7999999999999999e107Initial program 100.0%
div-inv99.7%
add-sqr-sqrt52.2%
fabs-sqr52.2%
add-sqr-sqrt53.1%
*-commutative53.1%
add-sqr-sqrt9.0%
fabs-sqr9.0%
add-sqr-sqrt16.7%
Applied egg-rr16.7%
flip--14.5%
associate-*r/13.3%
+-commutative13.3%
Applied egg-rr13.3%
Taylor expanded in x around 0 1.7%
unpow21.7%
mul-1-neg1.7%
distribute-rgt-neg-out1.7%
Simplified1.7%
associate-*r*1.9%
lft-mult-inverse1.9%
*-un-lft-identity1.9%
neg-sub01.9%
metadata-eval1.9%
sub-neg1.9%
metadata-eval1.9%
add-sqr-sqrt1.0%
sqrt-unprod19.1%
sqr-neg19.1%
sqrt-unprod34.5%
add-sqr-sqrt69.2%
Applied egg-rr69.2%
+-lft-identity69.2%
Simplified69.2%
if 1.7999999999999999e107 < x Initial program 100.0%
div-inv99.7%
add-sqr-sqrt92.1%
fabs-sqr92.1%
add-sqr-sqrt92.5%
*-commutative92.5%
add-sqr-sqrt43.7%
fabs-sqr43.7%
add-sqr-sqrt44.3%
Applied egg-rr44.3%
Taylor expanded in y around 0 44.8%
Final simplification62.6%
(FPCore (x y) :precision binary64 (if (<= x -1.55e+113) (/ x y) (if (<= x 5.5e+105) (/ y (+ x y)) (/ x y))))
double code(double x, double y) {
double tmp;
if (x <= -1.55e+113) {
tmp = x / y;
} else if (x <= 5.5e+105) {
tmp = y / (x + y);
} else {
tmp = x / y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-1.55d+113)) then
tmp = x / y
else if (x <= 5.5d+105) then
tmp = y / (x + y)
else
tmp = x / y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1.55e+113) {
tmp = x / y;
} else if (x <= 5.5e+105) {
tmp = y / (x + y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1.55e+113: tmp = x / y elif x <= 5.5e+105: tmp = y / (x + y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (x <= -1.55e+113) tmp = Float64(x / y); elseif (x <= 5.5e+105) tmp = Float64(y / Float64(x + y)); else tmp = Float64(x / y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1.55e+113) tmp = x / y; elseif (x <= 5.5e+105) tmp = y / (x + y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1.55e+113], N[(x / y), $MachinePrecision], If[LessEqual[x, 5.5e+105], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.55 \cdot 10^{+113}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;x \leq 5.5 \cdot 10^{+105}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < -1.54999999999999996e113 or 5.49999999999999979e105 < x Initial program 100.0%
div-inv99.7%
add-sqr-sqrt46.5%
fabs-sqr46.5%
add-sqr-sqrt47.0%
*-commutative47.0%
add-sqr-sqrt19.3%
fabs-sqr19.3%
add-sqr-sqrt38.7%
Applied egg-rr38.7%
Taylor expanded in y around 0 39.0%
if -1.54999999999999996e113 < x < 5.49999999999999979e105Initial program 100.0%
div-inv99.7%
add-sqr-sqrt53.3%
fabs-sqr53.3%
add-sqr-sqrt54.2%
*-commutative54.2%
add-sqr-sqrt9.1%
fabs-sqr9.1%
add-sqr-sqrt16.6%
Applied egg-rr16.6%
flip--14.4%
associate-*r/13.3%
+-commutative13.3%
Applied egg-rr13.3%
Taylor expanded in x around 0 1.8%
unpow21.8%
mul-1-neg1.8%
distribute-rgt-neg-out1.8%
Simplified1.8%
associate-*r*2.0%
lft-mult-inverse2.0%
*-un-lft-identity2.0%
neg-sub02.0%
metadata-eval2.0%
sub-neg2.0%
metadata-eval2.0%
add-sqr-sqrt1.1%
sqrt-unprod18.3%
sqr-neg18.3%
sqrt-unprod32.9%
add-sqr-sqrt65.9%
Applied egg-rr65.9%
+-lft-identity65.9%
Simplified65.9%
Final simplification56.0%
(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-sqrt50.8%
fabs-sqr50.8%
add-sqr-sqrt51.5%
*-commutative51.5%
add-sqr-sqrt12.8%
fabs-sqr12.8%
add-sqr-sqrt24.7%
Applied egg-rr24.7%
Taylor expanded in y around 0 25.3%
Final simplification25.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%
div-inv99.7%
add-sqr-sqrt50.8%
fabs-sqr50.8%
add-sqr-sqrt51.5%
*-commutative51.5%
add-sqr-sqrt12.8%
fabs-sqr12.8%
add-sqr-sqrt24.7%
Applied egg-rr24.7%
Taylor expanded in y around inf 1.3%
Final simplification1.3%
herbie shell --seed 2023195
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))