
(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%
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-/.f64N/A
lift--.f64N/A
div-subN/A
*-inversesN/A
lower--.f64N/A
lower-/.f64100.0
Applied rewrites100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (<= (/ (fabs (- y x)) (fabs y)) 40000000.0) (- 1.0 (/ x y)) (fabs (/ (- x) y))))
double code(double x, double y) {
double tmp;
if ((fabs((y - x)) / fabs(y)) <= 40000000.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)) / abs(y)) <= 40000000.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)) / Math.abs(y)) <= 40000000.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)) / math.fabs(y)) <= 40000000.0: tmp = 1.0 - (x / y) else: tmp = math.fabs((-x / y)) return tmp
function code(x, y) tmp = 0.0 if (Float64(abs(Float64(y - x)) / abs(y)) <= 40000000.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)) / abs(y)) <= 40000000.0) tmp = 1.0 - (x / y); else tmp = abs((-x / y)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / N[Abs[y], $MachinePrecision]), $MachinePrecision], 40000000.0], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[Abs[N[((-x) / y), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{\left|y - x\right|}{\left|y\right|} \leq 40000000:\\
\;\;\;\;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)) < 4e7Initial program 99.9%
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--.f6499.9
Applied rewrites99.9%
lift-fabs.f64N/A
lift-/.f64N/A
clear-numN/A
lift-/.f64N/A
fabs-divN/A
metadata-evalN/A
lift-fabs.f64N/A
inv-powN/A
metadata-evalN/A
pow-powN/A
pow2N/A
lift-fabs.f64N/A
lift-fabs.f64N/A
sqr-absN/A
pow-prod-downN/A
pow-prod-upN/A
metadata-evalN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
lift--.f64N/A
div-subN/A
*-inversesN/A
lower--.f64N/A
lower-/.f6497.9
Applied rewrites97.9%
if 4e7 < (/.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.5
Applied rewrites99.5%
Final simplification98.6%
(FPCore (x y) :precision binary64 (if (<= (/ (fabs (- y x)) (fabs y)) 2000.0) 1.0 (/ (- x) y)))
double code(double x, double y) {
double tmp;
if ((fabs((y - x)) / fabs(y)) <= 2000.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)) / abs(y)) <= 2000.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)) / Math.abs(y)) <= 2000.0) {
tmp = 1.0;
} else {
tmp = -x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if (math.fabs((y - x)) / math.fabs(y)) <= 2000.0: tmp = 1.0 else: tmp = -x / y return tmp
function code(x, y) tmp = 0.0 if (Float64(abs(Float64(y - x)) / abs(y)) <= 2000.0) tmp = 1.0; else tmp = Float64(Float64(-x) / y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((abs((y - x)) / abs(y)) <= 2000.0) tmp = 1.0; else tmp = -x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / N[Abs[y], $MachinePrecision]), $MachinePrecision], 2000.0], 1.0, N[((-x) / y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{\left|y - x\right|}{\left|y\right|} \leq 2000:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{-x}{y}\\
\end{array}
\end{array}
if (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) < 2e3Initial 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
fabs-divN/A
metadata-evalN/A
lift-fabs.f64N/A
inv-powN/A
metadata-evalN/A
pow-powN/A
pow2N/A
lift-fabs.f64N/A
lift-fabs.f64N/A
sqr-absN/A
pow-prod-downN/A
pow-prod-upN/A
metadata-evalN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
lift-/.f6499.3
Applied rewrites99.3%
Taylor expanded in y around inf
Applied rewrites96.3%
if 2e3 < (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) Initial program 99.9%
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--.f6499.9
Applied rewrites99.9%
lift-fabs.f64N/A
lift-/.f64N/A
clear-numN/A
lift-/.f64N/A
fabs-divN/A
metadata-evalN/A
lift-fabs.f64N/A
inv-powN/A
metadata-evalN/A
pow-powN/A
pow2N/A
lift-fabs.f64N/A
lift-fabs.f64N/A
sqr-absN/A
pow-prod-downN/A
pow-prod-upN/A
metadata-evalN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
lift-/.f6454.3
Applied rewrites54.3%
Taylor expanded in y around 0
mul-1-negN/A
lower-neg.f6452.4
Applied rewrites52.4%
Final simplification75.4%
(FPCore (x y) :precision binary64 (if (<= (/ (fabs (- y x)) (fabs y)) 2.0) 1.0 (/ x y)))
double code(double x, double y) {
double tmp;
if ((fabs((y - x)) / fabs(y)) <= 2.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)) / abs(y)) <= 2.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)) / Math.abs(y)) <= 2.0) {
tmp = 1.0;
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if (math.fabs((y - x)) / math.fabs(y)) <= 2.0: tmp = 1.0 else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (Float64(abs(Float64(y - x)) / abs(y)) <= 2.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)) / abs(y)) <= 2.0) tmp = 1.0; else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / N[Abs[y], $MachinePrecision]), $MachinePrecision], 2.0], 1.0, N[(x / y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{\left|y - x\right|}{\left|y\right|} \leq 2:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) < 2Initial 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
fabs-divN/A
metadata-evalN/A
lift-fabs.f64N/A
inv-powN/A
metadata-evalN/A
pow-powN/A
pow2N/A
lift-fabs.f64N/A
lift-fabs.f64N/A
sqr-absN/A
pow-prod-downN/A
pow-prod-upN/A
metadata-evalN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
lift-/.f64100.0
Applied rewrites100.0%
Taylor expanded in y around inf
Applied rewrites96.9%
if 2 < (/.f64 (fabs.f64 (-.f64 x y)) (fabs.f64 y)) Initial program 99.9%
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--.f6499.9
Applied rewrites99.9%
lift-fabs.f64N/A
lift-/.f64N/A
clear-numN/A
lift-/.f64N/A
fabs-divN/A
metadata-evalN/A
lift-fabs.f64N/A
inv-powN/A
metadata-evalN/A
pow-powN/A
pow2N/A
lift-fabs.f64N/A
lift-fabs.f64N/A
sqr-absN/A
pow-prod-downN/A
pow-prod-upN/A
metadata-evalN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
lift-/.f6453.9
Applied rewrites53.9%
Taylor expanded in y around 0
mul-1-negN/A
lower-neg.f6452.0
Applied rewrites52.0%
Applied rewrites45.0%
Final simplification72.0%
(FPCore (x y) :precision binary64 (let* ((t_0 (- 1.0 (/ x y)))) (if (<= x -9e+247) t_0 (if (<= x -1.7e+202) (/ x y) t_0))))
double code(double x, double y) {
double t_0 = 1.0 - (x / y);
double tmp;
if (x <= -9e+247) {
tmp = t_0;
} else if (x <= -1.7e+202) {
tmp = x / y;
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: tmp
t_0 = 1.0d0 - (x / y)
if (x <= (-9d+247)) then
tmp = t_0
else if (x <= (-1.7d+202)) then
tmp = x / y
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = 1.0 - (x / y);
double tmp;
if (x <= -9e+247) {
tmp = t_0;
} else if (x <= -1.7e+202) {
tmp = x / y;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = 1.0 - (x / y) tmp = 0 if x <= -9e+247: tmp = t_0 elif x <= -1.7e+202: tmp = x / y else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(1.0 - Float64(x / y)) tmp = 0.0 if (x <= -9e+247) tmp = t_0; elseif (x <= -1.7e+202) tmp = Float64(x / y); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = 1.0 - (x / y); tmp = 0.0; if (x <= -9e+247) tmp = t_0; elseif (x <= -1.7e+202) tmp = x / y; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -9e+247], t$95$0, If[LessEqual[x, -1.7e+202], N[(x / y), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 1 - \frac{x}{y}\\
\mathbf{if}\;x \leq -9 \cdot 10^{+247}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;x \leq -1.7 \cdot 10^{+202}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if x < -9.00000000000000004e247 or -1.7e202 < x 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
fabs-divN/A
metadata-evalN/A
lift-fabs.f64N/A
inv-powN/A
metadata-evalN/A
pow-powN/A
pow2N/A
lift-fabs.f64N/A
lift-fabs.f64N/A
sqr-absN/A
pow-prod-downN/A
pow-prod-upN/A
metadata-evalN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
lift--.f64N/A
div-subN/A
*-inversesN/A
lower--.f64N/A
lower-/.f6481.6
Applied rewrites81.6%
if -9.00000000000000004e247 < x < -1.7e202Initial 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
fabs-divN/A
metadata-evalN/A
lift-fabs.f64N/A
inv-powN/A
metadata-evalN/A
pow-powN/A
pow2N/A
lift-fabs.f64N/A
lift-fabs.f64N/A
sqr-absN/A
pow-prod-downN/A
pow-prod-upN/A
metadata-evalN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
lift-/.f647.9
Applied rewrites7.9%
Taylor expanded in y around 0
mul-1-negN/A
lower-neg.f647.9
Applied rewrites7.9%
Applied rewrites92.4%
(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
fabs-divN/A
metadata-evalN/A
lift-fabs.f64N/A
inv-powN/A
metadata-evalN/A
pow-powN/A
pow2N/A
lift-fabs.f64N/A
lift-fabs.f64N/A
sqr-absN/A
pow-prod-downN/A
pow-prod-upN/A
metadata-evalN/A
inv-powN/A
lift-/.f64N/A
clear-numN/A
lift-/.f6477.8
Applied rewrites77.8%
Taylor expanded in y around inf
Applied rewrites52.9%
herbie shell --seed 2024268
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))