
(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 (<= x -8.2e+52)
(* x (/ (/ x y) (- y x)))
(if (<= x -2.15e-10)
t_0
(if (<= x -1.6e-31)
(/ (* x (/ x y)) (+ x y))
(if (<= x 2.4e+162) t_0 (+ (/ x y) -1.0)))))))
double code(double x, double y) {
double t_0 = y / (x + y);
double tmp;
if (x <= -8.2e+52) {
tmp = x * ((x / y) / (y - x));
} else if (x <= -2.15e-10) {
tmp = t_0;
} else if (x <= -1.6e-31) {
tmp = (x * (x / y)) / (x + y);
} else if (x <= 2.4e+162) {
tmp = t_0;
} 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) :: t_0
real(8) :: tmp
t_0 = y / (x + y)
if (x <= (-8.2d+52)) then
tmp = x * ((x / y) / (y - x))
else if (x <= (-2.15d-10)) then
tmp = t_0
else if (x <= (-1.6d-31)) then
tmp = (x * (x / y)) / (x + y)
else if (x <= 2.4d+162) then
tmp = t_0
else
tmp = (x / y) + (-1.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = y / (x + y);
double tmp;
if (x <= -8.2e+52) {
tmp = x * ((x / y) / (y - x));
} else if (x <= -2.15e-10) {
tmp = t_0;
} else if (x <= -1.6e-31) {
tmp = (x * (x / y)) / (x + y);
} else if (x <= 2.4e+162) {
tmp = t_0;
} else {
tmp = (x / y) + -1.0;
}
return tmp;
}
def code(x, y): t_0 = y / (x + y) tmp = 0 if x <= -8.2e+52: tmp = x * ((x / y) / (y - x)) elif x <= -2.15e-10: tmp = t_0 elif x <= -1.6e-31: tmp = (x * (x / y)) / (x + y) elif x <= 2.4e+162: tmp = t_0 else: tmp = (x / y) + -1.0 return tmp
function code(x, y) t_0 = Float64(y / Float64(x + y)) tmp = 0.0 if (x <= -8.2e+52) tmp = Float64(x * Float64(Float64(x / y) / Float64(y - x))); elseif (x <= -2.15e-10) tmp = t_0; elseif (x <= -1.6e-31) tmp = Float64(Float64(x * Float64(x / y)) / Float64(x + y)); elseif (x <= 2.4e+162) tmp = t_0; else tmp = Float64(Float64(x / y) + -1.0); end return tmp end
function tmp_2 = code(x, y) t_0 = y / (x + y); tmp = 0.0; if (x <= -8.2e+52) tmp = x * ((x / y) / (y - x)); elseif (x <= -2.15e-10) tmp = t_0; elseif (x <= -1.6e-31) tmp = (x * (x / y)) / (x + y); elseif (x <= 2.4e+162) tmp = t_0; else tmp = (x / y) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -8.2e+52], N[(x * N[(N[(x / y), $MachinePrecision] / N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, -2.15e-10], t$95$0, If[LessEqual[x, -1.6e-31], N[(N[(x * N[(x / y), $MachinePrecision]), $MachinePrecision] / N[(x + y), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 2.4e+162], t$95$0, N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{y}{x + y}\\
\mathbf{if}\;x \leq -8.2 \cdot 10^{+52}:\\
\;\;\;\;x \cdot \frac{\frac{x}{y}}{y - x}\\
\mathbf{elif}\;x \leq -2.15 \cdot 10^{-10}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \leq -1.6 \cdot 10^{-31}:\\
\;\;\;\;\frac{x \cdot \frac{x}{y}}{x + y}\\
\mathbf{elif}\;x \leq 2.4 \cdot 10^{+162}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y} + -1\\
\end{array}
\end{array}
if x < -8.1999999999999999e52Initial program 100.0%
div-inv99.7%
add-sqr-sqrt3.9%
fabs-sqr3.9%
add-sqr-sqrt4.4%
*-commutative4.4%
add-sqr-sqrt0.4%
fabs-sqr0.4%
add-sqr-sqrt37.5%
Applied egg-rr37.5%
flip--22.7%
associate-*r/17.1%
+-commutative17.1%
Applied egg-rr17.1%
Taylor expanded in y around 0 17.5%
unpow217.5%
associate-/l*21.4%
associate-/r/21.4%
Simplified21.4%
associate-*l/17.5%
*-un-lft-identity17.5%
associate-*l/17.5%
frac-2neg17.5%
div-inv17.5%
associate-*l/17.5%
*-un-lft-identity17.5%
distribute-neg-frac17.5%
add-sqr-sqrt0.3%
sqrt-unprod0.4%
sqr-neg0.4%
sqrt-unprod0.2%
add-sqr-sqrt21.9%
frac-2neg21.9%
associate-*r/27.5%
distribute-neg-in27.5%
add-sqr-sqrt0.2%
sqrt-unprod25.7%
sqr-neg25.7%
sqrt-unprod27.4%
add-sqr-sqrt27.7%
sub-neg27.7%
Applied egg-rr27.7%
associate-*l*53.2%
associate-*r/53.3%
*-rgt-identity53.3%
Simplified53.3%
if -8.1999999999999999e52 < x < -2.15000000000000007e-10 or -1.60000000000000009e-31 < x < 2.40000000000000009e162Initial program 100.0%
div-inv99.7%
add-sqr-sqrt57.2%
fabs-sqr57.2%
add-sqr-sqrt58.1%
*-commutative58.1%
add-sqr-sqrt11.8%
fabs-sqr11.8%
add-sqr-sqrt18.4%
Applied egg-rr18.4%
flip--12.4%
associate-*r/10.7%
+-commutative10.7%
Applied egg-rr10.7%
Taylor expanded in x around 0 1.6%
unpow21.6%
mul-1-neg1.6%
distribute-rgt-neg-out1.6%
Simplified1.6%
associate-*r*1.8%
lft-mult-inverse1.8%
*-un-lft-identity1.8%
neg-sub01.8%
metadata-eval1.8%
sub-neg1.8%
metadata-eval1.8%
add-sqr-sqrt1.0%
sqrt-unprod17.9%
sqr-neg17.9%
sqrt-unprod34.2%
add-sqr-sqrt70.7%
Applied egg-rr70.7%
+-lft-identity70.7%
Simplified70.7%
if -2.15000000000000007e-10 < x < -1.60000000000000009e-31Initial program 99.8%
div-inv99.8%
add-sqr-sqrt0.0%
fabs-sqr0.0%
add-sqr-sqrt1.0%
*-commutative1.0%
add-sqr-sqrt0.4%
fabs-sqr0.4%
add-sqr-sqrt75.2%
Applied egg-rr75.2%
flip--75.1%
associate-*r/74.8%
+-commutative74.8%
Applied egg-rr74.8%
Taylor expanded in y around 0 74.9%
unpow274.9%
associate-/l*75.1%
associate-/r/75.4%
Simplified75.4%
if 2.40000000000000009e162 < x Initial 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.1%
fabs-sqr48.1%
rem-square-sqrt48.7%
div-sub48.7%
sub-neg48.7%
*-inverses48.7%
metadata-eval48.7%
+-commutative48.7%
Simplified48.7%
Final simplification64.9%
(FPCore (x y)
:precision binary64
(if (<= x -8.5e+52)
(* x (/ (/ x y) (- y x)))
(if (or (<= x -9.5e-11) (and (not (<= x -1.65e-31)) (<= x 5.4e+163)))
(/ y (+ x y))
(+ (/ x y) -1.0))))
double code(double x, double y) {
double tmp;
if (x <= -8.5e+52) {
tmp = x * ((x / y) / (y - x));
} else if ((x <= -9.5e-11) || (!(x <= -1.65e-31) && (x <= 5.4e+163))) {
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 (x <= (-8.5d+52)) then
tmp = x * ((x / y) / (y - x))
else if ((x <= (-9.5d-11)) .or. (.not. (x <= (-1.65d-31))) .and. (x <= 5.4d+163)) 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 (x <= -8.5e+52) {
tmp = x * ((x / y) / (y - x));
} else if ((x <= -9.5e-11) || (!(x <= -1.65e-31) && (x <= 5.4e+163))) {
tmp = y / (x + y);
} else {
tmp = (x / y) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -8.5e+52: tmp = x * ((x / y) / (y - x)) elif (x <= -9.5e-11) or (not (x <= -1.65e-31) and (x <= 5.4e+163)): tmp = y / (x + y) else: tmp = (x / y) + -1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= -8.5e+52) tmp = Float64(x * Float64(Float64(x / y) / Float64(y - x))); elseif ((x <= -9.5e-11) || (!(x <= -1.65e-31) && (x <= 5.4e+163))) 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 (x <= -8.5e+52) tmp = x * ((x / y) / (y - x)); elseif ((x <= -9.5e-11) || (~((x <= -1.65e-31)) && (x <= 5.4e+163))) tmp = y / (x + y); else tmp = (x / y) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -8.5e+52], N[(x * N[(N[(x / y), $MachinePrecision] / N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[x, -9.5e-11], And[N[Not[LessEqual[x, -1.65e-31]], $MachinePrecision], LessEqual[x, 5.4e+163]]], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -8.5 \cdot 10^{+52}:\\
\;\;\;\;x \cdot \frac{\frac{x}{y}}{y - x}\\
\mathbf{elif}\;x \leq -9.5 \cdot 10^{-11} \lor \neg \left(x \leq -1.65 \cdot 10^{-31}\right) \land x \leq 5.4 \cdot 10^{+163}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y} + -1\\
\end{array}
\end{array}
if x < -8.49999999999999994e52Initial program 100.0%
div-inv99.7%
add-sqr-sqrt3.9%
fabs-sqr3.9%
add-sqr-sqrt4.4%
*-commutative4.4%
add-sqr-sqrt0.4%
fabs-sqr0.4%
add-sqr-sqrt37.5%
Applied egg-rr37.5%
flip--22.7%
associate-*r/17.1%
+-commutative17.1%
Applied egg-rr17.1%
Taylor expanded in y around 0 17.5%
unpow217.5%
associate-/l*21.4%
associate-/r/21.4%
Simplified21.4%
associate-*l/17.5%
*-un-lft-identity17.5%
associate-*l/17.5%
frac-2neg17.5%
div-inv17.5%
associate-*l/17.5%
*-un-lft-identity17.5%
distribute-neg-frac17.5%
add-sqr-sqrt0.3%
sqrt-unprod0.4%
sqr-neg0.4%
sqrt-unprod0.2%
add-sqr-sqrt21.9%
frac-2neg21.9%
associate-*r/27.5%
distribute-neg-in27.5%
add-sqr-sqrt0.2%
sqrt-unprod25.7%
sqr-neg25.7%
sqrt-unprod27.4%
add-sqr-sqrt27.7%
sub-neg27.7%
Applied egg-rr27.7%
associate-*l*53.2%
associate-*r/53.3%
*-rgt-identity53.3%
Simplified53.3%
if -8.49999999999999994e52 < x < -9.49999999999999951e-11 or -1.65e-31 < x < 5.39999999999999998e163Initial program 100.0%
div-inv99.7%
add-sqr-sqrt57.2%
fabs-sqr57.2%
add-sqr-sqrt58.1%
*-commutative58.1%
add-sqr-sqrt11.8%
fabs-sqr11.8%
add-sqr-sqrt18.4%
Applied egg-rr18.4%
flip--12.4%
associate-*r/10.7%
+-commutative10.7%
Applied egg-rr10.7%
Taylor expanded in x around 0 1.6%
unpow21.6%
mul-1-neg1.6%
distribute-rgt-neg-out1.6%
Simplified1.6%
associate-*r*1.8%
lft-mult-inverse1.8%
*-un-lft-identity1.8%
neg-sub01.8%
metadata-eval1.8%
sub-neg1.8%
metadata-eval1.8%
add-sqr-sqrt1.0%
sqrt-unprod17.9%
sqr-neg17.9%
sqrt-unprod34.2%
add-sqr-sqrt70.7%
Applied egg-rr70.7%
+-lft-identity70.7%
Simplified70.7%
if -9.49999999999999951e-11 < x < -1.65e-31 or 5.39999999999999998e163 < x Initial 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.7%
fabs-sqr53.7%
rem-square-sqrt54.4%
div-sub54.5%
sub-neg54.5%
*-inverses54.5%
metadata-eval54.5%
+-commutative54.5%
Simplified54.5%
Final simplification64.9%
(FPCore (x y) :precision binary64 (if (or (<= y -1.8e-39) (not (<= y 1.5e-12))) (/ y (+ x y)) (+ (/ x y) -1.0)))
double code(double x, double y) {
double tmp;
if ((y <= -1.8e-39) || !(y <= 1.5e-12)) {
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 <= (-1.8d-39)) .or. (.not. (y <= 1.5d-12))) 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 <= -1.8e-39) || !(y <= 1.5e-12)) {
tmp = y / (x + y);
} else {
tmp = (x / y) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -1.8e-39) or not (y <= 1.5e-12): tmp = y / (x + y) else: tmp = (x / y) + -1.0 return tmp
function code(x, y) tmp = 0.0 if ((y <= -1.8e-39) || !(y <= 1.5e-12)) 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 <= -1.8e-39) || ~((y <= 1.5e-12))) tmp = y / (x + y); else tmp = (x / y) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -1.8e-39], N[Not[LessEqual[y, 1.5e-12]], $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 -1.8 \cdot 10^{-39} \lor \neg \left(y \leq 1.5 \cdot 10^{-12}\right):\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y} + -1\\
\end{array}
\end{array}
if y < -1.8e-39 or 1.5000000000000001e-12 < y Initial program 100.0%
div-inv99.7%
add-sqr-sqrt51.2%
fabs-sqr51.2%
add-sqr-sqrt52.2%
*-commutative52.2%
add-sqr-sqrt6.1%
fabs-sqr6.1%
add-sqr-sqrt13.8%
Applied egg-rr13.8%
flip--4.7%
associate-*r/4.7%
+-commutative4.7%
Applied egg-rr4.7%
Taylor expanded in x around 0 1.5%
unpow21.5%
mul-1-neg1.5%
distribute-rgt-neg-out1.5%
Simplified1.5%
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-unprod15.7%
sqr-neg15.7%
sqrt-unprod33.7%
add-sqr-sqrt73.6%
Applied egg-rr73.6%
+-lft-identity73.6%
Simplified73.6%
if -1.8e-39 < y < 1.5000000000000001e-12Initial 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-sqrt44.0%
fabs-sqr44.0%
rem-square-sqrt44.7%
div-sub44.7%
sub-neg44.7%
*-inverses44.7%
metadata-eval44.7%
+-commutative44.7%
Simplified44.7%
Final simplification60.8%
(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-sqrt49.6%
fabs-sqr49.6%
add-sqr-sqrt50.4%
*-commutative50.4%
add-sqr-sqrt13.2%
fabs-sqr13.2%
add-sqr-sqrt27.4%
Applied egg-rr27.4%
Taylor expanded in y around 0 27.8%
Final simplification27.8%
(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-sqrt49.6%
fabs-sqr49.6%
add-sqr-sqrt50.4%
*-commutative50.4%
add-sqr-sqrt13.2%
fabs-sqr13.2%
add-sqr-sqrt27.4%
Applied egg-rr27.4%
Taylor expanded in y around inf 1.3%
Final simplification1.3%
herbie shell --seed 2023297
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))