
(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 5 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 (if (or (<= x -1.75e+21) (not (<= x 1.85e+61))) (fabs (/ x y)) 1.0))
double code(double x, double y) {
double tmp;
if ((x <= -1.75e+21) || !(x <= 1.85e+61)) {
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 ((x <= (-1.75d+21)) .or. (.not. (x <= 1.85d+61))) 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 ((x <= -1.75e+21) || !(x <= 1.85e+61)) {
tmp = Math.abs((x / y));
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -1.75e+21) or not (x <= 1.85e+61): tmp = math.fabs((x / y)) else: tmp = 1.0 return tmp
function code(x, y) tmp = 0.0 if ((x <= -1.75e+21) || !(x <= 1.85e+61)) tmp = abs(Float64(x / y)); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((x <= -1.75e+21) || ~((x <= 1.85e+61))) tmp = abs((x / y)); else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -1.75e+21], N[Not[LessEqual[x, 1.85e+61]], $MachinePrecision]], N[Abs[N[(x / y), $MachinePrecision]], $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.75 \cdot 10^{+21} \lor \neg \left(x \leq 1.85 \cdot 10^{+61}\right):\\
\;\;\;\;\left|\frac{x}{y}\right|\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if x < -1.75e21 or 1.85000000000000001e61 < x 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%
Taylor expanded in x around inf 83.2%
associate-*r/83.2%
neg-mul-183.2%
Simplified83.2%
if -1.75e21 < x < 1.85000000000000001e61Initial 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%
Taylor expanded in x around 0 78.7%
Final simplification80.7%
(FPCore (x y) :precision binary64 (if (<= x -1.6e+55) (/ x y) (if (<= x 4.2e+126) 1.0 (+ (/ x y) -1.0))))
double code(double x, double y) {
double tmp;
if (x <= -1.6e+55) {
tmp = x / y;
} else if (x <= 4.2e+126) {
tmp = 1.0;
} 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 (x <= (-1.6d+55)) then
tmp = x / y
else if (x <= 4.2d+126) then
tmp = 1.0d0
else
tmp = (x / y) + (-1.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1.6e+55) {
tmp = x / y;
} else if (x <= 4.2e+126) {
tmp = 1.0;
} else {
tmp = (x / y) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1.6e+55: tmp = x / y elif x <= 4.2e+126: tmp = 1.0 else: tmp = (x / y) + -1.0 return tmp
function code(x, y) tmp = 0.0 if (x <= -1.6e+55) tmp = Float64(x / y); elseif (x <= 4.2e+126) tmp = 1.0; else tmp = Float64(Float64(x / y) + -1.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1.6e+55) tmp = x / y; elseif (x <= 4.2e+126) tmp = 1.0; else tmp = (x / y) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1.6e+55], N[(x / y), $MachinePrecision], If[LessEqual[x, 4.2e+126], 1.0, N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.6 \cdot 10^{+55}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;x \leq 4.2 \cdot 10^{+126}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y} + -1\\
\end{array}
\end{array}
if x < -1.6000000000000001e55Initial 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-sqrt39.1%
fabs-sqr39.1%
rem-square-sqrt39.6%
div-sub39.6%
sub-neg39.6%
*-inverses39.6%
metadata-eval39.6%
+-commutative39.6%
Simplified39.6%
Taylor expanded in x around inf 39.8%
if -1.6000000000000001e55 < x < 4.1999999999999998e126Initial 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%
Taylor expanded in x around 0 73.3%
if 4.1999999999999998e126 < x Initial 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-sqrt52.2%
fabs-sqr52.2%
rem-square-sqrt52.7%
div-sub52.7%
sub-neg52.7%
*-inverses52.7%
metadata-eval52.7%
+-commutative52.7%
Simplified52.7%
Final simplification63.4%
(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%
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-sqrt26.4%
fabs-sqr26.4%
rem-square-sqrt27.5%
div-sub27.5%
sub-neg27.5%
*-inverses27.5%
metadata-eval27.5%
+-commutative27.5%
Simplified27.5%
Taylor expanded in x around inf 28.0%
Final simplification28.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%
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-sqrt26.4%
fabs-sqr26.4%
rem-square-sqrt27.5%
div-sub27.5%
sub-neg27.5%
*-inverses27.5%
metadata-eval27.5%
+-commutative27.5%
Simplified27.5%
Taylor expanded in x around 0 1.3%
Final simplification1.3%
herbie shell --seed 2024039
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))