
(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 7 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 (if (or (<= y -1.6e-212) (and (not (<= y -6.6e-257)) (<= y 2.5e-172))) (- 1.0 (/ x y)) (/ (+ x y) y)))
double code(double x, double y) {
double tmp;
if ((y <= -1.6e-212) || (!(y <= -6.6e-257) && (y <= 2.5e-172))) {
tmp = 1.0 - (x / y);
} else {
tmp = (x + y) / y;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y <= (-1.6d-212)) .or. (.not. (y <= (-6.6d-257))) .and. (y <= 2.5d-172)) then
tmp = 1.0d0 - (x / y)
else
tmp = (x + y) / y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -1.6e-212) || (!(y <= -6.6e-257) && (y <= 2.5e-172))) {
tmp = 1.0 - (x / y);
} else {
tmp = (x + y) / y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -1.6e-212) or (not (y <= -6.6e-257) and (y <= 2.5e-172)): tmp = 1.0 - (x / y) else: tmp = (x + y) / y return tmp
function code(x, y) tmp = 0.0 if ((y <= -1.6e-212) || (!(y <= -6.6e-257) && (y <= 2.5e-172))) tmp = Float64(1.0 - Float64(x / y)); else tmp = Float64(Float64(x + y) / y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -1.6e-212) || (~((y <= -6.6e-257)) && (y <= 2.5e-172))) tmp = 1.0 - (x / y); else tmp = (x + y) / y; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -1.6e-212], And[N[Not[LessEqual[y, -6.6e-257]], $MachinePrecision], LessEqual[y, 2.5e-172]]], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(N[(x + y), $MachinePrecision] / y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.6 \cdot 10^{-212} \lor \neg \left(y \leq -6.6 \cdot 10^{-257}\right) \land y \leq 2.5 \cdot 10^{-172}:\\
\;\;\;\;1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x + y}{y}\\
\end{array}
\end{array}
if y < -1.5999999999999999e-212 or -6.6e-257 < y < 2.5e-172Initial program 100.0%
add-sqr-sqrt73.8%
fabs-sqr73.8%
add-sqr-sqrt7.3%
fabs-sqr7.3%
add-sqr-sqrt7.4%
add-sqr-sqrt20.4%
clear-num20.3%
associate-/r/20.3%
Applied egg-rr20.3%
associate-*l/20.4%
*-un-lft-identity20.4%
frac-2neg20.4%
sub-neg20.4%
distribute-neg-in20.4%
remove-double-neg20.4%
add-sqr-sqrt12.9%
sqrt-unprod14.0%
sqr-neg14.0%
sqrt-unprod13.4%
add-sqr-sqrt80.6%
Applied egg-rr80.6%
Taylor expanded in x around 0 80.7%
mul-1-neg80.7%
unsub-neg80.7%
Simplified80.7%
if -1.5999999999999999e-212 < y < -6.6e-257 or 2.5e-172 < y Initial program 100.0%
add-sqr-sqrt22.3%
fabs-sqr22.3%
add-sqr-sqrt21.3%
fabs-sqr21.3%
add-sqr-sqrt22.4%
add-sqr-sqrt29.9%
clear-num29.8%
associate-/r/29.8%
Applied egg-rr29.8%
associate-*l/29.9%
*-un-lft-identity29.9%
sub-neg29.9%
add-sqr-sqrt7.5%
sqrt-unprod59.9%
sqr-neg59.9%
sqrt-unprod77.5%
add-sqr-sqrt86.3%
Applied egg-rr86.3%
Final simplification83.0%
(FPCore (x y) :precision binary64 (if (or (<= x -7e+231) (not (<= x 2.9e+143))) (/ x y) 1.0))
double code(double x, double y) {
double tmp;
if ((x <= -7e+231) || !(x <= 2.9e+143)) {
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 ((x <= (-7d+231)) .or. (.not. (x <= 2.9d+143))) 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 ((x <= -7e+231) || !(x <= 2.9e+143)) {
tmp = x / y;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -7e+231) or not (x <= 2.9e+143): tmp = x / y else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if ((x <= -7e+231) || !(x <= 2.9e+143)) tmp = Float64(x / y); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -7e+231) || ~((x <= 2.9e+143))) tmp = x / y; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -7e+231], N[Not[LessEqual[x, 2.9e+143]], $MachinePrecision]], N[(x / y), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7 \cdot 10^{+231} \lor \neg \left(x \leq 2.9 \cdot 10^{+143}\right):\\
\;\;\;\;\frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -6.9999999999999997e231 or 2.8999999999999998e143 < x Initial program 99.9%
add-sqr-sqrt68.1%
fabs-sqr68.1%
add-sqr-sqrt38.6%
fabs-sqr38.6%
add-sqr-sqrt38.8%
add-sqr-sqrt52.1%
clear-num52.0%
associate-/r/52.0%
Applied egg-rr52.0%
Taylor expanded in y around 0 51.8%
if -6.9999999999999997e231 < x < 2.8999999999999998e143Initial program 100.0%
add-sqr-sqrt48.1%
fabs-sqr48.1%
add-sqr-sqrt6.3%
fabs-sqr6.3%
add-sqr-sqrt6.9%
add-sqr-sqrt17.0%
clear-num16.9%
associate-/r/16.9%
Applied egg-rr16.9%
associate-*l/17.0%
*-un-lft-identity17.0%
sub-neg17.0%
add-sqr-sqrt10.0%
sqrt-unprod32.8%
sqr-neg32.8%
sqrt-unprod37.0%
add-sqr-sqrt81.2%
Applied egg-rr81.2%
Taylor expanded in x around 0 67.8%
Final simplification64.4%
(FPCore (x y) :precision binary64 (if (<= x -6.5e+179) (/ (- x) y) (if (<= x 2.8e+143) 1.0 (/ x y))))
double code(double x, double y) {
double tmp;
if (x <= -6.5e+179) {
tmp = -x / y;
} else if (x <= 2.8e+143) {
tmp = 1.0;
} 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 <= (-6.5d+179)) then
tmp = -x / y
else if (x <= 2.8d+143) then
tmp = 1.0d0
else
tmp = x / y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -6.5e+179) {
tmp = -x / y;
} else if (x <= 2.8e+143) {
tmp = 1.0;
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -6.5e+179: tmp = -x / y elif x <= 2.8e+143: tmp = 1.0 else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (x <= -6.5e+179) tmp = Float64(Float64(-x) / y); elseif (x <= 2.8e+143) tmp = 1.0; else tmp = Float64(x / y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -6.5e+179) tmp = -x / y; elseif (x <= 2.8e+143) tmp = 1.0; else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -6.5e+179], N[((-x) / y), $MachinePrecision], If[LessEqual[x, 2.8e+143], 1.0, N[(x / y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6.5 \cdot 10^{+179}:\\
\;\;\;\;\frac{-x}{y}\\
\mathbf{elif}\;x \leq 2.8 \cdot 10^{+143}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < -6.50000000000000052e179Initial program 99.9%
add-sqr-sqrt3.9%
fabs-sqr3.9%
add-sqr-sqrt0.0%
fabs-sqr0.0%
add-sqr-sqrt0.2%
add-sqr-sqrt36.2%
clear-num36.1%
associate-/r/36.1%
Applied egg-rr36.1%
associate-*l/36.2%
*-un-lft-identity36.2%
frac-2neg36.2%
sub-neg36.2%
distribute-neg-in36.2%
remove-double-neg36.2%
add-sqr-sqrt35.8%
sqrt-unprod76.7%
sqr-neg76.7%
sqrt-unprod59.8%
add-sqr-sqrt64.2%
Applied egg-rr64.2%
Taylor expanded in x around inf 53.0%
associate-*r/53.0%
mul-1-neg53.0%
Simplified53.0%
if -6.50000000000000052e179 < x < 2.79999999999999998e143Initial program 100.0%
add-sqr-sqrt50.3%
fabs-sqr50.3%
add-sqr-sqrt6.6%
fabs-sqr6.6%
add-sqr-sqrt7.2%
add-sqr-sqrt16.7%
clear-num16.7%
associate-/r/16.7%
Applied egg-rr16.7%
associate-*l/16.7%
*-un-lft-identity16.7%
sub-neg16.7%
add-sqr-sqrt9.4%
sqrt-unprod33.3%
sqr-neg33.3%
sqrt-unprod37.7%
add-sqr-sqrt82.9%
Applied egg-rr82.9%
Taylor expanded in x around 0 69.8%
if 2.79999999999999998e143 < x Initial program 100.0%
add-sqr-sqrt94.2%
fabs-sqr94.2%
add-sqr-sqrt54.9%
fabs-sqr54.9%
add-sqr-sqrt55.1%
add-sqr-sqrt55.5%
clear-num55.4%
associate-/r/55.4%
Applied egg-rr55.4%
Taylor expanded in y around 0 56.0%
Final simplification66.1%
(FPCore (x y) :precision binary64 (if (<= x 6.5e+260) (- 1.0 (/ x y)) (/ x y)))
double code(double x, double y) {
double tmp;
if (x <= 6.5e+260) {
tmp = 1.0 - (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 <= 6.5d+260) then
tmp = 1.0d0 - (x / y)
else
tmp = x / y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 6.5e+260) {
tmp = 1.0 - (x / y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 6.5e+260: tmp = 1.0 - (x / y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (x <= 6.5e+260) tmp = Float64(1.0 - Float64(x / y)); else tmp = Float64(x / y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 6.5e+260) tmp = 1.0 - (x / y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 6.5e+260], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 6.5 \cdot 10^{+260}:\\
\;\;\;\;1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < 6.49999999999999954e260Initial program 100.0%
add-sqr-sqrt50.0%
fabs-sqr50.0%
add-sqr-sqrt10.1%
fabs-sqr10.1%
add-sqr-sqrt10.6%
add-sqr-sqrt21.9%
clear-num21.8%
associate-/r/21.8%
Applied egg-rr21.8%
associate-*l/21.9%
*-un-lft-identity21.9%
frac-2neg21.9%
sub-neg21.9%
distribute-neg-in21.9%
remove-double-neg21.9%
add-sqr-sqrt11.1%
sqrt-unprod28.5%
sqr-neg28.5%
sqrt-unprod38.7%
add-sqr-sqrt79.2%
Applied egg-rr79.2%
Taylor expanded in x around 0 79.2%
mul-1-neg79.2%
unsub-neg79.2%
Simplified79.2%
if 6.49999999999999954e260 < x Initial program 100.0%
add-sqr-sqrt99.7%
fabs-sqr99.7%
add-sqr-sqrt74.7%
fabs-sqr74.7%
add-sqr-sqrt74.9%
add-sqr-sqrt75.0%
clear-num75.0%
associate-/r/75.0%
Applied egg-rr75.0%
Taylor expanded in y around 0 75.0%
Final simplification79.0%
(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%
add-sqr-sqrt52.3%
fabs-sqr52.3%
add-sqr-sqrt13.2%
fabs-sqr13.2%
add-sqr-sqrt13.7%
add-sqr-sqrt24.4%
clear-num24.3%
associate-/r/24.3%
Applied egg-rr24.3%
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%
add-sqr-sqrt52.3%
fabs-sqr52.3%
add-sqr-sqrt13.2%
fabs-sqr13.2%
add-sqr-sqrt13.7%
add-sqr-sqrt24.4%
clear-num24.3%
associate-/r/24.3%
Applied egg-rr24.3%
associate-*l/24.4%
*-un-lft-identity24.4%
sub-neg24.4%
add-sqr-sqrt10.7%
sqrt-unprod36.1%
sqr-neg36.1%
sqrt-unprod38.0%
add-sqr-sqrt75.9%
Applied egg-rr75.9%
Taylor expanded in x around 0 55.4%
Final simplification55.4%
herbie shell --seed 2023334
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))