
(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 x)))) (t_1 (/ y (+ x y))))
(if (<= x -2.25e+92)
t_0
(if (<= x -2500.0)
t_1
(if (<= x -1.9e-37) t_0 (if (<= x 7.5e+29) t_1 (/ x y)))))))
double code(double x, double y) {
double t_0 = x * ((x / y) / (y - x));
double t_1 = y / (x + y);
double tmp;
if (x <= -2.25e+92) {
tmp = t_0;
} else if (x <= -2500.0) {
tmp = t_1;
} else if (x <= -1.9e-37) {
tmp = t_0;
} else if (x <= 7.5e+29) {
tmp = t_1;
} else {
tmp = x / y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = x * ((x / y) / (y - x))
t_1 = y / (x + y)
if (x <= (-2.25d+92)) then
tmp = t_0
else if (x <= (-2500.0d0)) then
tmp = t_1
else if (x <= (-1.9d-37)) then
tmp = t_0
else if (x <= 7.5d+29) then
tmp = t_1
else
tmp = x / y
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = x * ((x / y) / (y - x));
double t_1 = y / (x + y);
double tmp;
if (x <= -2.25e+92) {
tmp = t_0;
} else if (x <= -2500.0) {
tmp = t_1;
} else if (x <= -1.9e-37) {
tmp = t_0;
} else if (x <= 7.5e+29) {
tmp = t_1;
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): t_0 = x * ((x / y) / (y - x)) t_1 = y / (x + y) tmp = 0 if x <= -2.25e+92: tmp = t_0 elif x <= -2500.0: tmp = t_1 elif x <= -1.9e-37: tmp = t_0 elif x <= 7.5e+29: tmp = t_1 else: tmp = x / y return tmp
function code(x, y) t_0 = Float64(x * Float64(Float64(x / y) / Float64(y - x))) t_1 = Float64(y / Float64(x + y)) tmp = 0.0 if (x <= -2.25e+92) tmp = t_0; elseif (x <= -2500.0) tmp = t_1; elseif (x <= -1.9e-37) tmp = t_0; elseif (x <= 7.5e+29) tmp = t_1; else tmp = Float64(x / y); end return tmp end
function tmp_2 = code(x, y) t_0 = x * ((x / y) / (y - x)); t_1 = y / (x + y); tmp = 0.0; if (x <= -2.25e+92) tmp = t_0; elseif (x <= -2500.0) tmp = t_1; elseif (x <= -1.9e-37) tmp = t_0; elseif (x <= 7.5e+29) tmp = t_1; else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(x * N[(N[(x / y), $MachinePrecision] / N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -2.25e+92], t$95$0, If[LessEqual[x, -2500.0], t$95$1, If[LessEqual[x, -1.9e-37], t$95$0, If[LessEqual[x, 7.5e+29], t$95$1, N[(x / y), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \frac{\frac{x}{y}}{y - x}\\
t_1 := \frac{y}{x + y}\\
\mathbf{if}\;x \leq -2.25 \cdot 10^{+92}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \leq -2500:\\
\;\;\;\;t_1\\
\mathbf{elif}\;x \leq -1.9 \cdot 10^{-37}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \leq 7.5 \cdot 10^{+29}:\\
\;\;\;\;t_1\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < -2.25e92 or -2500 < x < -1.9000000000000002e-37Initial program 100.0%
div-inv99.7%
add-sqr-sqrt8.8%
fabs-sqr8.8%
add-sqr-sqrt9.3%
*-commutative9.3%
add-sqr-sqrt0.3%
fabs-sqr0.3%
add-sqr-sqrt30.7%
Applied egg-rr30.7%
flip--18.4%
associate-*r/15.1%
+-commutative15.1%
Applied egg-rr15.1%
Taylor expanded in y around 0 15.8%
unpow215.8%
associate-/l*16.2%
associate-/r/16.2%
Simplified16.2%
associate-*l/15.8%
*-un-lft-identity15.8%
associate-*l/15.8%
frac-2neg15.8%
div-inv15.8%
associate-*l/15.8%
*-un-lft-identity15.8%
distribute-neg-frac15.8%
add-sqr-sqrt0.4%
sqrt-unprod0.3%
sqr-neg0.3%
sqrt-unprod0.1%
add-sqr-sqrt33.8%
frac-2neg33.8%
associate-*r/35.8%
distribute-neg-in35.8%
add-sqr-sqrt0.3%
sqrt-unprod34.0%
sqr-neg34.0%
sqrt-unprod35.6%
add-sqr-sqrt36.3%
sub-neg36.3%
Applied egg-rr36.3%
associate-*l*52.7%
associate-*r/52.8%
*-rgt-identity52.8%
Simplified52.8%
if -2.25e92 < x < -2500 or -1.9000000000000002e-37 < x < 7.49999999999999945e29Initial program 100.0%
div-inv99.6%
add-sqr-sqrt44.3%
fabs-sqr44.3%
add-sqr-sqrt45.2%
*-commutative45.2%
add-sqr-sqrt4.0%
fabs-sqr4.0%
add-sqr-sqrt13.3%
Applied egg-rr13.3%
flip--10.5%
associate-*r/10.4%
+-commutative10.4%
Applied egg-rr10.4%
Taylor expanded in x around 0 1.8%
unpow21.8%
mul-1-neg1.8%
distribute-rgt-neg-out1.8%
Simplified1.8%
associate-*r*2.1%
lft-mult-inverse2.1%
*-un-lft-identity2.1%
neg-sub02.1%
metadata-eval2.1%
sub-neg2.1%
metadata-eval2.1%
add-sqr-sqrt1.0%
sqrt-unprod23.6%
sqr-neg23.6%
sqrt-unprod37.4%
add-sqr-sqrt73.9%
Applied egg-rr73.9%
+-lft-identity73.9%
Simplified73.9%
if 7.49999999999999945e29 < x Initial program 100.0%
div-inv99.7%
add-sqr-sqrt83.4%
fabs-sqr83.4%
add-sqr-sqrt84.0%
*-commutative84.0%
add-sqr-sqrt48.0%
fabs-sqr48.0%
add-sqr-sqrt48.3%
Applied egg-rr48.3%
Taylor expanded in y around 0 48.6%
Final simplification64.4%
(FPCore (x y) :precision binary64 (if (<= x -2e+176) (/ x y) (if (<= x 3.95e+30) (/ y (+ x y)) (/ x y))))
double code(double x, double y) {
double tmp;
if (x <= -2e+176) {
tmp = x / y;
} else if (x <= 3.95e+30) {
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 <= (-2d+176)) then
tmp = x / y
else if (x <= 3.95d+30) 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 <= -2e+176) {
tmp = x / y;
} else if (x <= 3.95e+30) {
tmp = y / (x + y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2e+176: tmp = x / y elif x <= 3.95e+30: tmp = y / (x + y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (x <= -2e+176) tmp = Float64(x / y); elseif (x <= 3.95e+30) 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 <= -2e+176) tmp = x / y; elseif (x <= 3.95e+30) tmp = y / (x + y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2e+176], N[(x / y), $MachinePrecision], If[LessEqual[x, 3.95e+30], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2 \cdot 10^{+176}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;x \leq 3.95 \cdot 10^{+30}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < -2e176 or 3.94999999999999981e30 < x Initial program 100.0%
div-inv99.7%
add-sqr-sqrt57.3%
fabs-sqr57.3%
add-sqr-sqrt57.8%
*-commutative57.8%
add-sqr-sqrt30.8%
fabs-sqr30.8%
add-sqr-sqrt46.5%
Applied egg-rr46.5%
Taylor expanded in y around 0 47.0%
if -2e176 < x < 3.94999999999999981e30Initial program 100.0%
div-inv99.7%
add-sqr-sqrt38.4%
fabs-sqr38.4%
add-sqr-sqrt39.3%
*-commutative39.3%
add-sqr-sqrt3.4%
fabs-sqr3.4%
add-sqr-sqrt14.1%
Applied egg-rr14.1%
flip--11.7%
associate-*r/10.6%
+-commutative10.6%
Applied egg-rr10.6%
Taylor expanded in x around 0 1.8%
unpow21.8%
mul-1-neg1.8%
distribute-rgt-neg-out1.8%
Simplified1.8%
associate-*r*2.1%
lft-mult-inverse2.1%
*-un-lft-identity2.1%
neg-sub02.1%
metadata-eval2.1%
sub-neg2.1%
metadata-eval2.1%
add-sqr-sqrt0.9%
sqrt-unprod20.2%
sqr-neg20.2%
sqrt-unprod33.9%
add-sqr-sqrt65.8%
Applied egg-rr65.8%
+-lft-identity65.8%
Simplified65.8%
Final simplification60.0%
(FPCore (x y) :precision binary64 (if (<= x -7e+115) (/ (* x x) (* y y)) (if (<= x 3.2e+30) (/ y (+ x y)) (/ x y))))
double code(double x, double y) {
double tmp;
if (x <= -7e+115) {
tmp = (x * x) / (y * y);
} else if (x <= 3.2e+30) {
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 <= (-7d+115)) then
tmp = (x * x) / (y * y)
else if (x <= 3.2d+30) 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 <= -7e+115) {
tmp = (x * x) / (y * y);
} else if (x <= 3.2e+30) {
tmp = y / (x + y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -7e+115: tmp = (x * x) / (y * y) elif x <= 3.2e+30: tmp = y / (x + y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (x <= -7e+115) tmp = Float64(Float64(x * x) / Float64(y * y)); elseif (x <= 3.2e+30) 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 <= -7e+115) tmp = (x * x) / (y * y); elseif (x <= 3.2e+30) tmp = y / (x + y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -7e+115], N[(N[(x * x), $MachinePrecision] / N[(y * y), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 3.2e+30], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7 \cdot 10^{+115}:\\
\;\;\;\;\frac{x \cdot x}{y \cdot y}\\
\mathbf{elif}\;x \leq 3.2 \cdot 10^{+30}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < -7.00000000000000011e115Initial program 100.0%
div-inv99.7%
add-sqr-sqrt11.5%
fabs-sqr11.5%
add-sqr-sqrt11.9%
*-commutative11.9%
add-sqr-sqrt0.2%
fabs-sqr0.2%
add-sqr-sqrt32.9%
Applied egg-rr32.9%
flip--16.9%
associate-*r/14.7%
+-commutative14.7%
Applied egg-rr14.7%
Taylor expanded in y around 0 15.5%
unpow215.5%
associate-/l*16.0%
associate-/r/16.0%
Simplified16.0%
Taylor expanded in x around 0 41.9%
unpow241.9%
unpow241.9%
Simplified41.9%
if -7.00000000000000011e115 < x < 3.19999999999999973e30Initial program 100.0%
div-inv99.6%
add-sqr-sqrt40.7%
fabs-sqr40.7%
add-sqr-sqrt41.7%
*-commutative41.7%
add-sqr-sqrt3.7%
fabs-sqr3.7%
add-sqr-sqrt14.1%
Applied egg-rr14.1%
flip--11.5%
associate-*r/10.9%
+-commutative10.9%
Applied egg-rr10.9%
Taylor expanded in x around 0 1.8%
unpow21.8%
mul-1-neg1.8%
distribute-rgt-neg-out1.8%
Simplified1.8%
associate-*r*2.1%
lft-mult-inverse2.1%
*-un-lft-identity2.1%
neg-sub02.1%
metadata-eval2.1%
sub-neg2.1%
metadata-eval2.1%
add-sqr-sqrt0.9%
sqrt-unprod21.9%
sqr-neg21.9%
sqrt-unprod35.7%
add-sqr-sqrt69.4%
Applied egg-rr69.4%
+-lft-identity69.4%
Simplified69.4%
if 3.19999999999999973e30 < x Initial program 100.0%
div-inv99.7%
add-sqr-sqrt83.4%
fabs-sqr83.4%
add-sqr-sqrt84.0%
*-commutative84.0%
add-sqr-sqrt48.0%
fabs-sqr48.0%
add-sqr-sqrt48.3%
Applied egg-rr48.3%
Taylor expanded in y around 0 48.6%
Final simplification60.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-sqrt44.2%
fabs-sqr44.2%
add-sqr-sqrt45.0%
*-commutative45.0%
add-sqr-sqrt11.8%
fabs-sqr11.8%
add-sqr-sqrt23.9%
Applied egg-rr23.9%
Taylor expanded in y around 0 24.9%
Final simplification24.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-sqrt44.2%
fabs-sqr44.2%
add-sqr-sqrt45.0%
*-commutative45.0%
add-sqr-sqrt11.8%
fabs-sqr11.8%
add-sqr-sqrt23.9%
Applied egg-rr23.9%
Taylor expanded in y around inf 1.3%
Final simplification1.3%
herbie shell --seed 2023240
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))