
(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 (- 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
(let* ((t_0 (* x (/ (/ x y) (- y x)))))
(if (<= y -8e-117)
(/ y (+ x y))
(if (<= y -5.8e-281)
t_0
(if (<= y 3.9e-193)
(/ x y)
(if (<= y 4.6e-169)
t_0
(if (<= y 6.5e-125) (/ x y) (* y (/ 1.0 (- y x))))))))))
double code(double x, double y) {
double t_0 = x * ((x / y) / (y - x));
double tmp;
if (y <= -8e-117) {
tmp = y / (x + y);
} else if (y <= -5.8e-281) {
tmp = t_0;
} else if (y <= 3.9e-193) {
tmp = x / y;
} else if (y <= 4.6e-169) {
tmp = t_0;
} else if (y <= 6.5e-125) {
tmp = x / y;
} else {
tmp = y * (1.0 / (y - x));
}
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 = x * ((x / y) / (y - x))
if (y <= (-8d-117)) then
tmp = y / (x + y)
else if (y <= (-5.8d-281)) then
tmp = t_0
else if (y <= 3.9d-193) then
tmp = x / y
else if (y <= 4.6d-169) then
tmp = t_0
else if (y <= 6.5d-125) then
tmp = x / y
else
tmp = y * (1.0d0 / (y - x))
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = x * ((x / y) / (y - x));
double tmp;
if (y <= -8e-117) {
tmp = y / (x + y);
} else if (y <= -5.8e-281) {
tmp = t_0;
} else if (y <= 3.9e-193) {
tmp = x / y;
} else if (y <= 4.6e-169) {
tmp = t_0;
} else if (y <= 6.5e-125) {
tmp = x / y;
} else {
tmp = y * (1.0 / (y - x));
}
return tmp;
}
def code(x, y): t_0 = x * ((x / y) / (y - x)) tmp = 0 if y <= -8e-117: tmp = y / (x + y) elif y <= -5.8e-281: tmp = t_0 elif y <= 3.9e-193: tmp = x / y elif y <= 4.6e-169: tmp = t_0 elif y <= 6.5e-125: tmp = x / y else: tmp = y * (1.0 / (y - x)) return tmp
function code(x, y) t_0 = Float64(x * Float64(Float64(x / y) / Float64(y - x))) tmp = 0.0 if (y <= -8e-117) tmp = Float64(y / Float64(x + y)); elseif (y <= -5.8e-281) tmp = t_0; elseif (y <= 3.9e-193) tmp = Float64(x / y); elseif (y <= 4.6e-169) tmp = t_0; elseif (y <= 6.5e-125) tmp = Float64(x / y); else tmp = Float64(y * Float64(1.0 / Float64(y - x))); end return tmp end
function tmp_2 = code(x, y) t_0 = x * ((x / y) / (y - x)); tmp = 0.0; if (y <= -8e-117) tmp = y / (x + y); elseif (y <= -5.8e-281) tmp = t_0; elseif (y <= 3.9e-193) tmp = x / y; elseif (y <= 4.6e-169) tmp = t_0; elseif (y <= 6.5e-125) tmp = x / y; else tmp = y * (1.0 / (y - x)); end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(x * N[(N[(x / y), $MachinePrecision] / N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -8e-117], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, -5.8e-281], t$95$0, If[LessEqual[y, 3.9e-193], N[(x / y), $MachinePrecision], If[LessEqual[y, 4.6e-169], t$95$0, If[LessEqual[y, 6.5e-125], N[(x / y), $MachinePrecision], N[(y * N[(1.0 / N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \frac{\frac{x}{y}}{y - x}\\
\mathbf{if}\;y \leq -8 \cdot 10^{-117}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{elif}\;y \leq -5.8 \cdot 10^{-281}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;y \leq 3.9 \cdot 10^{-193}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;y \leq 4.6 \cdot 10^{-169}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;y \leq 6.5 \cdot 10^{-125}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;y \cdot \frac{1}{y - x}\\
\end{array}
\end{array}
if y < -8.00000000000000024e-117Initial program 100.0%
div-inv99.8%
add-sqr-sqrt87.7%
fabs-sqr87.7%
add-sqr-sqrt88.5%
*-commutative88.5%
add-sqr-sqrt0.0%
fabs-sqr0.0%
add-sqr-sqrt12.6%
Applied egg-rr12.6%
flip--4.5%
associate-*r/4.5%
+-commutative4.5%
Applied egg-rr4.5%
Taylor expanded in x around 0 1.5%
unpow21.5%
mul-1-neg1.5%
distribute-rgt-neg-out1.5%
Simplified1.5%
associate-*r*1.9%
lft-mult-inverse1.9%
*-un-lft-identity1.9%
neg-sub01.9%
metadata-eval1.9%
sub-neg1.9%
metadata-eval1.9%
add-sqr-sqrt1.9%
sqrt-unprod1.5%
sqr-neg1.5%
sqrt-unprod0.0%
add-sqr-sqrt78.2%
Applied egg-rr78.2%
+-lft-identity78.2%
Simplified78.2%
if -8.00000000000000024e-117 < y < -5.7999999999999998e-281 or 3.8999999999999999e-193 < y < 4.6000000000000002e-169Initial program 99.9%
div-inv99.9%
add-sqr-sqrt62.0%
fabs-sqr62.0%
add-sqr-sqrt62.2%
*-commutative62.2%
add-sqr-sqrt0.0%
fabs-sqr0.0%
add-sqr-sqrt22.7%
Applied egg-rr22.7%
flip--20.9%
associate-*r/18.9%
+-commutative18.9%
Applied egg-rr18.9%
Taylor expanded in y around 0 19.1%
unpow219.1%
associate-/l*21.3%
associate-/r/21.4%
Simplified21.4%
associate-*l/19.1%
*-un-lft-identity19.1%
associate-*l/19.1%
frac-2neg19.1%
div-inv19.0%
associate-*l/19.0%
*-un-lft-identity19.0%
distribute-neg-frac19.0%
add-sqr-sqrt0.0%
sqrt-unprod23.4%
sqr-neg23.4%
sqrt-unprod38.7%
add-sqr-sqrt54.3%
frac-2neg54.3%
associate-*r/60.7%
distribute-neg-in60.7%
add-sqr-sqrt45.2%
sqrt-unprod60.6%
sqr-neg60.6%
sqrt-unprod15.6%
add-sqr-sqrt60.6%
sub-neg60.6%
Applied egg-rr60.6%
associate-*l*62.7%
associate-*r/62.7%
*-rgt-identity62.7%
Simplified62.7%
if -5.7999999999999998e-281 < y < 3.8999999999999999e-193 or 4.6000000000000002e-169 < y < 6.4999999999999999e-125Initial program 100.0%
div-inv99.7%
add-sqr-sqrt61.2%
fabs-sqr61.2%
add-sqr-sqrt61.6%
*-commutative61.6%
add-sqr-sqrt58.8%
fabs-sqr58.8%
add-sqr-sqrt74.3%
Applied egg-rr74.3%
Taylor expanded in y around 0 74.8%
if 6.4999999999999999e-125 < y Initial program 100.0%
div-inv99.6%
add-sqr-sqrt19.8%
fabs-sqr19.8%
add-sqr-sqrt21.0%
*-commutative21.0%
add-sqr-sqrt21.0%
fabs-sqr21.0%
add-sqr-sqrt21.0%
Applied egg-rr21.0%
flip--8.0%
associate-*r/8.0%
+-commutative8.0%
Applied egg-rr8.0%
Taylor expanded in y around inf 2.3%
neg-mul-12.3%
Simplified2.3%
frac-2neg2.3%
div-inv2.3%
remove-double-neg2.3%
distribute-neg-in2.3%
add-sqr-sqrt0.0%
sqrt-unprod34.0%
sqr-neg34.0%
sqrt-unprod61.9%
add-sqr-sqrt62.4%
sub-neg62.4%
Applied egg-rr62.4%
Final simplification70.3%
(FPCore (x y) :precision binary64 (if (<= y -8e-135) (/ y (+ x y)) (if (<= y 4.5e-125) (/ x y) (* y (/ 1.0 (- y x))))))
double code(double x, double y) {
double tmp;
if (y <= -8e-135) {
tmp = y / (x + y);
} else if (y <= 4.5e-125) {
tmp = x / y;
} else {
tmp = y * (1.0 / (y - x));
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-8d-135)) then
tmp = y / (x + y)
else if (y <= 4.5d-125) then
tmp = x / y
else
tmp = y * (1.0d0 / (y - x))
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -8e-135) {
tmp = y / (x + y);
} else if (y <= 4.5e-125) {
tmp = x / y;
} else {
tmp = y * (1.0 / (y - x));
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -8e-135: tmp = y / (x + y) elif y <= 4.5e-125: tmp = x / y else: tmp = y * (1.0 / (y - x)) return tmp
function code(x, y) tmp = 0.0 if (y <= -8e-135) tmp = Float64(y / Float64(x + y)); elseif (y <= 4.5e-125) tmp = Float64(x / y); else tmp = Float64(y * Float64(1.0 / Float64(y - x))); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -8e-135) tmp = y / (x + y); elseif (y <= 4.5e-125) tmp = x / y; else tmp = y * (1.0 / (y - x)); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -8e-135], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 4.5e-125], N[(x / y), $MachinePrecision], N[(y * N[(1.0 / N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -8 \cdot 10^{-135}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{elif}\;y \leq 4.5 \cdot 10^{-125}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;y \cdot \frac{1}{y - x}\\
\end{array}
\end{array}
if y < -8.0000000000000003e-135Initial program 100.0%
div-inv99.8%
add-sqr-sqrt88.0%
fabs-sqr88.0%
add-sqr-sqrt88.9%
*-commutative88.9%
add-sqr-sqrt0.0%
fabs-sqr0.0%
add-sqr-sqrt12.2%
Applied egg-rr12.2%
flip--4.4%
associate-*r/4.4%
+-commutative4.4%
Applied egg-rr4.4%
Taylor expanded in x around 0 1.6%
unpow21.6%
mul-1-neg1.6%
distribute-rgt-neg-out1.6%
Simplified1.6%
associate-*r*1.9%
lft-mult-inverse1.9%
*-un-lft-identity1.9%
neg-sub01.9%
metadata-eval1.9%
sub-neg1.9%
metadata-eval1.9%
add-sqr-sqrt1.9%
sqrt-unprod1.6%
sqr-neg1.6%
sqrt-unprod0.0%
add-sqr-sqrt76.5%
Applied egg-rr76.5%
+-lft-identity76.5%
Simplified76.5%
if -8.0000000000000003e-135 < y < 4.50000000000000012e-125Initial program 100.0%
div-inv99.8%
add-sqr-sqrt60.2%
fabs-sqr60.2%
add-sqr-sqrt60.5%
*-commutative60.5%
add-sqr-sqrt28.3%
fabs-sqr28.3%
add-sqr-sqrt48.4%
Applied egg-rr48.4%
Taylor expanded in y around 0 48.7%
if 4.50000000000000012e-125 < y Initial program 100.0%
div-inv99.6%
add-sqr-sqrt19.8%
fabs-sqr19.8%
add-sqr-sqrt21.0%
*-commutative21.0%
add-sqr-sqrt21.0%
fabs-sqr21.0%
add-sqr-sqrt21.0%
Applied egg-rr21.0%
flip--8.0%
associate-*r/8.0%
+-commutative8.0%
Applied egg-rr8.0%
Taylor expanded in y around inf 2.3%
neg-mul-12.3%
Simplified2.3%
frac-2neg2.3%
div-inv2.3%
remove-double-neg2.3%
distribute-neg-in2.3%
add-sqr-sqrt0.0%
sqrt-unprod34.0%
sqr-neg34.0%
sqrt-unprod61.9%
add-sqr-sqrt62.4%
sub-neg62.4%
Applied egg-rr62.4%
Final simplification63.6%
(FPCore (x y) :precision binary64 (if (or (<= y -8.2e-136) (not (<= y 7.8e-125))) (/ y (+ x y)) (/ x y)))
double code(double x, double y) {
double tmp;
if ((y <= -8.2e-136) || !(y <= 7.8e-125)) {
tmp = y / (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 ((y <= (-8.2d-136)) .or. (.not. (y <= 7.8d-125))) then
tmp = y / (x + y)
else
tmp = x / y
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -8.2e-136) || !(y <= 7.8e-125)) {
tmp = y / (x + y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -8.2e-136) or not (y <= 7.8e-125): tmp = y / (x + y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if ((y <= -8.2e-136) || !(y <= 7.8e-125)) tmp = Float64(y / Float64(x + y)); else tmp = Float64(x / y); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -8.2e-136) || ~((y <= 7.8e-125))) tmp = y / (x + y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -8.2e-136], N[Not[LessEqual[y, 7.8e-125]], $MachinePrecision]], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -8.2 \cdot 10^{-136} \lor \neg \left(y \leq 7.8 \cdot 10^{-125}\right):\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if y < -8.20000000000000051e-136 or 7.79999999999999965e-125 < y Initial program 100.0%
div-inv99.7%
add-sqr-sqrt58.8%
fabs-sqr58.8%
add-sqr-sqrt59.8%
*-commutative59.8%
add-sqr-sqrt9.0%
fabs-sqr9.0%
add-sqr-sqrt16.0%
Applied egg-rr16.0%
flip--5.9%
associate-*r/5.9%
+-commutative5.9%
Applied egg-rr5.9%
Taylor expanded in x around 0 1.7%
unpow21.7%
mul-1-neg1.7%
distribute-rgt-neg-out1.7%
Simplified1.7%
associate-*r*2.0%
lft-mult-inverse2.0%
*-un-lft-identity2.0%
neg-sub02.0%
metadata-eval2.0%
sub-neg2.0%
metadata-eval2.0%
add-sqr-sqrt1.1%
sqrt-unprod15.4%
sqr-neg15.4%
sqrt-unprod26.4%
add-sqr-sqrt70.5%
Applied egg-rr70.5%
+-lft-identity70.5%
Simplified70.5%
if -8.20000000000000051e-136 < y < 7.79999999999999965e-125Initial program 100.0%
div-inv99.8%
add-sqr-sqrt60.2%
fabs-sqr60.2%
add-sqr-sqrt60.5%
*-commutative60.5%
add-sqr-sqrt28.3%
fabs-sqr28.3%
add-sqr-sqrt48.4%
Applied egg-rr48.4%
Taylor expanded in y around 0 48.7%
Final simplification63.6%
(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%
div-inv99.7%
add-sqr-sqrt59.3%
fabs-sqr59.3%
add-sqr-sqrt60.0%
*-commutative60.0%
add-sqr-sqrt15.1%
fabs-sqr15.1%
add-sqr-sqrt26.3%
Applied egg-rr26.3%
Taylor expanded in y around 0 27.4%
Final simplification27.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%
div-inv99.7%
add-sqr-sqrt59.3%
fabs-sqr59.3%
add-sqr-sqrt60.0%
*-commutative60.0%
add-sqr-sqrt15.1%
fabs-sqr15.1%
add-sqr-sqrt26.3%
Applied egg-rr26.3%
Taylor expanded in y around inf 1.3%
Final simplification1.3%
herbie shell --seed 2023275
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))