
(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 7 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) y)))
double code(double x, double y) {
return fabs(((y - x) / y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = abs(((y - x) / y))
end function
public static double code(double x, double y) {
return Math.abs(((y - x) / y));
}
def code(x, y): return math.fabs(((y - x) / y))
function code(x, y) return abs(Float64(Float64(y - x) / y)) end
function tmp = code(x, y) tmp = abs(((y - x) / y)); end
code[x_, y_] := N[Abs[N[(N[(y - x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\left|\frac{y - x}{y}\right|
\end{array}
Initial program 100.0%
fabs-sub100.0%
div-fabs100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(if (<= y -2.4e-58)
(* (- y x) (/ 1.0 y))
(if (<= y 1.2e-48)
(fabs (/ x y))
(* (- y x) (* (/ 1.0 (+ y x)) (/ (+ y x) y))))))
double code(double x, double y) {
double tmp;
if (y <= -2.4e-58) {
tmp = (y - x) * (1.0 / y);
} else if (y <= 1.2e-48) {
tmp = fabs((x / y));
} else {
tmp = (y - x) * ((1.0 / (y + x)) * ((y + x) / y));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-2.4d-58)) then
tmp = (y - x) * (1.0d0 / y)
else if (y <= 1.2d-48) then
tmp = abs((x / y))
else
tmp = (y - x) * ((1.0d0 / (y + x)) * ((y + x) / y))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -2.4e-58) {
tmp = (y - x) * (1.0 / y);
} else if (y <= 1.2e-48) {
tmp = Math.abs((x / y));
} else {
tmp = (y - x) * ((1.0 / (y + x)) * ((y + x) / y));
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -2.4e-58: tmp = (y - x) * (1.0 / y) elif y <= 1.2e-48: tmp = math.fabs((x / y)) else: tmp = (y - x) * ((1.0 / (y + x)) * ((y + x) / y)) return tmp
function code(x, y) tmp = 0.0 if (y <= -2.4e-58) tmp = Float64(Float64(y - x) * Float64(1.0 / y)); elseif (y <= 1.2e-48) tmp = abs(Float64(x / y)); else tmp = Float64(Float64(y - x) * Float64(Float64(1.0 / Float64(y + x)) * Float64(Float64(y + x) / y))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -2.4e-58) tmp = (y - x) * (1.0 / y); elseif (y <= 1.2e-48) tmp = abs((x / y)); else tmp = (y - x) * ((1.0 / (y + x)) * ((y + x) / y)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -2.4e-58], N[(N[(y - x), $MachinePrecision] * N[(1.0 / y), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 1.2e-48], N[Abs[N[(x / y), $MachinePrecision]], $MachinePrecision], N[(N[(y - x), $MachinePrecision] * N[(N[(1.0 / N[(y + x), $MachinePrecision]), $MachinePrecision] * N[(N[(y + x), $MachinePrecision] / y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.4 \cdot 10^{-58}:\\
\;\;\;\;\left(y - x\right) \cdot \frac{1}{y}\\
\mathbf{elif}\;y \leq 1.2 \cdot 10^{-48}:\\
\;\;\;\;\left|\frac{x}{y}\right|\\
\mathbf{else}:\\
\;\;\;\;\left(y - x\right) \cdot \left(\frac{1}{y + x} \cdot \frac{y + x}{y}\right)\\
\end{array}
\end{array}
if y < -2.4000000000000001e-58Initial program 100.0%
div-inv99.7%
add-sqr-sqrt90.2%
fabs-sqr90.2%
add-sqr-sqrt90.9%
*-commutative90.9%
add-sqr-sqrt0.0%
fabs-sqr0.0%
add-sqr-sqrt10.3%
Applied egg-rr10.3%
flip--4.1%
associate-*r/4.1%
difference-of-squares4.2%
+-commutative4.2%
+-commutative4.2%
Applied egg-rr4.2%
flip-+3.7%
associate-/r/3.7%
associate-*r*4.9%
*-commutative4.9%
associate-*l/4.9%
*-un-lft-identity4.9%
cancel-sign-sub-inv4.9%
fma-def5.1%
Applied egg-rr5.1%
*-commutative5.1%
associate-/l*5.2%
fma-udef5.0%
cancel-sign-sub-inv5.0%
+-commutative5.0%
Simplified5.0%
expm1-log1p-u4.9%
expm1-udef4.0%
Applied egg-rr5.6%
expm1-def38.6%
expm1-log1p47.5%
associate-/r*90.9%
*-inverses90.9%
Simplified90.9%
if -2.4000000000000001e-58 < y < 1.2e-48Initial program 100.0%
fabs-sub100.0%
div-fabs100.0%
Applied egg-rr100.0%
Taylor expanded in y around 0 83.4%
neg-mul-183.4%
distribute-neg-frac83.4%
Simplified83.4%
if 1.2e-48 < y Initial program 100.0%
div-inv99.7%
add-sqr-sqrt15.2%
fabs-sqr15.2%
add-sqr-sqrt16.6%
*-commutative16.6%
add-sqr-sqrt16.5%
fabs-sqr16.5%
add-sqr-sqrt16.6%
Applied egg-rr16.6%
flip--6.4%
associate-*r/6.3%
difference-of-squares6.4%
+-commutative6.4%
+-commutative6.4%
Applied egg-rr6.4%
flip-+5.7%
associate-/r/5.7%
associate-*r*6.8%
*-commutative6.8%
associate-*l/6.8%
*-un-lft-identity6.8%
cancel-sign-sub-inv6.8%
fma-def7.1%
Applied egg-rr7.1%
*-commutative7.1%
associate-/l*7.3%
fma-udef7.0%
cancel-sign-sub-inv7.0%
+-commutative7.0%
Simplified7.0%
*-un-lft-identity7.0%
associate-/r/7.0%
+-commutative7.0%
flip--14.7%
times-frac16.6%
sub-neg16.6%
mul-1-neg16.6%
add-sqr-sqrt0.8%
sqrt-unprod1.8%
mul-1-neg1.8%
mul-1-neg1.8%
sqr-neg1.8%
sqrt-prod0.6%
add-sqr-sqrt12.8%
+-commutative12.8%
Applied egg-rr84.5%
Final simplification85.7%
(FPCore (x y) :precision binary64 (if (or (<= y -8.4e-203) (not (<= y 1.9e-297))) (* (- y x) (/ 1.0 y)) (/ x y)))
double code(double x, double y) {
double tmp;
if ((y <= -8.4e-203) || !(y <= 1.9e-297)) {
tmp = (y - x) * (1.0 / 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 ((y <= (-8.4d-203)) .or. (.not. (y <= 1.9d-297))) then
tmp = (y - x) * (1.0d0 / y)
else
tmp = x / y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -8.4e-203) || !(y <= 1.9e-297)) {
tmp = (y - x) * (1.0 / y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -8.4e-203) or not (y <= 1.9e-297): tmp = (y - x) * (1.0 / y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if ((y <= -8.4e-203) || !(y <= 1.9e-297)) tmp = Float64(Float64(y - x) * Float64(1.0 / y)); else tmp = Float64(x / y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -8.4e-203) || ~((y <= 1.9e-297))) tmp = (y - x) * (1.0 / y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -8.4e-203], N[Not[LessEqual[y, 1.9e-297]], $MachinePrecision]], N[(N[(y - x), $MachinePrecision] * N[(1.0 / y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -8.4 \cdot 10^{-203} \lor \neg \left(y \leq 1.9 \cdot 10^{-297}\right):\\
\;\;\;\;\left(y - x\right) \cdot \frac{1}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if y < -8.40000000000000008e-203 or 1.90000000000000002e-297 < y Initial program 100.0%
div-inv99.7%
add-sqr-sqrt48.9%
fabs-sqr48.9%
add-sqr-sqrt49.8%
*-commutative49.8%
add-sqr-sqrt14.5%
fabs-sqr14.5%
add-sqr-sqrt21.7%
Applied egg-rr21.7%
flip--14.7%
associate-*r/14.3%
difference-of-squares14.4%
+-commutative14.4%
+-commutative14.4%
Applied egg-rr14.4%
flip-+11.6%
associate-/r/11.6%
associate-*r*12.3%
*-commutative12.3%
associate-*l/12.3%
*-un-lft-identity12.3%
cancel-sign-sub-inv12.3%
fma-def12.5%
Applied egg-rr12.5%
*-commutative12.5%
associate-/l*12.0%
fma-udef11.8%
cancel-sign-sub-inv11.8%
+-commutative11.8%
Simplified11.8%
expm1-log1p-u5.7%
expm1-udef4.7%
Applied egg-rr20.3%
expm1-def39.8%
expm1-log1p50.6%
associate-/r*79.3%
*-inverses79.3%
Simplified79.3%
if -8.40000000000000008e-203 < y < 1.90000000000000002e-297Initial program 100.0%
div-inv99.9%
add-sqr-sqrt44.6%
fabs-sqr44.6%
add-sqr-sqrt44.9%
*-commutative44.9%
add-sqr-sqrt6.9%
fabs-sqr6.9%
add-sqr-sqrt62.2%
Applied egg-rr62.2%
Taylor expanded in y around 0 62.3%
Final simplification77.3%
(FPCore (x y) :precision binary64 (if (or (<= y -9.5e-58) (not (<= y 8e-49))) (/ y (+ y x)) (/ x y)))
double code(double x, double y) {
double tmp;
if ((y <= -9.5e-58) || !(y <= 8e-49)) {
tmp = y / (y + x);
} 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 ((y <= (-9.5d-58)) .or. (.not. (y <= 8d-49))) then
tmp = y / (y + x)
else
tmp = x / y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -9.5e-58) || !(y <= 8e-49)) {
tmp = y / (y + x);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -9.5e-58) or not (y <= 8e-49): tmp = y / (y + x) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if ((y <= -9.5e-58) || !(y <= 8e-49)) tmp = Float64(y / Float64(y + x)); else tmp = Float64(x / y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -9.5e-58) || ~((y <= 8e-49))) tmp = y / (y + x); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -9.5e-58], N[Not[LessEqual[y, 8e-49]], $MachinePrecision]], N[(y / N[(y + x), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -9.5 \cdot 10^{-58} \lor \neg \left(y \leq 8 \cdot 10^{-49}\right):\\
\;\;\;\;\frac{y}{y + x}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if y < -9.4999999999999994e-58 or 7.99999999999999949e-49 < y Initial program 100.0%
div-inv99.7%
add-sqr-sqrt49.9%
fabs-sqr49.9%
add-sqr-sqrt50.9%
*-commutative50.9%
add-sqr-sqrt8.9%
fabs-sqr8.9%
add-sqr-sqrt13.7%
Applied egg-rr13.7%
flip--5.3%
associate-*r/5.3%
difference-of-squares5.4%
+-commutative5.4%
+-commutative5.4%
Applied egg-rr5.4%
Taylor expanded in y around inf 1.8%
*-commutative1.8%
neg-mul-11.8%
+-commutative1.8%
sub-neg1.8%
fma-def1.8%
sub-neg1.8%
add-sqr-sqrt0.8%
sqrt-unprod21.4%
sqr-neg21.4%
sqrt-prod38.9%
add-sqr-sqrt79.4%
Applied egg-rr79.4%
fma-udef79.4%
*-commutative79.4%
mul-1-neg79.4%
neg-sub079.4%
associate--r-79.4%
neg-sub079.4%
neg-mul-179.4%
associate--r+79.5%
+-inverses79.5%
neg-sub079.5%
mul-1-neg79.5%
associate-*r*79.5%
metadata-eval79.5%
*-lft-identity79.5%
Simplified79.5%
if -9.4999999999999994e-58 < y < 7.99999999999999949e-49Initial program 100.0%
div-inv99.7%
add-sqr-sqrt46.5%
fabs-sqr46.5%
add-sqr-sqrt47.1%
*-commutative47.1%
add-sqr-sqrt19.9%
fabs-sqr19.9%
add-sqr-sqrt42.6%
Applied egg-rr42.6%
Taylor expanded in y around 0 43.3%
Final simplification63.8%
(FPCore (x y) :precision binary64 (/ 0.0 x))
double code(double x, double y) {
return 0.0 / x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 0.0d0 / x
end function
public static double code(double x, double y) {
return 0.0 / x;
}
def code(x, y): return 0.0 / x
function code(x, y) return Float64(0.0 / x) end
function tmp = code(x, y) tmp = 0.0 / x; end
code[x_, y_] := N[(0.0 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{0}{x}
\end{array}
Initial program 100.0%
div-inv99.7%
add-sqr-sqrt48.4%
fabs-sqr48.4%
add-sqr-sqrt49.3%
*-commutative49.3%
add-sqr-sqrt13.7%
fabs-sqr13.7%
add-sqr-sqrt26.2%
Applied egg-rr26.2%
flip--19.0%
associate-*r/17.6%
difference-of-squares17.7%
+-commutative17.7%
+-commutative17.7%
Applied egg-rr17.7%
Taylor expanded in y around inf 2.0%
Taylor expanded in y around 0 2.7%
+-commutative2.7%
mul-1-neg2.7%
sub-neg2.7%
+-inverses2.7%
Simplified2.7%
Final simplification2.7%
(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-sqrt48.4%
fabs-sqr48.4%
add-sqr-sqrt49.3%
*-commutative49.3%
add-sqr-sqrt13.7%
fabs-sqr13.7%
add-sqr-sqrt26.2%
Applied egg-rr26.2%
Taylor expanded in y around 0 27.4%
Final simplification27.4%
(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-sqrt48.4%
fabs-sqr48.4%
add-sqr-sqrt49.3%
*-commutative49.3%
add-sqr-sqrt13.7%
fabs-sqr13.7%
add-sqr-sqrt26.2%
Applied egg-rr26.2%
Taylor expanded in y around inf 1.3%
Final simplification1.3%
herbie shell --seed 2023238
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))