
(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 (/ (* x x) (* y y))))
(if (<= x -8.2e+172)
t_0
(if (<= x 6.8e+43)
(/ y (+ x y))
(if (<= x 2.1e+165) (+ (/ x y) -1.0) t_0)))))
double code(double x, double y) {
double t_0 = (x * x) / (y * y);
double tmp;
if (x <= -8.2e+172) {
tmp = t_0;
} else if (x <= 6.8e+43) {
tmp = y / (x + y);
} else if (x <= 2.1e+165) {
tmp = (x / y) + -1.0;
} 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 = (x * x) / (y * y)
if (x <= (-8.2d+172)) then
tmp = t_0
else if (x <= 6.8d+43) then
tmp = y / (x + y)
else if (x <= 2.1d+165) then
tmp = (x / y) + (-1.0d0)
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = (x * x) / (y * y);
double tmp;
if (x <= -8.2e+172) {
tmp = t_0;
} else if (x <= 6.8e+43) {
tmp = y / (x + y);
} else if (x <= 2.1e+165) {
tmp = (x / y) + -1.0;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = (x * x) / (y * y) tmp = 0 if x <= -8.2e+172: tmp = t_0 elif x <= 6.8e+43: tmp = y / (x + y) elif x <= 2.1e+165: tmp = (x / y) + -1.0 else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(Float64(x * x) / Float64(y * y)) tmp = 0.0 if (x <= -8.2e+172) tmp = t_0; elseif (x <= 6.8e+43) tmp = Float64(y / Float64(x + y)); elseif (x <= 2.1e+165) tmp = Float64(Float64(x / y) + -1.0); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = (x * x) / (y * y); tmp = 0.0; if (x <= -8.2e+172) tmp = t_0; elseif (x <= 6.8e+43) tmp = y / (x + y); elseif (x <= 2.1e+165) tmp = (x / y) + -1.0; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(N[(x * x), $MachinePrecision] / N[(y * y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -8.2e+172], t$95$0, If[LessEqual[x, 6.8e+43], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 2.1e+165], N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x \cdot x}{y \cdot y}\\
\mathbf{if}\;x \leq -8.2 \cdot 10^{+172}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \leq 6.8 \cdot 10^{+43}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{elif}\;x \leq 2.1 \cdot 10^{+165}:\\
\;\;\;\;\frac{x}{y} + -1\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if x < -8.200000000000001e172 or 2.1000000000000001e165 < x Initial program 100.0%
div-inv99.8%
add-sqr-sqrt55.2%
fabs-sqr55.2%
add-sqr-sqrt55.6%
*-commutative55.6%
add-sqr-sqrt21.7%
fabs-sqr21.7%
add-sqr-sqrt37.2%
Applied egg-rr37.2%
flip--17.6%
associate-*r/17.6%
+-commutative17.6%
Applied egg-rr17.6%
Taylor expanded in y around 0 18.1%
unpow218.1%
associate-/l*21.4%
associate-/r/21.5%
Simplified21.5%
Taylor expanded in x around 0 46.3%
unpow246.3%
unpow246.3%
Simplified46.3%
if -8.200000000000001e172 < x < 6.80000000000000024e43Initial program 100.0%
div-inv99.8%
add-sqr-sqrt49.6%
fabs-sqr49.6%
add-sqr-sqrt50.6%
*-commutative50.6%
add-sqr-sqrt4.9%
fabs-sqr4.9%
add-sqr-sqrt12.5%
Applied egg-rr12.5%
flip--9.4%
associate-*r/9.4%
+-commutative9.4%
Applied egg-rr9.4%
Taylor expanded in x around 0 1.9%
unpow21.9%
mul-1-neg1.9%
distribute-rgt-neg-out1.9%
Simplified1.9%
associate-*r*2.3%
lft-mult-inverse2.3%
*-un-lft-identity2.3%
neg-sub02.3%
metadata-eval2.3%
sub-neg2.3%
metadata-eval2.3%
add-sqr-sqrt1.2%
sqrt-unprod21.4%
sqr-neg21.4%
sqrt-unprod31.3%
add-sqr-sqrt68.5%
Applied egg-rr68.5%
+-lft-identity68.5%
Simplified68.5%
if 6.80000000000000024e43 < x < 2.1000000000000001e165Initial 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.9%
div-sub48.9%
sub-neg48.9%
*-inverses48.9%
metadata-eval48.9%
+-commutative48.9%
Simplified48.9%
Final simplification60.5%
(FPCore (x y) :precision binary64 (if (or (<= x -1.3e+176) (not (<= x 2.05e+87))) (* x (/ (/ x y) (- y x))) (/ y (+ x y))))
double code(double x, double y) {
double tmp;
if ((x <= -1.3e+176) || !(x <= 2.05e+87)) {
tmp = x * ((x / y) / (y - x));
} else {
tmp = 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 ((x <= (-1.3d+176)) .or. (.not. (x <= 2.05d+87))) then
tmp = x * ((x / y) / (y - x))
else
tmp = y / (x + y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -1.3e+176) || !(x <= 2.05e+87)) {
tmp = x * ((x / y) / (y - x));
} else {
tmp = y / (x + y);
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -1.3e+176) or not (x <= 2.05e+87): tmp = x * ((x / y) / (y - x)) else: tmp = y / (x + y) return tmp
function code(x, y) tmp = 0.0 if ((x <= -1.3e+176) || !(x <= 2.05e+87)) tmp = Float64(x * Float64(Float64(x / y) / Float64(y - x))); else tmp = Float64(y / Float64(x + y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -1.3e+176) || ~((x <= 2.05e+87))) tmp = x * ((x / y) / (y - x)); else tmp = y / (x + y); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -1.3e+176], N[Not[LessEqual[x, 2.05e+87]], $MachinePrecision]], N[(x * N[(N[(x / y), $MachinePrecision] / N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.3 \cdot 10^{+176} \lor \neg \left(x \leq 2.05 \cdot 10^{+87}\right):\\
\;\;\;\;x \cdot \frac{\frac{x}{y}}{y - x}\\
\mathbf{else}:\\
\;\;\;\;\frac{y}{x + y}\\
\end{array}
\end{array}
if x < -1.29999999999999995e176 or 2.05e87 < x Initial program 100.0%
div-inv99.8%
add-sqr-sqrt64.4%
fabs-sqr64.4%
add-sqr-sqrt65.0%
*-commutative65.0%
add-sqr-sqrt27.4%
fabs-sqr27.4%
add-sqr-sqrt37.8%
Applied egg-rr37.8%
flip--20.1%
associate-*r/19.1%
+-commutative19.1%
Applied egg-rr19.1%
Taylor expanded in y around 0 19.3%
unpow219.3%
associate-/l*21.8%
associate-/r/21.8%
Simplified21.8%
associate-*l/19.3%
*-un-lft-identity19.3%
associate-*l/19.3%
frac-2neg19.3%
div-inv19.3%
associate-*l/19.3%
*-un-lft-identity19.3%
distribute-neg-frac19.3%
add-sqr-sqrt15.5%
sqrt-unprod34.3%
sqr-neg34.3%
sqrt-unprod19.3%
add-sqr-sqrt27.8%
frac-2neg27.8%
associate-*r/34.1%
distribute-neg-in34.1%
add-sqr-sqrt21.5%
sqrt-unprod31.2%
sqr-neg31.2%
sqrt-unprod12.7%
add-sqr-sqrt34.1%
sub-neg34.1%
Applied egg-rr34.1%
associate-*l*52.1%
associate-*r/52.2%
*-rgt-identity52.2%
Simplified52.2%
if -1.29999999999999995e176 < x < 2.05e87Initial program 100.0%
div-inv99.8%
add-sqr-sqrt50.7%
fabs-sqr50.7%
add-sqr-sqrt51.7%
*-commutative51.7%
add-sqr-sqrt7.7%
fabs-sqr7.7%
add-sqr-sqrt15.5%
Applied egg-rr15.5%
flip--12.5%
associate-*r/12.5%
+-commutative12.5%
Applied egg-rr12.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.3%
lft-mult-inverse2.3%
*-un-lft-identity2.3%
neg-sub02.3%
metadata-eval2.3%
sub-neg2.3%
metadata-eval2.3%
add-sqr-sqrt1.1%
sqrt-unprod20.5%
sqr-neg20.5%
sqrt-unprod30.5%
add-sqr-sqrt66.6%
Applied egg-rr66.6%
+-lft-identity66.6%
Simplified66.6%
Final simplification61.6%
(FPCore (x y) :precision binary64 (if (<= x -6.1e+172) (/ x y) (if (<= x 6.1e+45) (/ y (+ x y)) (+ (/ x y) -1.0))))
double code(double x, double y) {
double tmp;
if (x <= -6.1e+172) {
tmp = x / y;
} else if (x <= 6.1e+45) {
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 <= (-6.1d+172)) then
tmp = x / y
else if (x <= 6.1d+45) 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 <= -6.1e+172) {
tmp = x / y;
} else if (x <= 6.1e+45) {
tmp = y / (x + y);
} else {
tmp = (x / y) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -6.1e+172: tmp = x / y elif x <= 6.1e+45: tmp = y / (x + y) else: tmp = (x / y) + -1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= -6.1e+172) tmp = Float64(x / y); elseif (x <= 6.1e+45) 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 <= -6.1e+172) tmp = x / y; elseif (x <= 6.1e+45) tmp = y / (x + y); else tmp = (x / y) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -6.1e+172], N[(x / y), $MachinePrecision], If[LessEqual[x, 6.1e+45], 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 -6.1 \cdot 10^{+172}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;x \leq 6.1 \cdot 10^{+45}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y} + -1\\
\end{array}
\end{array}
if x < -6.0999999999999998e172Initial program 99.9%
div-inv99.8%
add-sqr-sqrt0.0%
fabs-sqr0.0%
add-sqr-sqrt0.6%
*-commutative0.6%
add-sqr-sqrt0.4%
fabs-sqr0.4%
add-sqr-sqrt37.3%
Applied egg-rr37.3%
Taylor expanded in y around 0 37.5%
if -6.0999999999999998e172 < x < 6.1e45Initial program 100.0%
div-inv99.8%
add-sqr-sqrt49.6%
fabs-sqr49.6%
add-sqr-sqrt50.6%
*-commutative50.6%
add-sqr-sqrt4.9%
fabs-sqr4.9%
add-sqr-sqrt12.5%
Applied egg-rr12.5%
flip--9.4%
associate-*r/9.4%
+-commutative9.4%
Applied egg-rr9.4%
Taylor expanded in x around 0 1.9%
unpow21.9%
mul-1-neg1.9%
distribute-rgt-neg-out1.9%
Simplified1.9%
associate-*r*2.3%
lft-mult-inverse2.3%
*-un-lft-identity2.3%
neg-sub02.3%
metadata-eval2.3%
sub-neg2.3%
metadata-eval2.3%
add-sqr-sqrt1.2%
sqrt-unprod21.4%
sqr-neg21.4%
sqrt-unprod31.3%
add-sqr-sqrt68.5%
Applied egg-rr68.5%
+-lft-identity68.5%
Simplified68.5%
if 6.1e45 < 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-sqrt41.8%
fabs-sqr41.8%
rem-square-sqrt42.4%
div-sub42.4%
sub-neg42.4%
*-inverses42.4%
metadata-eval42.4%
+-commutative42.4%
Simplified42.4%
Final simplification58.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.8%
add-sqr-sqrt55.4%
fabs-sqr55.4%
add-sqr-sqrt56.3%
*-commutative56.3%
add-sqr-sqrt14.4%
fabs-sqr14.4%
add-sqr-sqrt23.1%
Applied egg-rr23.1%
Taylor expanded in y around 0 23.7%
Final simplification23.7%
(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.8%
add-sqr-sqrt55.4%
fabs-sqr55.4%
add-sqr-sqrt56.3%
*-commutative56.3%
add-sqr-sqrt14.4%
fabs-sqr14.4%
add-sqr-sqrt23.1%
Applied egg-rr23.1%
Taylor expanded in y around inf 1.3%
Final simplification1.3%
herbie shell --seed 2023185
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))