
(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 3 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 (- y x)) (fabs y)))
double code(double x, double y) {
return fabs((y - x)) / fabs(y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = abs((y - x)) / abs(y)
end function
public static double code(double x, double y) {
return Math.abs((y - x)) / Math.abs(y);
}
def code(x, y): return math.fabs((y - x)) / math.fabs(y)
function code(x, y) return Float64(abs(Float64(y - x)) / abs(y)) end
function tmp = code(x, y) tmp = abs((y - x)) / abs(y); end
code[x_, y_] := N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / N[Abs[y], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\left|y - x\right|}{\left|y\right|}
\end{array}
Initial program 100.0%
Final simplification100.0%
(FPCore (x y)
:precision binary64
(if (or (<= x -4e+159)
(and (not (<= x -1.7e+123))
(or (<= x 1.5e+154)
(and (not (<= x 2.35e+260)) (<= x 4.1e+295)))))
(- 1.0 (/ x y))
(+ (/ x y) -1.0)))
double code(double x, double y) {
double tmp;
if ((x <= -4e+159) || (!(x <= -1.7e+123) && ((x <= 1.5e+154) || (!(x <= 2.35e+260) && (x <= 4.1e+295))))) {
tmp = 1.0 - (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 ((x <= (-4d+159)) .or. (.not. (x <= (-1.7d+123))) .and. (x <= 1.5d+154) .or. (.not. (x <= 2.35d+260)) .and. (x <= 4.1d+295)) then
tmp = 1.0d0 - (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 ((x <= -4e+159) || (!(x <= -1.7e+123) && ((x <= 1.5e+154) || (!(x <= 2.35e+260) && (x <= 4.1e+295))))) {
tmp = 1.0 - (x / y);
} else {
tmp = (x / y) + -1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if (x <= -4e+159) or (not (x <= -1.7e+123) and ((x <= 1.5e+154) or (not (x <= 2.35e+260) and (x <= 4.1e+295)))): tmp = 1.0 - (x / y) else: tmp = (x / y) + -1.0 return tmp
function code(x, y) tmp = 0.0 if ((x <= -4e+159) || (!(x <= -1.7e+123) && ((x <= 1.5e+154) || (!(x <= 2.35e+260) && (x <= 4.1e+295))))) tmp = Float64(1.0 - 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 ((x <= -4e+159) || (~((x <= -1.7e+123)) && ((x <= 1.5e+154) || (~((x <= 2.35e+260)) && (x <= 4.1e+295))))) tmp = 1.0 - (x / y); else tmp = (x / y) + -1.0; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[x, -4e+159], And[N[Not[LessEqual[x, -1.7e+123]], $MachinePrecision], Or[LessEqual[x, 1.5e+154], And[N[Not[LessEqual[x, 2.35e+260]], $MachinePrecision], LessEqual[x, 4.1e+295]]]]], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4 \cdot 10^{+159} \lor \neg \left(x \leq -1.7 \cdot 10^{+123}\right) \land \left(x \leq 1.5 \cdot 10^{+154} \lor \neg \left(x \leq 2.35 \cdot 10^{+260}\right) \land x \leq 4.1 \cdot 10^{+295}\right):\\
\;\;\;\;1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y} + -1\\
\end{array}
\end{array}
if x < -3.9999999999999997e159 or -1.70000000000000001e123 < x < 1.50000000000000013e154 or 2.35000000000000011e260 < x < 4.09999999999999983e295Initial program 100.0%
fabs-sub100.0%
Simplified100.0%
Taylor expanded in y around -inf 100.0%
fabs-neg100.0%
neg-mul-1100.0%
sub-neg100.0%
fabs-sub100.0%
fabs-div100.0%
rem-square-sqrt81.7%
fabs-sqr81.7%
rem-square-sqrt82.0%
div-sub82.0%
*-inverses82.0%
Simplified82.0%
if -3.9999999999999997e159 < x < -1.70000000000000001e123 or 1.50000000000000013e154 < x < 2.35000000000000011e260 or 4.09999999999999983e295 < x Initial program 100.0%
fabs-sub100.0%
Simplified100.0%
Taylor expanded in y around -inf 100.0%
fabs-neg100.0%
neg-mul-1100.0%
sub-neg100.0%
fabs-sub100.0%
fabs-div100.0%
rem-square-sqrt28.5%
fabs-sqr28.5%
rem-square-sqrt28.8%
rem-square-sqrt5.7%
fabs-sqr5.7%
rem-square-sqrt57.4%
fabs-sub57.4%
unpow157.4%
sqr-pow51.3%
fabs-sqr51.3%
sqr-pow71.7%
unpow171.7%
div-sub71.7%
*-inverses71.7%
sub-neg71.7%
metadata-eval71.7%
+-commutative71.7%
Simplified71.7%
Final simplification80.6%
(FPCore (x y) :precision binary64 (+ (/ x y) -1.0))
double code(double x, double y) {
return (x / y) + -1.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x / y) + (-1.0d0)
end function
public static double code(double x, double y) {
return (x / y) + -1.0;
}
def code(x, y): return (x / y) + -1.0
function code(x, y) return Float64(Float64(x / y) + -1.0) end
function tmp = code(x, y) tmp = (x / y) + -1.0; end
code[x_, y_] := N[(N[(x / y), $MachinePrecision] + -1.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{y} + -1
\end{array}
Initial program 100.0%
fabs-sub100.0%
Simplified100.0%
Taylor expanded in y around -inf 100.0%
fabs-neg100.0%
neg-mul-1100.0%
sub-neg100.0%
fabs-sub100.0%
fabs-div100.0%
rem-square-sqrt74.5%
fabs-sqr74.5%
rem-square-sqrt74.7%
rem-square-sqrt36.8%
fabs-sqr36.8%
rem-square-sqrt49.7%
fabs-sub49.7%
unpow149.7%
sqr-pow12.5%
fabs-sqr12.5%
sqr-pow26.3%
unpow126.3%
div-sub26.3%
*-inverses26.3%
sub-neg26.3%
metadata-eval26.3%
+-commutative26.3%
Simplified26.3%
Final simplification26.3%
herbie shell --seed 2023336
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))