
(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 (/ (- y x) y)))
double code(double x, double y) {
return fabs(((y - x) / y));
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = abs(((y - x) / y))
end function
public static double code(double x, double y) {
return Math.abs(((y - x) / y));
}
def code(x, y): return math.fabs(((y - x) / y))
function code(x, y) return abs(Float64(Float64(y - x) / y)) end
function tmp = code(x, y) tmp = abs(((y - x) / y)); end
code[x_, y_] := N[Abs[N[(N[(y - x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
\\
\left|\frac{y - x}{y}\right|
\end{array}
Initial program 100.0%
lift-/.f64N/A
lift-fabs.f64N/A
neg-fabsN/A
lift-fabs.f64N/A
div-fabsN/A
lower-fabs.f64N/A
lower-/.f64N/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
(FPCore (x y) :precision binary64 (if (<= (fabs (/ (- y x) y)) 50000000.0) (- 1.0 (/ x y)) (fabs (/ (- x) y))))
double code(double x, double y) {
double tmp;
if (fabs(((y - x) / y)) <= 50000000.0) {
tmp = 1.0 - (x / y);
} else {
tmp = fabs((-x / y));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (abs(((y - x) / y)) <= 50000000.0d0) then
tmp = 1.0d0 - (x / y)
else
tmp = abs((-x / y))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (Math.abs(((y - x) / y)) <= 50000000.0) {
tmp = 1.0 - (x / y);
} else {
tmp = Math.abs((-x / y));
}
return tmp;
}
def code(x, y): tmp = 0 if math.fabs(((y - x) / y)) <= 50000000.0: tmp = 1.0 - (x / y) else: tmp = math.fabs((-x / y)) return tmp
function code(x, y) tmp = 0.0 if (abs(Float64(Float64(y - x) / y)) <= 50000000.0) tmp = Float64(1.0 - Float64(x / y)); else tmp = abs(Float64(Float64(-x) / y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (abs(((y - x) / y)) <= 50000000.0) tmp = 1.0 - (x / y); else tmp = abs((-x / y)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[Abs[N[(N[(y - x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision], 50000000.0], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[Abs[N[((-x) / y), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|\frac{y - x}{y}\right| \leq 50000000:\\
\;\;\;\;1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;\left|\frac{-x}{y}\right|\\
\end{array}
\end{array}
if (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) < 5e7Initial program 100.0%
lift-/.f64N/A
lift-fabs.f64N/A
neg-fabsN/A
lift-fabs.f64N/A
div-fabsN/A
lower-fabs.f64N/A
lower-/.f64N/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
lift-fabs.f64N/A
lift-/.f64N/A
clear-numN/A
lift-/.f64N/A
inv-powN/A
sqr-powN/A
fabs-sqrN/A
sqr-powN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
lift--.f64N/A
div-subN/A
*-inversesN/A
lower--.f64N/A
lower-/.f6498.6
Applied rewrites98.6%
if 5e7 < (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) Initial program 100.0%
lift-/.f64N/A
lift-fabs.f64N/A
neg-fabsN/A
lift-fabs.f64N/A
div-fabsN/A
lower-fabs.f64N/A
lower-/.f64N/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around 0
mul-1-negN/A
lower-neg.f6499.6
Applied rewrites99.6%
Final simplification99.0%
(FPCore (x y) :precision binary64 (if (<= (fabs (/ (- y x) y)) 50000000.0) (- 1.0 (/ x y)) (- (/ x y) 1.0)))
double code(double x, double y) {
double tmp;
if (fabs(((y - x) / y)) <= 50000000.0) {
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 (abs(((y - x) / y)) <= 50000000.0d0) 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 (Math.abs(((y - x) / y)) <= 50000000.0) {
tmp = 1.0 - (x / y);
} else {
tmp = (x / y) - 1.0;
}
return tmp;
}
def code(x, y): tmp = 0 if math.fabs(((y - x) / y)) <= 50000000.0: tmp = 1.0 - (x / y) else: tmp = (x / y) - 1.0 return tmp
function code(x, y) tmp = 0.0 if (abs(Float64(Float64(y - x) / y)) <= 50000000.0) 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 (abs(((y - x) / y)) <= 50000000.0) tmp = 1.0 - (x / y); else tmp = (x / y) - 1.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[Abs[N[(N[(y - x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision], 50000000.0], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(N[(x / y), $MachinePrecision] - 1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|\frac{y - x}{y}\right| \leq 50000000:\\
\;\;\;\;1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y} - 1\\
\end{array}
\end{array}
if (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) < 5e7Initial program 100.0%
lift-/.f64N/A
lift-fabs.f64N/A
neg-fabsN/A
lift-fabs.f64N/A
div-fabsN/A
lower-fabs.f64N/A
lower-/.f64N/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
lift-fabs.f64N/A
lift-/.f64N/A
clear-numN/A
lift-/.f64N/A
inv-powN/A
sqr-powN/A
fabs-sqrN/A
sqr-powN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
lift--.f64N/A
div-subN/A
*-inversesN/A
lower--.f64N/A
lower-/.f6498.6
Applied rewrites98.6%
if 5e7 < (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) Initial program 100.0%
lift-/.f64N/A
lift-fabs.f64N/A
neg-fabsN/A
lift-fabs.f64N/A
div-fabsN/A
lower-fabs.f64N/A
lower-/.f64N/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
lift-fabs.f64N/A
lift-/.f64N/A
clear-numN/A
lift-/.f64N/A
inv-powN/A
sqr-powN/A
fabs-sqrN/A
sqr-powN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
frac-2negN/A
div-invN/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
inv-powN/A
sqr-powN/A
fabs-sqrN/A
sqr-powN/A
inv-powN/A
fabs-divN/A
neg-fabsN/A
fabs-divN/A
inv-powN/A
sqr-powN/A
Applied rewrites57.8%
Final simplification79.8%
(FPCore (x y) :precision binary64 (if (<= (fabs (/ (- y x) y)) 4e+19) (- 1.0 (/ x y)) (/ x y)))
double code(double x, double y) {
double tmp;
if (fabs(((y - x) / y)) <= 4e+19) {
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 (abs(((y - x) / y)) <= 4d+19) 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 (Math.abs(((y - x) / y)) <= 4e+19) {
tmp = 1.0 - (x / y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if math.fabs(((y - x) / y)) <= 4e+19: tmp = 1.0 - (x / y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (abs(Float64(Float64(y - x) / y)) <= 4e+19) 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 (abs(((y - x) / y)) <= 4e+19) tmp = 1.0 - (x / y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[Abs[N[(N[(y - x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision], 4e+19], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|\frac{y - x}{y}\right| \leq 4 \cdot 10^{+19}:\\
\;\;\;\;1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) < 4e19Initial program 100.0%
lift-/.f64N/A
lift-fabs.f64N/A
neg-fabsN/A
lift-fabs.f64N/A
div-fabsN/A
lower-fabs.f64N/A
lower-/.f64N/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
lift-fabs.f64N/A
lift-/.f64N/A
clear-numN/A
lift-/.f64N/A
inv-powN/A
sqr-powN/A
fabs-sqrN/A
sqr-powN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
lift--.f64N/A
div-subN/A
*-inversesN/A
lower--.f64N/A
lower-/.f6497.2
Applied rewrites97.2%
if 4e19 < (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) Initial program 100.0%
lift-/.f64N/A
lift-fabs.f64N/A
neg-fabsN/A
lift-fabs.f64N/A
div-fabsN/A
lower-fabs.f64N/A
lower-/.f64N/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
lift-fabs.f64N/A
lift-/.f64N/A
clear-numN/A
lift-/.f64N/A
inv-powN/A
sqr-powN/A
fabs-sqrN/A
sqr-powN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
frac-2negN/A
div-invN/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
inv-powN/A
sqr-powN/A
fabs-sqrN/A
sqr-powN/A
inv-powN/A
fabs-divN/A
neg-fabsN/A
fabs-divN/A
inv-powN/A
sqr-powN/A
Applied rewrites58.1%
Taylor expanded in y around 0
lower-/.f6458.1
Applied rewrites58.1%
Final simplification79.8%
(FPCore (x y) :precision binary64 (if (<= (fabs (/ (- y x) y)) 4500.0) 1.0 (/ x y)))
double code(double x, double y) {
double tmp;
if (fabs(((y - x) / y)) <= 4500.0) {
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 (abs(((y - x) / y)) <= 4500.0d0) 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 (Math.abs(((y - x) / y)) <= 4500.0) {
tmp = 1.0;
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if math.fabs(((y - x) / y)) <= 4500.0: tmp = 1.0 else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (abs(Float64(Float64(y - x) / y)) <= 4500.0) tmp = 1.0; else tmp = Float64(x / y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (abs(((y - x) / y)) <= 4500.0) tmp = 1.0; else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[Abs[N[(N[(y - x), $MachinePrecision] / y), $MachinePrecision]], $MachinePrecision], 4500.0], 1.0, N[(x / y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|\frac{y - x}{y}\right| \leq 4500:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) < 4500Initial program 100.0%
lift-/.f64N/A
lift-fabs.f64N/A
neg-fabsN/A
lift-fabs.f64N/A
div-fabsN/A
lower-fabs.f64N/A
lower-/.f64N/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
lift-fabs.f64N/A
lift-/.f64N/A
clear-numN/A
lift-/.f64N/A
inv-powN/A
sqr-powN/A
fabs-sqrN/A
sqr-powN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
lift--.f64N/A
div-subN/A
*-inversesN/A
lower--.f64N/A
lower-/.f6499.3
Applied rewrites99.3%
Taylor expanded in y around inf
Applied rewrites96.5%
if 4500 < (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) Initial program 100.0%
lift-/.f64N/A
lift-fabs.f64N/A
neg-fabsN/A
lift-fabs.f64N/A
div-fabsN/A
lower-fabs.f64N/A
lower-/.f64N/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
lift-fabs.f64N/A
lift-/.f64N/A
clear-numN/A
lift-/.f64N/A
inv-powN/A
sqr-powN/A
fabs-sqrN/A
sqr-powN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
frac-2negN/A
div-invN/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
inv-powN/A
sqr-powN/A
fabs-sqrN/A
sqr-powN/A
inv-powN/A
fabs-divN/A
neg-fabsN/A
fabs-divN/A
inv-powN/A
sqr-powN/A
Applied rewrites57.3%
Taylor expanded in y around 0
lower-/.f6456.3
Applied rewrites56.3%
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%
lift-/.f64N/A
lift-fabs.f64N/A
neg-fabsN/A
lift-fabs.f64N/A
div-fabsN/A
lower-fabs.f64N/A
lower-/.f64N/A
lift--.f64N/A
sub-negN/A
+-commutativeN/A
distribute-neg-inN/A
remove-double-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
lift-fabs.f64N/A
lift-/.f64N/A
clear-numN/A
lift-/.f64N/A
inv-powN/A
sqr-powN/A
fabs-sqrN/A
sqr-powN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
lift--.f64N/A
div-subN/A
*-inversesN/A
lower--.f64N/A
lower-/.f6472.8
Applied rewrites72.8%
Taylor expanded in y around inf
Applied rewrites53.3%
herbie shell --seed 2024277
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))