
(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 (/ y (+ x y))))
(if (<= y -1.22e-36)
t_0
(if (<= y -5.5e-280)
(* x (/ (/ x y) (- y x)))
(if (<= y 1.75e-143) (/ (* x (/ x y)) (+ x y)) t_0)))))
double code(double x, double y) {
double t_0 = y / (x + y);
double tmp;
if (y <= -1.22e-36) {
tmp = t_0;
} else if (y <= -5.5e-280) {
tmp = x * ((x / y) / (y - x));
} else if (y <= 1.75e-143) {
tmp = (x * (x / y)) / (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 = y / (x + y)
if (y <= (-1.22d-36)) then
tmp = t_0
else if (y <= (-5.5d-280)) then
tmp = x * ((x / y) / (y - x))
else if (y <= 1.75d-143) then
tmp = (x * (x / y)) / (x + y)
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = y / (x + y);
double tmp;
if (y <= -1.22e-36) {
tmp = t_0;
} else if (y <= -5.5e-280) {
tmp = x * ((x / y) / (y - x));
} else if (y <= 1.75e-143) {
tmp = (x * (x / y)) / (x + y);
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = y / (x + y) tmp = 0 if y <= -1.22e-36: tmp = t_0 elif y <= -5.5e-280: tmp = x * ((x / y) / (y - x)) elif y <= 1.75e-143: tmp = (x * (x / y)) / (x + y) else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(y / Float64(x + y)) tmp = 0.0 if (y <= -1.22e-36) tmp = t_0; elseif (y <= -5.5e-280) tmp = Float64(x * Float64(Float64(x / y) / Float64(y - x))); elseif (y <= 1.75e-143) tmp = Float64(Float64(x * Float64(x / y)) / Float64(x + y)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = y / (x + y); tmp = 0.0; if (y <= -1.22e-36) tmp = t_0; elseif (y <= -5.5e-280) tmp = x * ((x / y) / (y - x)); elseif (y <= 1.75e-143) tmp = (x * (x / y)) / (x + y); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -1.22e-36], t$95$0, If[LessEqual[y, -5.5e-280], N[(x * N[(N[(x / y), $MachinePrecision] / N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 1.75e-143], N[(N[(x * N[(x / y), $MachinePrecision]), $MachinePrecision] / N[(x + y), $MachinePrecision]), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{y}{x + y}\\
\mathbf{if}\;y \leq -1.22 \cdot 10^{-36}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;y \leq -5.5 \cdot 10^{-280}:\\
\;\;\;\;x \cdot \frac{\frac{x}{y}}{y - x}\\
\mathbf{elif}\;y \leq 1.75 \cdot 10^{-143}:\\
\;\;\;\;\frac{x \cdot \frac{x}{y}}{x + y}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if y < -1.2200000000000001e-36 or 1.75000000000000003e-143 < y Initial program 100.0%
div-inv99.7%
add-sqr-sqrt48.4%
fabs-sqr48.4%
add-sqr-sqrt49.4%
*-commutative49.4%
add-sqr-sqrt8.1%
fabs-sqr8.1%
add-sqr-sqrt16.9%
Applied egg-rr16.9%
flip--5.9%
associate-*r/5.3%
+-commutative5.3%
Applied egg-rr5.3%
Taylor expanded in x around 0 1.4%
unpow21.4%
mul-1-neg1.4%
distribute-rgt-neg-out1.4%
Simplified1.4%
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-sqrt0.9%
sqrt-unprod15.9%
sqr-neg15.9%
sqrt-unprod35.4%
add-sqr-sqrt74.3%
Applied egg-rr74.3%
+-lft-identity74.3%
Simplified74.3%
if -1.2200000000000001e-36 < y < -5.50000000000000001e-280Initial program 100.0%
div-inv99.6%
add-sqr-sqrt70.6%
fabs-sqr70.6%
add-sqr-sqrt70.9%
*-commutative70.9%
add-sqr-sqrt0.0%
fabs-sqr0.0%
add-sqr-sqrt29.3%
Applied egg-rr29.3%
flip--29.5%
associate-*r/25.3%
+-commutative25.3%
Applied egg-rr25.3%
Taylor expanded in y around 0 25.7%
unpow225.7%
associate-/l*25.9%
associate-/r/25.9%
Simplified25.9%
associate-*l/25.7%
*-un-lft-identity25.7%
associate-*l/25.7%
frac-2neg25.7%
div-inv25.7%
associate-*l/25.7%
*-un-lft-identity25.7%
distribute-neg-frac25.7%
add-sqr-sqrt0.0%
sqrt-unprod31.1%
sqr-neg31.1%
sqrt-unprod45.3%
add-sqr-sqrt45.4%
frac-2neg45.4%
associate-*r/47.6%
distribute-neg-in47.6%
add-sqr-sqrt47.6%
sqrt-unprod47.1%
sqr-neg47.1%
sqrt-unprod0.0%
add-sqr-sqrt47.0%
sub-neg47.0%
Applied egg-rr47.0%
associate-*l*53.2%
associate-*r/53.2%
*-rgt-identity53.2%
Simplified53.2%
if -5.50000000000000001e-280 < y < 1.75000000000000003e-143Initial program 100.0%
div-inv99.8%
add-sqr-sqrt48.4%
fabs-sqr48.4%
add-sqr-sqrt48.9%
*-commutative48.9%
add-sqr-sqrt41.2%
fabs-sqr41.2%
add-sqr-sqrt59.2%
Applied egg-rr59.2%
flip--48.0%
associate-*r/48.0%
+-commutative48.0%
Applied egg-rr48.0%
Taylor expanded in y around 0 48.0%
unpow248.0%
associate-/l*59.4%
associate-/r/59.5%
Simplified59.5%
Final simplification68.4%
(FPCore (x y)
:precision binary64
(let* ((t_0 (/ y (+ x y))))
(if (<= y -2e-36)
t_0
(if (<= y -7e-281)
(* x (/ (/ x y) (- y x)))
(if (<= y 9e-139) (/ x y) t_0)))))
double code(double x, double y) {
double t_0 = y / (x + y);
double tmp;
if (y <= -2e-36) {
tmp = t_0;
} else if (y <= -7e-281) {
tmp = x * ((x / y) / (y - x));
} else if (y <= 9e-139) {
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 = y / (x + y)
if (y <= (-2d-36)) then
tmp = t_0
else if (y <= (-7d-281)) then
tmp = x * ((x / y) / (y - x))
else if (y <= 9d-139) 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 = y / (x + y);
double tmp;
if (y <= -2e-36) {
tmp = t_0;
} else if (y <= -7e-281) {
tmp = x * ((x / y) / (y - x));
} else if (y <= 9e-139) {
tmp = x / y;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y): t_0 = y / (x + y) tmp = 0 if y <= -2e-36: tmp = t_0 elif y <= -7e-281: tmp = x * ((x / y) / (y - x)) elif y <= 9e-139: tmp = x / y else: tmp = t_0 return tmp
function code(x, y) t_0 = Float64(y / Float64(x + y)) tmp = 0.0 if (y <= -2e-36) tmp = t_0; elseif (y <= -7e-281) tmp = Float64(x * Float64(Float64(x / y) / Float64(y - x))); elseif (y <= 9e-139) tmp = Float64(x / y); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y) t_0 = y / (x + y); tmp = 0.0; if (y <= -2e-36) tmp = t_0; elseif (y <= -7e-281) tmp = x * ((x / y) / (y - x)); elseif (y <= 9e-139) tmp = x / y; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -2e-36], t$95$0, If[LessEqual[y, -7e-281], N[(x * N[(N[(x / y), $MachinePrecision] / N[(y - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 9e-139], N[(x / y), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{y}{x + y}\\
\mathbf{if}\;y \leq -2 \cdot 10^{-36}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;y \leq -7 \cdot 10^{-281}:\\
\;\;\;\;x \cdot \frac{\frac{x}{y}}{y - x}\\
\mathbf{elif}\;y \leq 9 \cdot 10^{-139}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if y < -1.9999999999999999e-36 or 9.00000000000000046e-139 < y Initial program 100.0%
div-inv99.7%
add-sqr-sqrt48.4%
fabs-sqr48.4%
add-sqr-sqrt49.4%
*-commutative49.4%
add-sqr-sqrt8.1%
fabs-sqr8.1%
add-sqr-sqrt16.9%
Applied egg-rr16.9%
flip--5.9%
associate-*r/5.3%
+-commutative5.3%
Applied egg-rr5.3%
Taylor expanded in x around 0 1.4%
unpow21.4%
mul-1-neg1.4%
distribute-rgt-neg-out1.4%
Simplified1.4%
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-sqrt0.9%
sqrt-unprod15.9%
sqr-neg15.9%
sqrt-unprod35.4%
add-sqr-sqrt74.3%
Applied egg-rr74.3%
+-lft-identity74.3%
Simplified74.3%
if -1.9999999999999999e-36 < y < -7.00000000000000044e-281Initial program 100.0%
div-inv99.6%
add-sqr-sqrt70.6%
fabs-sqr70.6%
add-sqr-sqrt70.9%
*-commutative70.9%
add-sqr-sqrt0.0%
fabs-sqr0.0%
add-sqr-sqrt29.3%
Applied egg-rr29.3%
flip--29.5%
associate-*r/25.3%
+-commutative25.3%
Applied egg-rr25.3%
Taylor expanded in y around 0 25.7%
unpow225.7%
associate-/l*25.9%
associate-/r/25.9%
Simplified25.9%
associate-*l/25.7%
*-un-lft-identity25.7%
associate-*l/25.7%
frac-2neg25.7%
div-inv25.7%
associate-*l/25.7%
*-un-lft-identity25.7%
distribute-neg-frac25.7%
add-sqr-sqrt0.0%
sqrt-unprod31.1%
sqr-neg31.1%
sqrt-unprod45.3%
add-sqr-sqrt45.4%
frac-2neg45.4%
associate-*r/47.6%
distribute-neg-in47.6%
add-sqr-sqrt47.6%
sqrt-unprod47.1%
sqr-neg47.1%
sqrt-unprod0.0%
add-sqr-sqrt47.0%
sub-neg47.0%
Applied egg-rr47.0%
associate-*l*53.2%
associate-*r/53.2%
*-rgt-identity53.2%
Simplified53.2%
if -7.00000000000000044e-281 < y < 9.00000000000000046e-139Initial program 100.0%
div-inv99.8%
add-sqr-sqrt48.4%
fabs-sqr48.4%
add-sqr-sqrt48.9%
*-commutative48.9%
add-sqr-sqrt41.2%
fabs-sqr41.2%
add-sqr-sqrt59.2%
Applied egg-rr59.2%
Taylor expanded in y around 0 59.3%
Final simplification68.3%
(FPCore (x y) :precision binary64 (if (<= x -1.48e+141) (/ x y) (if (<= x 3.4e+91) (/ y (+ x y)) (/ x y))))
double code(double x, double y) {
double tmp;
if (x <= -1.48e+141) {
tmp = x / y;
} else if (x <= 3.4e+91) {
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 (x <= (-1.48d+141)) then
tmp = x / y
else if (x <= 3.4d+91) 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 (x <= -1.48e+141) {
tmp = x / y;
} else if (x <= 3.4e+91) {
tmp = y / (x + y);
} else {
tmp = x / y;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1.48e+141: tmp = x / y elif x <= 3.4e+91: tmp = y / (x + y) else: tmp = x / y return tmp
function code(x, y) tmp = 0.0 if (x <= -1.48e+141) tmp = Float64(x / y); elseif (x <= 3.4e+91) 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 (x <= -1.48e+141) tmp = x / y; elseif (x <= 3.4e+91) tmp = y / (x + y); else tmp = x / y; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1.48e+141], N[(x / y), $MachinePrecision], If[LessEqual[x, 3.4e+91], N[(y / N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x / y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.48 \cdot 10^{+141}:\\
\;\;\;\;\frac{x}{y}\\
\mathbf{elif}\;x \leq 3.4 \cdot 10^{+91}:\\
\;\;\;\;\frac{y}{x + y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{y}\\
\end{array}
\end{array}
if x < -1.48000000000000001e141 or 3.4000000000000001e91 < x Initial program 100.0%
div-inv99.8%
add-sqr-sqrt49.7%
fabs-sqr49.7%
add-sqr-sqrt50.2%
*-commutative50.2%
add-sqr-sqrt24.5%
fabs-sqr24.5%
add-sqr-sqrt52.9%
Applied egg-rr52.9%
Taylor expanded in y around 0 53.4%
if -1.48000000000000001e141 < x < 3.4000000000000001e91Initial program 100.0%
div-inv99.7%
add-sqr-sqrt53.4%
fabs-sqr53.4%
add-sqr-sqrt54.4%
*-commutative54.4%
add-sqr-sqrt6.2%
fabs-sqr6.2%
add-sqr-sqrt13.6%
Applied egg-rr13.6%
flip--10.9%
associate-*r/9.8%
+-commutative9.8%
Applied egg-rr9.8%
Taylor expanded in x around 0 1.7%
unpow21.7%
mul-1-neg1.7%
distribute-rgt-neg-out1.7%
Simplified1.7%
associate-*r*2.1%
lft-mult-inverse2.1%
*-un-lft-identity2.1%
neg-sub02.1%
metadata-eval2.1%
sub-neg2.1%
metadata-eval2.1%
add-sqr-sqrt1.2%
sqrt-unprod15.6%
sqr-neg15.6%
sqrt-unprod32.4%
add-sqr-sqrt72.0%
Applied egg-rr72.0%
+-lft-identity72.0%
Simplified72.0%
Final simplification66.3%
(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-sqrt52.3%
fabs-sqr52.3%
add-sqr-sqrt53.1%
*-commutative53.1%
add-sqr-sqrt11.7%
fabs-sqr11.7%
add-sqr-sqrt25.5%
Applied egg-rr25.5%
Taylor expanded in y around 0 26.7%
Final simplification26.7%
(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-sqrt52.3%
fabs-sqr52.3%
add-sqr-sqrt53.1%
*-commutative53.1%
add-sqr-sqrt11.7%
fabs-sqr11.7%
add-sqr-sqrt25.5%
Applied egg-rr25.5%
Taylor expanded in y around inf 1.3%
Final simplification1.3%
herbie shell --seed 2023196
(FPCore (x y)
:name "Numeric.LinearAlgebra.Util:formatSparse from hmatrix-0.16.1.5"
:precision binary64
(/ (fabs (- x y)) (fabs y)))