
(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 (if (<= x -2.2e+43) (* x (/ (/ x y) (- y x))) (if (<= x 1.05e+107) (/ y (+ x y)) (/ x y))))
double code(double x, double y) {
double tmp;
if (x <= -2.2e+43) {
tmp = x * ((x / y) / (y - x));
} else if (x <= 1.05e+107) {
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 <= (-2.2d+43)) then
tmp = x * ((x / y) / (y - x))
else if (x <= 1.05d+107) 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 <= -2.2e+43) {
tmp = x * ((x / y) / (y - x));
} else if (x <= 1.05e+107) {
tmp = y / (x + y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2.2e+43: tmp = x * ((x / y) / (y - x)) elif x <= 1.05e+107: tmp = y / (x + y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (x <= -2.2e+43) tmp = Float64(x * Float64(Float64(x / y) / Float64(y - x))); elseif (x <= 1.05e+107) 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 <= -2.2e+43) tmp = x * ((x / y) / (y - x)); elseif (x <= 1.05e+107) tmp = y / (x + y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2.2e+43], N[(x * N[(N[(x / y), $MachinePrecision] / N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 1.05e+107], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.2 \cdot 10^{+43}:\\
\;\;\;\;x \cdot \frac{\frac{x}{y}}{y - x}\\
\mathbf{elif}\;x \leq 1.05 \cdot 10^{+107}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < -2.20000000000000001e43Initial program 100.0%
div-inv99.7%
add-sqr-sqrt1.9%
fabs-sqr1.9%
add-sqr-sqrt2.4%
*-commutative2.4%
add-sqr-sqrt0.3%
fabs-sqr0.3%
add-sqr-sqrt36.1%
Applied egg-rr36.1%
flip--20.0%
associate-*r/18.2%
+-commutative18.2%
Applied egg-rr18.2%
Taylor expanded in y around 0 18.8%
unpow218.8%
associate-/l*19.0%
associate-/r/19.0%
Simplified19.0%
associate-*l/18.8%
*-un-lft-identity18.8%
associate-*l/18.8%
frac-2neg18.8%
div-inv18.8%
associate-*l/18.7%
*-un-lft-identity18.7%
distribute-neg-frac18.7%
add-sqr-sqrt0.6%
sqrt-unprod0.5%
sqr-neg0.5%
sqrt-unprod0.1%
add-sqr-sqrt25.1%
frac-2neg25.1%
associate-*r/25.2%
distribute-neg-in25.2%
add-sqr-sqrt0.2%
sqrt-unprod25.4%
sqr-neg25.4%
sqrt-unprod25.4%
add-sqr-sqrt25.7%
sub-neg25.7%
Applied egg-rr25.7%
associate-*l*51.7%
associate-*r/51.7%
*-rgt-identity51.7%
Simplified51.7%
if -2.20000000000000001e43 < x < 1.05e107Initial program 100.0%
div-inv99.7%
add-sqr-sqrt53.1%
fabs-sqr53.1%
add-sqr-sqrt54.0%
*-commutative54.0%
add-sqr-sqrt7.6%
fabs-sqr7.6%
add-sqr-sqrt13.5%
Applied egg-rr13.5%
flip--12.1%
associate-*r/11.5%
+-commutative11.5%
Applied egg-rr11.5%
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-unprod22.0%
sqr-neg22.0%
sqrt-unprod34.4%
add-sqr-sqrt75.1%
Applied egg-rr75.1%
+-lft-identity75.1%
Simplified75.1%
if 1.05e107 < x Initial program 100.0%
div-inv99.6%
add-sqr-sqrt95.2%
fabs-sqr95.2%
add-sqr-sqrt95.5%
*-commutative95.5%
add-sqr-sqrt47.7%
fabs-sqr47.7%
add-sqr-sqrt48.1%
Applied egg-rr48.1%
Taylor expanded in y around 0 48.4%
Final simplification65.3%
(FPCore (x y) :precision binary64 (if (<= x -7e+88) (/ x y) (if (<= x 1.56e+108) (/ y (+ x y)) (/ x y))))
double code(double x, double y) {
double tmp;
if (x <= -7e+88) {
tmp = x / y;
} else if (x <= 1.56e+108) {
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 <= (-7d+88)) then
tmp = x / y
else if (x <= 1.56d+108) 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 <= -7e+88) {
tmp = x / y;
} else if (x <= 1.56e+108) {
tmp = y / (x + y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -7e+88: tmp = x / y elif x <= 1.56e+108: tmp = y / (x + y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (x <= -7e+88) tmp = Float64(x / y); elseif (x <= 1.56e+108) 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 <= -7e+88) tmp = x / y; elseif (x <= 1.56e+108) tmp = y / (x + y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -7e+88], N[(x / y), $MachinePrecision], If[LessEqual[x, 1.56e+108], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7 \cdot 10^{+88}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;x \leq 1.56 \cdot 10^{+108}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < -6.9999999999999995e88 or 1.5599999999999999e108 < x Initial program 100.0%
div-inv99.7%
add-sqr-sqrt49.7%
fabs-sqr49.7%
add-sqr-sqrt50.1%
*-commutative50.1%
add-sqr-sqrt24.5%
fabs-sqr24.5%
add-sqr-sqrt43.8%
Applied egg-rr43.8%
Taylor expanded in y around 0 44.1%
if -6.9999999999999995e88 < x < 1.5599999999999999e108Initial program 100.0%
div-inv99.7%
add-sqr-sqrt50.8%
fabs-sqr50.8%
add-sqr-sqrt51.7%
*-commutative51.7%
add-sqr-sqrt7.3%
fabs-sqr7.3%
add-sqr-sqrt13.5%
Applied egg-rr13.5%
flip--12.2%
associate-*r/11.7%
+-commutative11.7%
Applied egg-rr11.7%
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-sqrt0.9%
sqrt-unprod21.7%
sqr-neg21.7%
sqrt-unprod34.1%
add-sqr-sqrt73.2%
Applied egg-rr73.2%
+-lft-identity73.2%
Simplified73.2%
Final simplification62.5%
(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.4%
fabs-sqr50.4%
add-sqr-sqrt51.1%
*-commutative51.1%
add-sqr-sqrt13.6%
fabs-sqr13.6%
add-sqr-sqrt24.7%
Applied egg-rr24.7%
Taylor expanded in y around 0 25.5%
Final simplification25.5%
(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.4%
fabs-sqr50.4%
add-sqr-sqrt51.1%
*-commutative51.1%
add-sqr-sqrt13.6%
fabs-sqr13.6%
add-sqr-sqrt24.7%
Applied egg-rr24.7%
Taylor expanded in y around inf 1.3%
Final simplification1.3%
herbie shell --seed 2023230
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))