
(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 (+ (/ x y) -1.0)))
double code(double x, double y) {
return fabs(((x / y) + -1.0));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = abs(((x / y) + (-1.0d0)))
end function
public static double code(double x, double y) {
return Math.abs(((x / y) + -1.0));
}
def code(x, y): return math.fabs(((x / y) + -1.0))
function code(x, y) return abs(Float64(Float64(x / y) + -1.0)) end
function tmp = code(x, y) tmp = abs(((x / y) + -1.0)); end
code[x_, y_] := N[Abs[N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\left|\frac{x}{y} + -1\right|
\end{array}
Initial program 100.0%
div-fabs100.0%
div-sub100.0%
pow1100.0%
pow1100.0%
pow-div100.0%
metadata-eval100.0%
metadata-eval100.0%
Applied egg-rr100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(let* ((t_0 (/ (- x) y)))
(if (<= y -2.8e-71)
1.0
(if (<= y -1.45e-99)
t_0
(if (<= y -4.5e-122)
1.0
(if (<= y -9.8e-287) (/ x y) (if (<= y 1.95e-172) t_0 1.0)))))))
double code(double x, double y) {
double t_0 = -x / y;
double tmp;
if (y <= -2.8e-71) {
tmp = 1.0;
} else if (y <= -1.45e-99) {
tmp = t_0;
} else if (y <= -4.5e-122) {
tmp = 1.0;
} else if (y <= -9.8e-287) {
tmp = x / y;
} else if (y <= 1.95e-172) {
tmp = t_0;
} else {
tmp = 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 = -x / y
if (y <= (-2.8d-71)) then
tmp = 1.0d0
else if (y <= (-1.45d-99)) then
tmp = t_0
else if (y <= (-4.5d-122)) then
tmp = 1.0d0
else if (y <= (-9.8d-287)) then
tmp = x / y
else if (y <= 1.95d-172) then
tmp = t_0
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = -x / y;
double tmp;
if (y <= -2.8e-71) {
tmp = 1.0;
} else if (y <= -1.45e-99) {
tmp = t_0;
} else if (y <= -4.5e-122) {
tmp = 1.0;
} else if (y <= -9.8e-287) {
tmp = x / y;
} else if (y <= 1.95e-172) {
tmp = t_0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): t_0 = -x / y tmp = 0 if y <= -2.8e-71: tmp = 1.0 elif y <= -1.45e-99: tmp = t_0 elif y <= -4.5e-122: tmp = 1.0 elif y <= -9.8e-287: tmp = x / y elif y <= 1.95e-172: tmp = t_0 else: tmp = 1.0 return tmp
function code(x, y) t_0 = Float64(Float64(-x) / y) tmp = 0.0 if (y <= -2.8e-71) tmp = 1.0; elseif (y <= -1.45e-99) tmp = t_0; elseif (y <= -4.5e-122) tmp = 1.0; elseif (y <= -9.8e-287) tmp = Float64(x / y); elseif (y <= 1.95e-172) tmp = t_0; else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) t_0 = -x / y; tmp = 0.0; if (y <= -2.8e-71) tmp = 1.0; elseif (y <= -1.45e-99) tmp = t_0; elseif (y <= -4.5e-122) tmp = 1.0; elseif (y <= -9.8e-287) tmp = x / y; elseif (y <= 1.95e-172) tmp = t_0; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[((-x) / y), $MachinePrecision]}, If[LessEqual[y, -2.8e-71], 1.0, If[LessEqual[y, -1.45e-99], t$95$0, If[LessEqual[y, -4.5e-122], 1.0, If[LessEqual[y, -9.8e-287], N[(x / y), $MachinePrecision], If[LessEqual[y, 1.95e-172], t$95$0, 1.0]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{-x}{y}\\
\mathbf{if}\;y \leq -2.8 \cdot 10^{-71}:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq -1.45 \cdot 10^{-99}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq -4.5 \cdot 10^{-122}:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq -9.8 \cdot 10^{-287}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;y \leq 1.95 \cdot 10^{-172}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if y < -2.8e-71 or -1.44999999999999993e-99 < y < -4.4999999999999998e-122 or 1.94999999999999986e-172 < y Initial program 100.0%
clear-num99.8%
add-sqr-sqrt52.3%
fabs-sqr52.3%
add-sqr-sqrt53.3%
add-sqr-sqrt10.3%
fabs-sqr10.3%
add-sqr-sqrt17.4%
associate-/r/17.4%
Applied egg-rr17.4%
associate-*l/17.5%
*-un-lft-identity17.5%
sub-neg17.5%
add-sqr-sqrt7.1%
sqrt-unprod31.4%
sqr-neg31.4%
sqrt-unprod41.4%
add-sqr-sqrt82.5%
Applied egg-rr82.5%
Taylor expanded in x around 0 68.6%
if -2.8e-71 < y < -1.44999999999999993e-99 or -9.8000000000000002e-287 < y < 1.94999999999999986e-172Initial program 100.0%
clear-num99.7%
add-sqr-sqrt59.1%
fabs-sqr59.1%
add-sqr-sqrt59.5%
add-sqr-sqrt16.6%
fabs-sqr16.6%
add-sqr-sqrt28.8%
associate-/r/28.9%
Applied egg-rr28.9%
associate-*l/28.9%
*-un-lft-identity28.9%
frac-2neg28.9%
sub-neg28.9%
distribute-neg-in28.9%
remove-double-neg28.9%
add-sqr-sqrt12.0%
sqrt-unprod20.7%
sqr-neg20.7%
sqrt-unprod42.8%
add-sqr-sqrt71.6%
Applied egg-rr71.6%
Taylor expanded in x around inf 63.7%
associate-*r/63.7%
mul-1-neg63.7%
Simplified63.7%
if -4.4999999999999998e-122 < y < -9.8000000000000002e-287Initial program 99.9%
clear-num99.7%
add-sqr-sqrt0.0%
fabs-sqr0.0%
add-sqr-sqrt0.7%
add-sqr-sqrt0.4%
fabs-sqr0.4%
add-sqr-sqrt53.5%
associate-/r/53.6%
Applied egg-rr53.6%
Taylor expanded in y around 0 54.1%
Final simplification66.1%
(FPCore (x y) :precision binary64 (if (<= y -2.15e-120) 1.0 (if (<= y 8.2e-219) (/ x y) 1.0)))
double code(double x, double y) {
double tmp;
if (y <= -2.15e-120) {
tmp = 1.0;
} else if (y <= 8.2e-219) {
tmp = x / y;
} else {
tmp = 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 <= (-2.15d-120)) then
tmp = 1.0d0
else if (y <= 8.2d-219) then
tmp = x / y
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -2.15e-120) {
tmp = 1.0;
} else if (y <= 8.2e-219) {
tmp = x / y;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -2.15e-120: tmp = 1.0 elif y <= 8.2e-219: tmp = x / y else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -2.15e-120) tmp = 1.0; elseif (y <= 8.2e-219) tmp = Float64(x / y); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -2.15e-120) tmp = 1.0; elseif (y <= 8.2e-219) tmp = x / y; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -2.15e-120], 1.0, If[LessEqual[y, 8.2e-219], N[(x / y), $MachinePrecision], 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.15 \cdot 10^{-120}:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq 8.2 \cdot 10^{-219}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if y < -2.14999999999999991e-120 or 8.2e-219 < y Initial program 100.0%
clear-num99.8%
add-sqr-sqrt53.0%
fabs-sqr53.0%
add-sqr-sqrt54.0%
add-sqr-sqrt10.5%
fabs-sqr10.5%
add-sqr-sqrt17.6%
associate-/r/17.6%
Applied egg-rr17.6%
associate-*l/17.7%
*-un-lft-identity17.7%
sub-neg17.7%
add-sqr-sqrt7.1%
sqrt-unprod30.5%
sqr-neg30.5%
sqrt-unprod40.6%
add-sqr-sqrt78.9%
Applied egg-rr78.9%
Taylor expanded in x around 0 64.9%
if -2.14999999999999991e-120 < y < 8.2e-219Initial program 100.0%
clear-num99.8%
add-sqr-sqrt26.6%
fabs-sqr26.6%
add-sqr-sqrt27.2%
add-sqr-sqrt9.1%
fabs-sqr9.1%
add-sqr-sqrt44.8%
associate-/r/44.8%
Applied egg-rr44.8%
Taylor expanded in y around 0 44.3%
Final simplification60.4%
(FPCore (x y) :precision binary64 (- 1.0 (/ x y)))
double code(double x, double y) {
return 1.0 - (x / y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 1.0d0 - (x / y)
end function
public static double code(double x, double y) {
return 1.0 - (x / y);
}
def code(x, y): return 1.0 - (x / y)
function code(x, y) return Float64(1.0 - Float64(x / y)) end
function tmp = code(x, y) tmp = 1.0 - (x / y); end
code[x_, y_] := N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
1 - \frac{x}{y}
\end{array}
Initial program 100.0%
clear-num99.8%
add-sqr-sqrt47.3%
fabs-sqr47.3%
add-sqr-sqrt48.1%
add-sqr-sqrt10.2%
fabs-sqr10.2%
add-sqr-sqrt23.5%
associate-/r/23.5%
Applied egg-rr23.5%
associate-*l/23.6%
*-un-lft-identity23.6%
frac-2neg23.6%
sub-neg23.6%
distribute-neg-in23.6%
remove-double-neg23.6%
add-sqr-sqrt13.3%
sqrt-unprod31.4%
sqr-neg31.4%
sqrt-unprod37.7%
add-sqr-sqrt77.5%
Applied egg-rr77.5%
Taylor expanded in x around 0 77.5%
mul-1-neg77.5%
unsub-neg77.5%
Simplified77.5%
Final simplification77.5%
(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%
clear-num99.8%
add-sqr-sqrt47.3%
fabs-sqr47.3%
add-sqr-sqrt48.1%
add-sqr-sqrt10.2%
fabs-sqr10.2%
add-sqr-sqrt23.5%
associate-/r/23.5%
Applied egg-rr23.5%
Taylor expanded in y around inf 1.3%
Final simplification1.3%
(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%
clear-num99.8%
add-sqr-sqrt47.3%
fabs-sqr47.3%
add-sqr-sqrt48.1%
add-sqr-sqrt10.2%
fabs-sqr10.2%
add-sqr-sqrt23.5%
associate-/r/23.5%
Applied egg-rr23.5%
associate-*l/23.6%
*-un-lft-identity23.6%
sub-neg23.6%
add-sqr-sqrt13.4%
sqrt-unprod33.5%
sqr-neg33.5%
sqrt-unprod33.5%
add-sqr-sqrt72.5%
Applied egg-rr72.5%
Taylor expanded in x around 0 52.9%
Final simplification52.9%
herbie shell --seed 2024031
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))