
(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
(let* ((t_0 (/ y (+ x y))))
(if (<= y -1.3e-22)
t_0
(if (<= y -4.4e-267)
(+ (/ x y) -1.0)
(if (<= y 4.1e-112)
(* (/ x y) (/ x (- y x)))
(if (<= y 1.1e-79) (/ x y) t_0))))))
double code(double x, double y) {
double t_0 = y / (x + y);
double tmp;
if (y <= -1.3e-22) {
tmp = t_0;
} else if (y <= -4.4e-267) {
tmp = (x / y) + -1.0;
} else if (y <= 4.1e-112) {
tmp = (x / y) * (x / (y - x));
} else if (y <= 1.1e-79) {
tmp = x / y;
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: tmp
t_0 = y / (x + y)
if (y <= (-1.3d-22)) then
tmp = t_0
else if (y <= (-4.4d-267)) then
tmp = (x / y) + (-1.0d0)
else if (y <= 4.1d-112) then
tmp = (x / y) * (x / (y - x))
else if (y <= 1.1d-79) then
tmp = x / y
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = y / (x + y);
double tmp;
if (y <= -1.3e-22) {
tmp = t_0;
} else if (y <= -4.4e-267) {
tmp = (x / y) + -1.0;
} else if (y <= 4.1e-112) {
tmp = (x / y) * (x / (y - x));
} else if (y <= 1.1e-79) {
tmp = x / y;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = y / (x + y) tmp = 0 if y <= -1.3e-22: tmp = t_0 elif y <= -4.4e-267: tmp = (x / y) + -1.0 elif y <= 4.1e-112: tmp = (x / y) * (x / (y - x)) elif y <= 1.1e-79: tmp = x / y else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(y / Float64(x + y)) tmp = 0.0 if (y <= -1.3e-22) tmp = t_0; elseif (y <= -4.4e-267) tmp = Float64(Float64(x / y) + -1.0); elseif (y <= 4.1e-112) tmp = Float64(Float64(x / y) * Float64(x / Float64(y - x))); elseif (y <= 1.1e-79) tmp = Float64(x / y); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = y / (x + y); tmp = 0.0; if (y <= -1.3e-22) tmp = t_0; elseif (y <= -4.4e-267) tmp = (x / y) + -1.0; elseif (y <= 4.1e-112) tmp = (x / y) * (x / (y - x)); elseif (y <= 1.1e-79) tmp = x / y; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -1.3e-22], t$95$0, If[LessEqual[y, -4.4e-267], N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision], If[LessEqual[y, 4.1e-112], N[(N[(x / y), $MachinePrecision] * N[(x / N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 1.1e-79], N[(x / y), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{y}{x + y}\\
\mathbf{if}\;y \leq -1.3 \cdot 10^{-22}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;y \leq -4.4 \cdot 10^{-267}:\\
\;\;\;\;\frac{x}{y} + -1\\
\mathbf{elif}\;y \leq 4.1 \cdot 10^{-112}:\\
\;\;\;\;\frac{x}{y} \cdot \frac{x}{y - x}\\
\mathbf{elif}\;y \leq 1.1 \cdot 10^{-79}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if y < -1.3e-22 or 1.0999999999999999e-79 < y Initial program 100.0%
div-inv99.7%
add-sqr-sqrt50.1%
fabs-sqr50.1%
add-sqr-sqrt51.2%
*-commutative51.2%
add-sqr-sqrt9.3%
fabs-sqr9.3%
add-sqr-sqrt15.4%
Applied egg-rr15.4%
flip--4.6%
associate-*r/4.6%
+-commutative4.6%
Applied egg-rr4.6%
Taylor expanded in y around inf 2.0%
neg-mul-12.0%
Simplified2.0%
*-un-lft-identity2.0%
lft-mult-inverse2.0%
associate-*r*1.5%
*-commutative1.5%
*-commutative1.5%
associate-/l*1.5%
div-inv1.5%
div-inv1.5%
metadata-eval1.5%
add-sqr-sqrt0.7%
sqrt-unprod17.3%
sqr-neg17.3%
sqrt-unprod16.6%
add-sqr-sqrt35.8%
frac-times37.5%
inv-pow37.5%
inv-pow37.5%
pow-prod-up37.5%
metadata-eval37.5%
Applied egg-rr37.5%
*-commutative37.5%
*-commutative37.5%
associate-/r*35.8%
metadata-eval35.8%
pow-sqr35.7%
unpow-135.7%
unpow-135.7%
associate-/l/35.8%
remove-double-div35.9%
associate-/l*35.9%
/-rgt-identity35.9%
associate-/l*71.9%
associate-*l/71.9%
/-rgt-identity71.9%
associate-/r/71.9%
remove-double-div72.0%
*-inverses72.0%
associate-/l*72.0%
*-lft-identity72.0%
Simplified72.0%
if -1.3e-22 < y < -4.39999999999999976e-267Initial program 100.0%
Taylor expanded in x around -inf 100.0%
fabs-neg100.0%
mul-1-neg100.0%
sub-neg100.0%
fabs-sub100.0%
fabs-div100.0%
rem-square-sqrt55.4%
fabs-sqr55.4%
rem-square-sqrt56.0%
div-sub56.0%
sub-neg56.0%
*-inverses56.0%
metadata-eval56.0%
+-commutative56.0%
Simplified56.0%
if -4.39999999999999976e-267 < y < 4.09999999999999996e-112Initial program 100.0%
div-inv99.8%
add-sqr-sqrt45.5%
fabs-sqr45.5%
add-sqr-sqrt45.9%
*-commutative45.9%
add-sqr-sqrt32.8%
fabs-sqr32.8%
add-sqr-sqrt37.2%
Applied egg-rr37.2%
flip--30.9%
associate-*r/26.9%
+-commutative26.9%
Applied egg-rr26.9%
Taylor expanded in y around 0 27.1%
unpow227.1%
associate-/l*33.5%
associate-/r/33.5%
Simplified33.5%
associate-/r/33.5%
expm1-log1p-u32.1%
expm1-udef32.0%
Applied egg-rr44.7%
expm1-def44.8%
expm1-log1p46.9%
associate-/l/44.1%
times-frac52.9%
Simplified52.9%
if 4.09999999999999996e-112 < y < 1.0999999999999999e-79Initial program 100.0%
div-inv99.0%
add-sqr-sqrt74.1%
fabs-sqr74.1%
add-sqr-sqrt74.6%
*-commutative74.6%
add-sqr-sqrt74.3%
fabs-sqr74.3%
add-sqr-sqrt74.6%
Applied egg-rr74.6%
Taylor expanded in y around 0 75.3%
Final simplification65.3%
(FPCore (x y)
:precision binary64
(let* ((t_0 (/ y (+ x y))))
(if (<= y -2.7e-23)
t_0
(if (<= y -6e-302)
(+ (/ x y) -1.0)
(if (<= y 7e-116)
(* (/ x y) (/ x y))
(if (<= y 1.05e-79) (/ x y) t_0))))))
double code(double x, double y) {
double t_0 = y / (x + y);
double tmp;
if (y <= -2.7e-23) {
tmp = t_0;
} else if (y <= -6e-302) {
tmp = (x / y) + -1.0;
} else if (y <= 7e-116) {
tmp = (x / y) * (x / y);
} else if (y <= 1.05e-79) {
tmp = x / y;
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: tmp
t_0 = y / (x + y)
if (y <= (-2.7d-23)) then
tmp = t_0
else if (y <= (-6d-302)) then
tmp = (x / y) + (-1.0d0)
else if (y <= 7d-116) then
tmp = (x / y) * (x / y)
else if (y <= 1.05d-79) then
tmp = x / y
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = y / (x + y);
double tmp;
if (y <= -2.7e-23) {
tmp = t_0;
} else if (y <= -6e-302) {
tmp = (x / y) + -1.0;
} else if (y <= 7e-116) {
tmp = (x / y) * (x / y);
} else if (y <= 1.05e-79) {
tmp = x / y;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = y / (x + y) tmp = 0 if y <= -2.7e-23: tmp = t_0 elif y <= -6e-302: tmp = (x / y) + -1.0 elif y <= 7e-116: tmp = (x / y) * (x / y) elif y <= 1.05e-79: tmp = x / y else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(y / Float64(x + y)) tmp = 0.0 if (y <= -2.7e-23) tmp = t_0; elseif (y <= -6e-302) tmp = Float64(Float64(x / y) + -1.0); elseif (y <= 7e-116) tmp = Float64(Float64(x / y) * Float64(x / y)); elseif (y <= 1.05e-79) tmp = Float64(x / y); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = y / (x + y); tmp = 0.0; if (y <= -2.7e-23) tmp = t_0; elseif (y <= -6e-302) tmp = (x / y) + -1.0; elseif (y <= 7e-116) tmp = (x / y) * (x / y); elseif (y <= 1.05e-79) tmp = x / y; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -2.7e-23], t$95$0, If[LessEqual[y, -6e-302], N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision], If[LessEqual[y, 7e-116], N[(N[(x / y), $MachinePrecision] * N[(x / y), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 1.05e-79], N[(x / y), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{y}{x + y}\\
\mathbf{if}\;y \leq -2.7 \cdot 10^{-23}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;y \leq -6 \cdot 10^{-302}:\\
\;\;\;\;\frac{x}{y} + -1\\
\mathbf{elif}\;y \leq 7 \cdot 10^{-116}:\\
\;\;\;\;\frac{x}{y} \cdot \frac{x}{y}\\
\mathbf{elif}\;y \leq 1.05 \cdot 10^{-79}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if y < -2.69999999999999985e-23 or 1.05e-79 < y Initial program 100.0%
div-inv99.7%
add-sqr-sqrt50.1%
fabs-sqr50.1%
add-sqr-sqrt51.2%
*-commutative51.2%
add-sqr-sqrt9.3%
fabs-sqr9.3%
add-sqr-sqrt15.4%
Applied egg-rr15.4%
flip--4.6%
associate-*r/4.6%
+-commutative4.6%
Applied egg-rr4.6%
Taylor expanded in y around inf 2.0%
neg-mul-12.0%
Simplified2.0%
*-un-lft-identity2.0%
lft-mult-inverse2.0%
associate-*r*1.5%
*-commutative1.5%
*-commutative1.5%
associate-/l*1.5%
div-inv1.5%
div-inv1.5%
metadata-eval1.5%
add-sqr-sqrt0.7%
sqrt-unprod17.3%
sqr-neg17.3%
sqrt-unprod16.6%
add-sqr-sqrt35.8%
frac-times37.5%
inv-pow37.5%
inv-pow37.5%
pow-prod-up37.5%
metadata-eval37.5%
Applied egg-rr37.5%
*-commutative37.5%
*-commutative37.5%
associate-/r*35.8%
metadata-eval35.8%
pow-sqr35.7%
unpow-135.7%
unpow-135.7%
associate-/l/35.8%
remove-double-div35.9%
associate-/l*35.9%
/-rgt-identity35.9%
associate-/l*71.9%
associate-*l/71.9%
/-rgt-identity71.9%
associate-/r/71.9%
remove-double-div72.0%
*-inverses72.0%
associate-/l*72.0%
*-lft-identity72.0%
Simplified72.0%
if -2.69999999999999985e-23 < y < -5.99999999999999978e-302Initial program 100.0%
Taylor expanded in x around -inf 100.0%
fabs-neg100.0%
mul-1-neg100.0%
sub-neg100.0%
fabs-sub100.0%
fabs-div100.0%
rem-square-sqrt53.2%
fabs-sqr53.2%
rem-square-sqrt53.7%
div-sub53.7%
sub-neg53.7%
*-inverses53.7%
metadata-eval53.7%
+-commutative53.7%
Simplified53.7%
if -5.99999999999999978e-302 < y < 6.99999999999999968e-116Initial program 100.0%
div-inv99.8%
add-sqr-sqrt42.3%
fabs-sqr42.3%
add-sqr-sqrt42.7%
*-commutative42.7%
add-sqr-sqrt37.7%
fabs-sqr37.7%
add-sqr-sqrt37.8%
Applied egg-rr37.8%
flip--35.4%
associate-*r/30.8%
+-commutative30.8%
Applied egg-rr30.8%
Taylor expanded in y around 0 31.0%
unpow231.0%
associate-/l*33.5%
associate-/r/33.5%
Simplified33.5%
Taylor expanded in x around 0 48.0%
unpow248.0%
unpow248.0%
times-frac48.5%
Simplified48.5%
if 6.99999999999999968e-116 < y < 1.05e-79Initial program 100.0%
div-inv99.0%
add-sqr-sqrt74.1%
fabs-sqr74.1%
add-sqr-sqrt74.6%
*-commutative74.6%
add-sqr-sqrt74.3%
fabs-sqr74.3%
add-sqr-sqrt74.6%
Applied egg-rr74.6%
Taylor expanded in y around 0 75.3%
Final simplification64.2%
(FPCore (x y) :precision binary64 (if (or (<= y -5.8e-28) (not (<= y 9.8e-80))) (/ y (+ x y)) (+ (/ x y) -1.0)))
double code(double x, double y) {
double tmp;
if ((y <= -5.8e-28) || !(y <= 9.8e-80)) {
tmp = y / (x + y);
} else {
tmp = (x / y) + -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 <= (-5.8d-28)) .or. (.not. (y <= 9.8d-80))) then
tmp = y / (x + y)
else
tmp = (x / y) + (-1.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -5.8e-28) || !(y <= 9.8e-80)) {
tmp = y / (x + y);
} else {
tmp = (x / y) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -5.8e-28) or not (y <= 9.8e-80): tmp = y / (x + y) else: tmp = (x / y) + -1.0 return tmp
function code(x, y) tmp = 0.0 if ((y <= -5.8e-28) || !(y <= 9.8e-80)) tmp = Float64(y / Float64(x + y)); else tmp = Float64(Float64(x / y) + -1.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -5.8e-28) || ~((y <= 9.8e-80))) tmp = y / (x + y); else tmp = (x / y) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -5.8e-28], N[Not[LessEqual[y, 9.8e-80]], $MachinePrecision]], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -5.8 \cdot 10^{-28} \lor \neg \left(y \leq 9.8 \cdot 10^{-80}\right):\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y} + -1\\
\end{array}
\end{array}
if y < -5.80000000000000026e-28 or 9.79999999999999981e-80 < y Initial program 100.0%
div-inv99.7%
add-sqr-sqrt50.1%
fabs-sqr50.1%
add-sqr-sqrt51.2%
*-commutative51.2%
add-sqr-sqrt9.3%
fabs-sqr9.3%
add-sqr-sqrt15.4%
Applied egg-rr15.4%
flip--4.6%
associate-*r/4.6%
+-commutative4.6%
Applied egg-rr4.6%
Taylor expanded in y around inf 2.0%
neg-mul-12.0%
Simplified2.0%
*-un-lft-identity2.0%
lft-mult-inverse2.0%
associate-*r*1.5%
*-commutative1.5%
*-commutative1.5%
associate-/l*1.5%
div-inv1.5%
div-inv1.5%
metadata-eval1.5%
add-sqr-sqrt0.7%
sqrt-unprod17.3%
sqr-neg17.3%
sqrt-unprod16.6%
add-sqr-sqrt35.8%
frac-times37.5%
inv-pow37.5%
inv-pow37.5%
pow-prod-up37.5%
metadata-eval37.5%
Applied egg-rr37.5%
*-commutative37.5%
*-commutative37.5%
associate-/r*35.8%
metadata-eval35.8%
pow-sqr35.7%
unpow-135.7%
unpow-135.7%
associate-/l/35.8%
remove-double-div35.9%
associate-/l*35.9%
/-rgt-identity35.9%
associate-/l*71.9%
associate-*l/71.9%
/-rgt-identity71.9%
associate-/r/71.9%
remove-double-div72.0%
*-inverses72.0%
associate-/l*72.0%
*-lft-identity72.0%
Simplified72.0%
if -5.80000000000000026e-28 < y < 9.79999999999999981e-80Initial program 100.0%
Taylor expanded in x around -inf 100.0%
fabs-neg100.0%
mul-1-neg100.0%
sub-neg100.0%
fabs-sub100.0%
fabs-div100.0%
rem-square-sqrt48.9%
fabs-sqr48.9%
rem-square-sqrt49.4%
div-sub49.4%
sub-neg49.4%
*-inverses49.4%
metadata-eval49.4%
+-commutative49.4%
Simplified49.4%
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-sqrt48.8%
fabs-sqr48.8%
add-sqr-sqrt49.6%
*-commutative49.6%
add-sqr-sqrt13.6%
fabs-sqr13.6%
add-sqr-sqrt29.7%
Applied egg-rr29.7%
Taylor expanded in y around 0 30.4%
Final simplification30.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.8%
fabs-sqr48.8%
add-sqr-sqrt49.6%
*-commutative49.6%
add-sqr-sqrt13.6%
fabs-sqr13.6%
add-sqr-sqrt29.7%
Applied egg-rr29.7%
Taylor expanded in y around inf 1.3%
Final simplification1.3%
herbie shell --seed 2023290
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))