
(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 6 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 -9.5e+60) (/ (/ x y) (/ (- y x) x)) (if (<= x 2.2e+36) (/ y (+ x y)) (/ x y))))
double code(double x, double y) {
double tmp;
if (x <= -9.5e+60) {
tmp = (x / y) / ((y - x) / x);
} else if (x <= 2.2e+36) {
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 <= (-9.5d+60)) then
tmp = (x / y) / ((y - x) / x)
else if (x <= 2.2d+36) 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 <= -9.5e+60) {
tmp = (x / y) / ((y - x) / x);
} else if (x <= 2.2e+36) {
tmp = y / (x + y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -9.5e+60: tmp = (x / y) / ((y - x) / x) elif x <= 2.2e+36: tmp = y / (x + y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (x <= -9.5e+60) tmp = Float64(Float64(x / y) / Float64(Float64(y - x) / x)); elseif (x <= 2.2e+36) 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 <= -9.5e+60) tmp = (x / y) / ((y - x) / x); elseif (x <= 2.2e+36) tmp = y / (x + y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -9.5e+60], N[(N[(x / y), $MachinePrecision] / N[(N[(y - x), $MachinePrecision] / x), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 2.2e+36], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9.5 \cdot 10^{+60}:\\
\;\;\;\;\frac{\frac{x}{y}}{\frac{y - x}{x}}\\
\mathbf{elif}\;x \leq 2.2 \cdot 10^{+36}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < -9.49999999999999988e60Initial program 100.0%
div-inv99.7%
add-sqr-sqrt15.0%
fabs-sqr15.0%
add-sqr-sqrt15.5%
*-commutative15.5%
add-sqr-sqrt0.3%
fabs-sqr0.3%
add-sqr-sqrt23.1%
Applied egg-rr23.1%
flip--8.6%
associate-*r/8.6%
+-commutative8.6%
Applied egg-rr8.6%
Taylor expanded in y around 0 9.5%
unpow29.5%
associate-/l*11.8%
associate-/r/11.9%
Simplified11.9%
associate-*l/9.5%
*-un-lft-identity9.5%
associate-*l/9.5%
frac-2neg9.5%
div-inv9.5%
associate-*l/9.5%
*-un-lft-identity9.5%
distribute-neg-frac9.5%
add-sqr-sqrt0.5%
sqrt-unprod0.6%
sqr-neg0.6%
sqrt-unprod0.2%
add-sqr-sqrt33.7%
frac-2neg33.7%
associate-*r/39.4%
distribute-neg-in39.4%
add-sqr-sqrt0.4%
sqrt-unprod36.0%
sqr-neg36.0%
sqrt-unprod39.1%
add-sqr-sqrt40.2%
sub-neg40.2%
Applied egg-rr40.2%
associate-*r/40.2%
*-rgt-identity40.2%
*-commutative40.2%
associate-/l*56.2%
Simplified56.2%
if -9.49999999999999988e60 < x < 2.2e36Initial program 100.0%
div-inv99.6%
add-sqr-sqrt50.6%
fabs-sqr50.6%
add-sqr-sqrt51.4%
*-commutative51.4%
add-sqr-sqrt7.8%
fabs-sqr7.8%
add-sqr-sqrt17.7%
Applied egg-rr17.7%
flip--16.0%
associate-*r/14.8%
+-commutative14.8%
Applied egg-rr14.8%
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%
add-sqr-sqrt1.2%
sqrt-unprod22.3%
sqr-neg22.3%
sqrt-unprod35.1%
add-sqr-sqrt71.8%
add-log-exp3.9%
*-un-lft-identity3.9%
log-prod3.9%
add-log-exp71.8%
metadata-eval71.8%
Applied egg-rr71.8%
+-lft-identity71.8%
Simplified71.8%
if 2.2e36 < x Initial program 100.0%
div-inv99.8%
add-sqr-sqrt87.9%
fabs-sqr87.9%
add-sqr-sqrt88.4%
*-commutative88.4%
add-sqr-sqrt47.9%
fabs-sqr47.9%
add-sqr-sqrt48.5%
Applied egg-rr48.5%
Taylor expanded in y around 0 49.2%
Final simplification64.0%
(FPCore (x y) :precision binary64 (if (<= x -2.7e+245) (/ x y) (if (<= x 2.15e+37) (/ y (+ x y)) (/ x y))))
double code(double x, double y) {
double tmp;
if (x <= -2.7e+245) {
tmp = x / y;
} else if (x <= 2.15e+37) {
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.7d+245)) then
tmp = x / y
else if (x <= 2.15d+37) 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.7e+245) {
tmp = x / y;
} else if (x <= 2.15e+37) {
tmp = y / (x + y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2.7e+245: tmp = x / y elif x <= 2.15e+37: tmp = y / (x + y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (x <= -2.7e+245) tmp = Float64(x / y); elseif (x <= 2.15e+37) 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.7e+245) tmp = x / y; elseif (x <= 2.15e+37) tmp = y / (x + y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2.7e+245], N[(x / y), $MachinePrecision], If[LessEqual[x, 2.15e+37], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.7 \cdot 10^{+245}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;x \leq 2.15 \cdot 10^{+37}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < -2.69999999999999991e245 or 2.1499999999999998e37 < x Initial program 100.0%
div-inv99.8%
add-sqr-sqrt64.9%
fabs-sqr64.9%
add-sqr-sqrt65.4%
*-commutative65.4%
add-sqr-sqrt34.7%
fabs-sqr34.7%
add-sqr-sqrt44.8%
Applied egg-rr44.8%
Taylor expanded in y around 0 45.6%
if -2.69999999999999991e245 < x < 2.1499999999999998e37Initial program 100.0%
div-inv99.6%
add-sqr-sqrt45.3%
fabs-sqr45.3%
add-sqr-sqrt46.1%
*-commutative46.1%
add-sqr-sqrt6.4%
fabs-sqr6.4%
add-sqr-sqrt17.3%
Applied egg-rr17.3%
flip--14.8%
associate-*r/13.9%
+-commutative13.9%
Applied egg-rr13.9%
Taylor expanded in x around 0 1.9%
unpow21.9%
mul-1-neg1.9%
distribute-rgt-neg-out1.9%
Simplified1.9%
associate-*r*2.1%
lft-mult-inverse2.1%
*-un-lft-identity2.1%
add-sqr-sqrt1.1%
sqrt-unprod19.7%
sqr-neg19.7%
sqrt-unprod31.1%
add-sqr-sqrt65.2%
add-log-exp3.6%
*-un-lft-identity3.6%
log-prod3.6%
add-log-exp65.2%
metadata-eval65.2%
Applied egg-rr65.2%
+-lft-identity65.2%
Simplified65.2%
Final simplification59.7%
(FPCore (x y) :precision binary64 (if (<= x -1.2e+178) (/ (* x x) (* y y)) (if (<= x 3.9e+37) (/ y (+ x y)) (/ x y))))
double code(double x, double y) {
double tmp;
if (x <= -1.2e+178) {
tmp = (x * x) / (y * y);
} else if (x <= 3.9e+37) {
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.2d+178)) then
tmp = (x * x) / (y * y)
else if (x <= 3.9d+37) 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.2e+178) {
tmp = (x * x) / (y * y);
} else if (x <= 3.9e+37) {
tmp = y / (x + y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1.2e+178: tmp = (x * x) / (y * y) elif x <= 3.9e+37: tmp = y / (x + y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (x <= -1.2e+178) tmp = Float64(Float64(x * x) / Float64(y * y)); elseif (x <= 3.9e+37) 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.2e+178) tmp = (x * x) / (y * y); elseif (x <= 3.9e+37) tmp = y / (x + y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1.2e+178], N[(N[(x * x), $MachinePrecision] / N[(y * y), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 3.9e+37], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.2 \cdot 10^{+178}:\\
\;\;\;\;\frac{x \cdot x}{y \cdot y}\\
\mathbf{elif}\;x \leq 3.9 \cdot 10^{+37}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < -1.2e178Initial program 99.9%
div-inv99.7%
add-sqr-sqrt10.7%
fabs-sqr10.7%
add-sqr-sqrt11.0%
*-commutative11.0%
add-sqr-sqrt0.2%
fabs-sqr0.2%
add-sqr-sqrt28.8%
Applied egg-rr28.8%
flip--5.1%
associate-*r/5.1%
+-commutative5.1%
Applied egg-rr5.1%
Taylor expanded in y around 0 5.7%
unpow25.7%
associate-/l*6.3%
associate-/r/6.3%
Simplified6.3%
Taylor expanded in x around 0 38.6%
unpow238.6%
unpow238.6%
Simplified38.6%
if -1.2e178 < x < 3.8999999999999999e37Initial program 100.0%
div-inv99.6%
add-sqr-sqrt46.2%
fabs-sqr46.2%
add-sqr-sqrt47.0%
*-commutative47.0%
add-sqr-sqrt6.7%
fabs-sqr6.7%
add-sqr-sqrt17.5%
Applied egg-rr17.5%
flip--15.5%
associate-*r/14.5%
+-commutative14.5%
Applied egg-rr14.5%
Taylor expanded in x around 0 1.9%
unpow21.9%
mul-1-neg1.9%
distribute-rgt-neg-out1.9%
Simplified1.9%
associate-*r*2.1%
lft-mult-inverse2.1%
*-un-lft-identity2.1%
add-sqr-sqrt1.1%
sqrt-unprod20.5%
sqr-neg20.5%
sqrt-unprod32.4%
add-sqr-sqrt66.9%
add-log-exp3.7%
*-un-lft-identity3.7%
log-prod3.7%
add-log-exp66.9%
metadata-eval66.9%
Applied egg-rr66.9%
+-lft-identity66.9%
Simplified66.9%
if 3.8999999999999999e37 < x Initial program 100.0%
div-inv99.8%
add-sqr-sqrt87.9%
fabs-sqr87.9%
add-sqr-sqrt88.4%
*-commutative88.4%
add-sqr-sqrt47.9%
fabs-sqr47.9%
add-sqr-sqrt48.5%
Applied egg-rr48.5%
Taylor expanded in y around 0 49.2%
Final simplification60.2%
(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-sqrt14.4%
fabs-sqr14.4%
add-sqr-sqrt25.1%
Applied egg-rr25.1%
Taylor expanded in y around 0 25.9%
Final simplification25.9%
(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-sqrt14.4%
fabs-sqr14.4%
add-sqr-sqrt25.1%
Applied egg-rr25.1%
Taylor expanded in y around inf 1.3%
Final simplification1.3%
herbie shell --seed 2023279
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))