
(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.25e-78)
t_0
(if (<= y -1e-194)
(+ (/ x y) -1.0)
(if (<= y 1.16e-270)
(/ (* x x) (* y y))
(if (<= y 2.2e-59) (/ x y) t_0))))))
double code(double x, double y) {
double t_0 = y / (x + y);
double tmp;
if (y <= -1.25e-78) {
tmp = t_0;
} else if (y <= -1e-194) {
tmp = (x / y) + -1.0;
} else if (y <= 1.16e-270) {
tmp = (x * x) / (y * y);
} else if (y <= 2.2e-59) {
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.25d-78)) then
tmp = t_0
else if (y <= (-1d-194)) then
tmp = (x / y) + (-1.0d0)
else if (y <= 1.16d-270) then
tmp = (x * x) / (y * y)
else if (y <= 2.2d-59) 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.25e-78) {
tmp = t_0;
} else if (y <= -1e-194) {
tmp = (x / y) + -1.0;
} else if (y <= 1.16e-270) {
tmp = (x * x) / (y * y);
} else if (y <= 2.2e-59) {
tmp = x / y;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = y / (x + y) tmp = 0 if y <= -1.25e-78: tmp = t_0 elif y <= -1e-194: tmp = (x / y) + -1.0 elif y <= 1.16e-270: tmp = (x * x) / (y * y) elif y <= 2.2e-59: 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.25e-78) tmp = t_0; elseif (y <= -1e-194) tmp = Float64(Float64(x / y) + -1.0); elseif (y <= 1.16e-270) tmp = Float64(Float64(x * x) / Float64(y * y)); elseif (y <= 2.2e-59) 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.25e-78) tmp = t_0; elseif (y <= -1e-194) tmp = (x / y) + -1.0; elseif (y <= 1.16e-270) tmp = (x * x) / (y * y); elseif (y <= 2.2e-59) 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.25e-78], t$95$0, If[LessEqual[y, -1e-194], N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision], If[LessEqual[y, 1.16e-270], N[(N[(x * x), $MachinePrecision] / N[(y * y), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 2.2e-59], N[(x / y), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{y}{x + y}\\
\mathbf{if}\;y \leq -1.25 \cdot 10^{-78}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;y \leq -1 \cdot 10^{-194}:\\
\;\;\;\;\frac{x}{y} + -1\\
\mathbf{elif}\;y \leq 1.16 \cdot 10^{-270}:\\
\;\;\;\;\frac{x \cdot x}{y \cdot y}\\
\mathbf{elif}\;y \leq 2.2 \cdot 10^{-59}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if y < -1.2499999999999999e-78 or 2.1999999999999999e-59 < y Initial program 100.0%
div-inv99.7%
add-sqr-sqrt52.1%
fabs-sqr52.1%
add-sqr-sqrt53.1%
*-commutative53.1%
add-sqr-sqrt7.6%
fabs-sqr7.6%
add-sqr-sqrt12.8%
Applied egg-rr12.8%
flip--5.1%
associate-*r/5.1%
+-commutative5.1%
Applied egg-rr5.1%
Taylor expanded in x around 0 1.7%
unpow21.7%
mul-1-neg1.7%
distribute-rgt-neg-out1.7%
Simplified1.7%
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.1%
sqrt-unprod16.9%
sqr-neg16.9%
sqrt-unprod33.9%
add-sqr-sqrt71.5%
Applied egg-rr71.5%
+-lft-identity71.5%
Simplified71.5%
if -1.2499999999999999e-78 < y < -1.00000000000000002e-194Initial program 99.8%
Taylor expanded in x around -inf 99.8%
fabs-neg99.8%
mul-1-neg99.8%
sub-neg99.8%
fabs-sub99.8%
fabs-div99.8%
rem-square-sqrt66.1%
fabs-sqr66.1%
rem-square-sqrt67.0%
div-sub67.0%
sub-neg67.0%
*-inverses67.0%
metadata-eval67.0%
+-commutative67.0%
Simplified67.0%
if -1.00000000000000002e-194 < y < 1.16000000000000006e-270Initial program 100.0%
div-inv99.8%
add-sqr-sqrt58.1%
fabs-sqr58.1%
add-sqr-sqrt58.4%
*-commutative58.4%
add-sqr-sqrt11.2%
fabs-sqr11.2%
add-sqr-sqrt36.4%
Applied egg-rr36.4%
flip--28.6%
associate-*r/28.6%
+-commutative28.6%
Applied egg-rr28.6%
Taylor expanded in y around 0 28.6%
unpow228.6%
associate-/l*36.8%
associate-/r/36.8%
Simplified36.8%
Taylor expanded in x around 0 51.7%
unpow251.7%
unpow251.7%
Simplified51.7%
if 1.16000000000000006e-270 < y < 2.1999999999999999e-59Initial program 100.0%
div-inv99.8%
add-sqr-sqrt47.1%
fabs-sqr47.1%
add-sqr-sqrt47.7%
*-commutative47.7%
add-sqr-sqrt47.6%
fabs-sqr47.6%
add-sqr-sqrt47.7%
Applied egg-rr47.7%
Taylor expanded in y around 0 48.3%
Final simplification64.7%
(FPCore (x y) :precision binary64 (if (<= x -8.2e+180) (* x (/ (/ x y) (- y x))) (if (<= x -2.95e+91) (/ x y) (if (<= x 1.97e+41) (/ y (+ x y)) (/ x y)))))
double code(double x, double y) {
double tmp;
if (x <= -8.2e+180) {
tmp = x * ((x / y) / (y - x));
} else if (x <= -2.95e+91) {
tmp = x / y;
} else if (x <= 1.97e+41) {
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 <= (-8.2d+180)) then
tmp = x * ((x / y) / (y - x))
else if (x <= (-2.95d+91)) then
tmp = x / y
else if (x <= 1.97d+41) 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 <= -8.2e+180) {
tmp = x * ((x / y) / (y - x));
} else if (x <= -2.95e+91) {
tmp = x / y;
} else if (x <= 1.97e+41) {
tmp = y / (x + y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -8.2e+180: tmp = x * ((x / y) / (y - x)) elif x <= -2.95e+91: tmp = x / y elif x <= 1.97e+41: tmp = y / (x + y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (x <= -8.2e+180) tmp = Float64(x * Float64(Float64(x / y) / Float64(y - x))); elseif (x <= -2.95e+91) tmp = Float64(x / y); elseif (x <= 1.97e+41) 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 <= -8.2e+180) tmp = x * ((x / y) / (y - x)); elseif (x <= -2.95e+91) tmp = x / y; elseif (x <= 1.97e+41) tmp = y / (x + y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -8.2e+180], N[(x * N[(N[(x / y), $MachinePrecision] / N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, -2.95e+91], N[(x / y), $MachinePrecision], If[LessEqual[x, 1.97e+41], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -8.2 \cdot 10^{+180}:\\
\;\;\;\;x \cdot \frac{\frac{x}{y}}{y - x}\\
\mathbf{elif}\;x \leq -2.95 \cdot 10^{+91}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;x \leq 1.97 \cdot 10^{+41}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < -8.2e180Initial program 100.0%
div-inv99.7%
add-sqr-sqrt8.0%
fabs-sqr8.0%
add-sqr-sqrt8.3%
*-commutative8.3%
add-sqr-sqrt0.3%
fabs-sqr0.3%
add-sqr-sqrt20.3%
Applied egg-rr20.3%
flip--12.6%
associate-*r/12.6%
+-commutative12.6%
Applied egg-rr12.6%
Taylor expanded in y around 0 13.0%
unpow213.0%
associate-/l*13.4%
associate-/r/13.4%
Simplified13.4%
associate-*l/13.0%
*-un-lft-identity13.0%
associate-*l/13.0%
frac-2neg13.0%
div-inv13.0%
associate-*l/13.0%
*-un-lft-identity13.0%
distribute-neg-frac13.0%
add-sqr-sqrt0.2%
sqrt-unprod0.0%
sqr-neg0.0%
sqrt-unprod0.1%
add-sqr-sqrt30.5%
frac-2neg30.5%
associate-*r/38.3%
distribute-neg-in38.3%
add-sqr-sqrt0.2%
sqrt-unprod34.1%
sqr-neg34.1%
sqrt-unprod38.3%
add-sqr-sqrt38.7%
sub-neg38.7%
Applied egg-rr38.7%
associate-*l*68.5%
associate-*r/68.7%
*-rgt-identity68.7%
Simplified68.7%
if -8.2e180 < x < -2.9500000000000001e91 or 1.97000000000000015e41 < x Initial program 100.0%
div-inv99.8%
add-sqr-sqrt71.2%
fabs-sqr71.2%
add-sqr-sqrt71.7%
*-commutative71.7%
add-sqr-sqrt37.8%
fabs-sqr37.8%
add-sqr-sqrt51.7%
Applied egg-rr51.7%
Taylor expanded in y around 0 52.0%
if -2.9500000000000001e91 < x < 1.97000000000000015e41Initial program 100.0%
div-inv99.7%
add-sqr-sqrt47.3%
fabs-sqr47.3%
add-sqr-sqrt48.3%
*-commutative48.3%
add-sqr-sqrt4.4%
fabs-sqr4.4%
add-sqr-sqrt15.8%
Applied egg-rr15.8%
flip--13.2%
associate-*r/13.2%
+-commutative13.2%
Applied egg-rr13.2%
Taylor expanded in x around 0 1.8%
unpow21.8%
mul-1-neg1.8%
distribute-rgt-neg-out1.8%
Simplified1.8%
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-unprod19.6%
sqr-neg19.6%
sqrt-unprod33.4%
add-sqr-sqrt70.1%
Applied egg-rr70.1%
+-lft-identity70.1%
Simplified70.1%
Final simplification64.7%
(FPCore (x y) :precision binary64 (if (or (<= y -8.5e-79) (not (<= y 3.2e-59))) (/ y (+ x y)) (+ (/ x y) -1.0)))
double code(double x, double y) {
double tmp;
if ((y <= -8.5e-79) || !(y <= 3.2e-59)) {
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 <= (-8.5d-79)) .or. (.not. (y <= 3.2d-59))) 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 <= -8.5e-79) || !(y <= 3.2e-59)) {
tmp = y / (x + y);
} else {
tmp = (x / y) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -8.5e-79) or not (y <= 3.2e-59): tmp = y / (x + y) else: tmp = (x / y) + -1.0 return tmp
function code(x, y) tmp = 0.0 if ((y <= -8.5e-79) || !(y <= 3.2e-59)) 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 <= -8.5e-79) || ~((y <= 3.2e-59))) tmp = y / (x + y); else tmp = (x / y) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -8.5e-79], N[Not[LessEqual[y, 3.2e-59]], $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 -8.5 \cdot 10^{-79} \lor \neg \left(y \leq 3.2 \cdot 10^{-59}\right):\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y} + -1\\
\end{array}
\end{array}
if y < -8.50000000000000029e-79 or 3.1999999999999999e-59 < y Initial program 100.0%
div-inv99.7%
add-sqr-sqrt52.1%
fabs-sqr52.1%
add-sqr-sqrt53.1%
*-commutative53.1%
add-sqr-sqrt7.6%
fabs-sqr7.6%
add-sqr-sqrt12.8%
Applied egg-rr12.8%
flip--5.1%
associate-*r/5.1%
+-commutative5.1%
Applied egg-rr5.1%
Taylor expanded in x around 0 1.7%
unpow21.7%
mul-1-neg1.7%
distribute-rgt-neg-out1.7%
Simplified1.7%
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.1%
sqrt-unprod16.9%
sqr-neg16.9%
sqrt-unprod33.9%
add-sqr-sqrt71.5%
Applied egg-rr71.5%
+-lft-identity71.5%
Simplified71.5%
if -8.50000000000000029e-79 < y < 3.1999999999999999e-59Initial 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-sqrt47.8%
fabs-sqr47.8%
rem-square-sqrt48.3%
div-sub48.3%
sub-neg48.3%
*-inverses48.3%
metadata-eval48.3%
+-commutative48.3%
Simplified48.3%
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-sqrt50.4%
fabs-sqr50.4%
add-sqr-sqrt51.2%
*-commutative51.2%
add-sqr-sqrt13.7%
fabs-sqr13.7%
add-sqr-sqrt26.6%
Applied egg-rr26.6%
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-sqrt50.4%
fabs-sqr50.4%
add-sqr-sqrt51.2%
*-commutative51.2%
add-sqr-sqrt13.7%
fabs-sqr13.7%
add-sqr-sqrt26.6%
Applied egg-rr26.6%
Taylor expanded in y around inf 1.3%
Final simplification1.3%
herbie shell --seed 2023271
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))