
(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%
Taylor expanded in x around -inf 100.0%
Simplified100.0%
(FPCore (x y) :precision binary64 (if (<= y -1.35e-40) 1.0 (if (<= y 1.2e-27) (fabs (/ x y)) 1.0)))
double code(double x, double y) {
double tmp;
if (y <= -1.35e-40) {
tmp = 1.0;
} else if (y <= 1.2e-27) {
tmp = fabs((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 <= (-1.35d-40)) then
tmp = 1.0d0
else if (y <= 1.2d-27) then
tmp = abs((x / y))
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -1.35e-40) {
tmp = 1.0;
} else if (y <= 1.2e-27) {
tmp = Math.abs((x / y));
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -1.35e-40: tmp = 1.0 elif y <= 1.2e-27: tmp = math.fabs((x / y)) else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -1.35e-40) tmp = 1.0; elseif (y <= 1.2e-27) tmp = abs(Float64(x / y)); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -1.35e-40) tmp = 1.0; elseif (y <= 1.2e-27) tmp = abs((x / y)); else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -1.35e-40], 1.0, If[LessEqual[y, 1.2e-27], N[Abs[N[(x / y), $MachinePrecision]], $MachinePrecision], 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.35 \cdot 10^{-40}:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq 1.2 \cdot 10^{-27}:\\
\;\;\;\;\left|\frac{x}{y}\right|\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if y < -1.35e-40 or 1.20000000000000001e-27 < y Initial program 100.0%
Taylor expanded in x around -inf 100.0%
Simplified100.0%
Taylor expanded in x around 0 80.1%
Applied egg-rr80.1%
if -1.35e-40 < y < 1.20000000000000001e-27Initial program 99.9%
Taylor expanded in x around -inf 99.9%
Simplified100.0%
Taylor expanded in x around inf 77.1%
(FPCore (x y) :precision binary64 (if (<= x -6e+65) (/ x y) (if (<= x 1.15e+123) 1.0 (pow x 3.0))))
double code(double x, double y) {
double tmp;
if (x <= -6e+65) {
tmp = x / y;
} else if (x <= 1.15e+123) {
tmp = 1.0;
} else {
tmp = pow(x, 3.0);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-6d+65)) then
tmp = x / y
else if (x <= 1.15d+123) then
tmp = 1.0d0
else
tmp = x ** 3.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -6e+65) {
tmp = x / y;
} else if (x <= 1.15e+123) {
tmp = 1.0;
} else {
tmp = Math.pow(x, 3.0);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -6e+65: tmp = x / y elif x <= 1.15e+123: tmp = 1.0 else: tmp = math.pow(x, 3.0) return tmp
function code(x, y) tmp = 0.0 if (x <= -6e+65) tmp = Float64(x / y); elseif (x <= 1.15e+123) tmp = 1.0; else tmp = x ^ 3.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -6e+65) tmp = x / y; elseif (x <= 1.15e+123) tmp = 1.0; else tmp = x ^ 3.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -6e+65], N[(x / y), $MachinePrecision], If[LessEqual[x, 1.15e+123], 1.0, N[Power[x, 3.0], $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6 \cdot 10^{+65}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;x \leq 1.15 \cdot 10^{+123}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;{x}^{3}\\
\end{array}
\end{array}
if x < -6.0000000000000004e65Initial program 100.0%
div-inv99.7%
add-sqr-sqrt10.5%
fabs-sqr10.5%
add-sqr-sqrt11.0%
*-commutative11.0%
add-sqr-sqrt0.2%
fabs-sqr0.2%
add-sqr-sqrt42.9%
Applied egg-rr42.9%
Taylor expanded in x around inf 43.5%
Taylor expanded in y around 0 43.5%
if -6.0000000000000004e65 < x < 1.14999999999999995e123Initial program 100.0%
Taylor expanded in x around -inf 100.0%
Simplified100.0%
Taylor expanded in x around 0 73.2%
Applied egg-rr73.2%
if 1.14999999999999995e123 < x Initial program 100.0%
div-inv99.8%
add-sqr-sqrt93.3%
fabs-sqr93.3%
add-sqr-sqrt93.7%
*-commutative93.7%
add-sqr-sqrt40.5%
fabs-sqr40.5%
add-sqr-sqrt40.7%
Applied egg-rr40.7%
Taylor expanded in x around inf 37.5%
Applied egg-rr49.6%
(FPCore (x y) :precision binary64 (if (or (<= x -1.65e+146) (not (<= x 2.4e+161))) (* x x) 1.0))
double code(double x, double y) {
double tmp;
if ((x <= -1.65e+146) || !(x <= 2.4e+161)) {
tmp = x * x;
} 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 <= (-1.65d+146)) .or. (.not. (x <= 2.4d+161))) then
tmp = x * x
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((x <= -1.65e+146) || !(x <= 2.4e+161)) {
tmp = x * x;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -1.65e+146) or not (x <= 2.4e+161): tmp = x * x else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if ((x <= -1.65e+146) || !(x <= 2.4e+161)) tmp = Float64(x * x); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -1.65e+146) || ~((x <= 2.4e+161))) tmp = x * x; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -1.65e+146], N[Not[LessEqual[x, 2.4e+161]], $MachinePrecision]], N[(x * x), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.65 \cdot 10^{+146} \lor \neg \left(x \leq 2.4 \cdot 10^{+161}\right):\\
\;\;\;\;x \cdot x\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -1.65000000000000008e146 or 2.3999999999999999e161 < x Initial program 100.0%
add-log-exp51.0%
*-un-lft-identity51.0%
log-prod51.0%
metadata-eval51.0%
add-log-exp100.0%
add-sqr-sqrt51.5%
fabs-sqr51.5%
add-sqr-sqrt20.6%
fabs-sqr20.6%
add-sqr-sqrt20.7%
add-sqr-sqrt46.8%
Applied egg-rr46.8%
Taylor expanded in x around inf 45.2%
Applied egg-rr44.4%
if -1.65000000000000008e146 < x < 2.3999999999999999e161Initial program 100.0%
Taylor expanded in x around -inf 100.0%
Simplified100.0%
Taylor expanded in x around 0 68.0%
Applied egg-rr68.0%
Final simplification62.7%
(FPCore (x y) :precision binary64 (if (<= x -8e+63) (/ x y) (if (<= x 5.8e+165) 1.0 (* x x))))
double code(double x, double y) {
double tmp;
if (x <= -8e+63) {
tmp = x / y;
} else if (x <= 5.8e+165) {
tmp = 1.0;
} else {
tmp = x * x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-8d+63)) then
tmp = x / y
else if (x <= 5.8d+165) then
tmp = 1.0d0
else
tmp = x * x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -8e+63) {
tmp = x / y;
} else if (x <= 5.8e+165) {
tmp = 1.0;
} else {
tmp = x * x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -8e+63: tmp = x / y elif x <= 5.8e+165: tmp = 1.0 else: tmp = x * x return tmp
function code(x, y) tmp = 0.0 if (x <= -8e+63) tmp = Float64(x / y); elseif (x <= 5.8e+165) tmp = 1.0; else tmp = Float64(x * x); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -8e+63) tmp = x / y; elseif (x <= 5.8e+165) tmp = 1.0; else tmp = x * x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -8e+63], N[(x / y), $MachinePrecision], If[LessEqual[x, 5.8e+165], 1.0, N[(x * x), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -8 \cdot 10^{+63}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;x \leq 5.8 \cdot 10^{+165}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;x \cdot x\\
\end{array}
\end{array}
if x < -8.00000000000000046e63Initial program 100.0%
div-inv99.7%
add-sqr-sqrt10.5%
fabs-sqr10.5%
add-sqr-sqrt11.0%
*-commutative11.0%
add-sqr-sqrt0.2%
fabs-sqr0.2%
add-sqr-sqrt42.9%
Applied egg-rr42.9%
Taylor expanded in x around inf 43.5%
Taylor expanded in y around 0 43.5%
if -8.00000000000000046e63 < x < 5.80000000000000011e165Initial program 100.0%
Taylor expanded in x around -inf 100.0%
Simplified100.0%
Taylor expanded in x around 0 72.2%
Applied egg-rr72.2%
if 5.80000000000000011e165 < x Initial program 99.9%
add-log-exp52.7%
*-un-lft-identity52.7%
log-prod52.7%
metadata-eval52.7%
add-log-exp99.9%
add-sqr-sqrt96.0%
fabs-sqr96.0%
add-sqr-sqrt42.6%
fabs-sqr42.6%
add-sqr-sqrt42.7%
add-sqr-sqrt43.0%
Applied egg-rr43.0%
Taylor expanded in x around inf 39.1%
Applied egg-rr49.2%
(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%
Taylor expanded in x around -inf 100.0%
Simplified100.0%
Taylor expanded in x around 0 55.1%
Applied egg-rr55.1%
herbie shell --seed 2024157
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))