
(FPCore (a b c d) :precision binary64 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = ((a * c) + (b * d)) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
def code(a, b, c, d): return ((a * c) + (b * d)) / ((c * c) + (d * d))
function code(a, b, c, d) return Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) end
function tmp = code(a, b, c, d) tmp = ((a * c) + (b * d)) / ((c * c) + (d * d)); end
code[a_, b_, c_, d_] := N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 11 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c d) :precision binary64 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = ((a * c) + (b * d)) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
return ((a * c) + (b * d)) / ((c * c) + (d * d));
}
def code(a, b, c, d): return ((a * c) + (b * d)) / ((c * c) + (d * d))
function code(a, b, c, d) return Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) end
function tmp = code(a, b, c, d) tmp = ((a * c) + (b * d)) / ((c * c) + (d * d)); end
code[a_, b_, c_, d_] := N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}
\end{array}
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(t_1 (/ 1.0 (/ d (fma a (/ c d) b)))))
(if (<= d -1.75e+149)
t_1
(if (<= d -3.5e-154)
t_0
(if (<= d 3.4e-114)
(/ (fma b (/ d c) a) c)
(if (<= d 1e+29) t_0 t_1))))))
double code(double a, double b, double c, double d) {
double t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
double t_1 = 1.0 / (d / fma(a, (c / d), b));
double tmp;
if (d <= -1.75e+149) {
tmp = t_1;
} else if (d <= -3.5e-154) {
tmp = t_0;
} else if (d <= 3.4e-114) {
tmp = fma(b, (d / c), a) / c;
} else if (d <= 1e+29) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) t_1 = Float64(1.0 / Float64(d / fma(a, Float64(c / d), b))) tmp = 0.0 if (d <= -1.75e+149) tmp = t_1; elseif (d <= -3.5e-154) tmp = t_0; elseif (d <= 3.4e-114) tmp = Float64(fma(b, Float64(d / c), a) / c); elseif (d <= 1e+29) tmp = t_0; else tmp = t_1; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(1.0 / N[(d / N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[d, -1.75e+149], t$95$1, If[LessEqual[d, -3.5e-154], t$95$0, If[LessEqual[d, 3.4e-114], N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 1e+29], t$95$0, t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
t_1 := \frac{1}{\frac{d}{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}}\\
\mathbf{if}\;d \leq -1.75 \cdot 10^{+149}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;d \leq -3.5 \cdot 10^{-154}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 3.4 \cdot 10^{-114}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{elif}\;d \leq 10^{+29}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if d < -1.75000000000000006e149 or 9.99999999999999914e28 < d Initial program 37.9%
Taylor expanded in d around inf 85.7%
clear-num86.4%
inv-pow86.4%
+-commutative86.4%
associate-/l*89.8%
fma-define89.8%
Applied egg-rr89.8%
unpow-189.8%
Simplified89.8%
if -1.75000000000000006e149 < d < -3.5000000000000001e-154 or 3.39999999999999981e-114 < d < 9.99999999999999914e28Initial program 88.1%
if -3.5000000000000001e-154 < d < 3.39999999999999981e-114Initial program 68.1%
Taylor expanded in c around inf 94.7%
+-commutative94.7%
associate-/l*94.9%
fma-define94.9%
Simplified94.9%
(FPCore (a b c d) :precision binary64 (if (<= (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) 5e+297) (/ (/ (fma c a (* b d)) (hypot c d)) (hypot c d)) (* (/ (fma a (/ c b) d) (hypot d c)) (/ b (hypot d c)))))
double code(double a, double b, double c, double d) {
double tmp;
if ((((a * c) + (b * d)) / ((c * c) + (d * d))) <= 5e+297) {
tmp = (fma(c, a, (b * d)) / hypot(c, d)) / hypot(c, d);
} else {
tmp = (fma(a, (c / b), d) / hypot(d, c)) * (b / hypot(d, c));
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) <= 5e+297) tmp = Float64(Float64(fma(c, a, Float64(b * d)) / hypot(c, d)) / hypot(c, d)); else tmp = Float64(Float64(fma(a, Float64(c / b), d) / hypot(d, c)) * Float64(b / hypot(d, c))); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 5e+297], N[(N[(N[(c * a + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision], N[(N[(N[(a * N[(c / b), $MachinePrecision] + d), $MachinePrecision] / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision] * N[(b / N[Sqrt[d ^ 2 + c ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq 5 \cdot 10^{+297}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(c, a, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{b}, d\right)}{\mathsf{hypot}\left(d, c\right)} \cdot \frac{b}{\mathsf{hypot}\left(d, c\right)}\\
\end{array}
\end{array}
if (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 4.9999999999999998e297Initial program 82.0%
*-un-lft-identity82.0%
add-sqr-sqrt82.0%
times-frac82.0%
hypot-define82.0%
fma-define82.0%
hypot-define97.7%
Applied egg-rr97.7%
*-commutative97.7%
associate-*l/97.7%
div-inv97.8%
fma-undefine97.8%
*-commutative97.8%
fma-define97.8%
*-commutative97.8%
Applied egg-rr97.8%
if 4.9999999999999998e297 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) Initial program 15.4%
add-cube-cbrt15.4%
pow315.4%
Applied egg-rr15.4%
Taylor expanded in b around inf 15.4%
associate-/l*15.4%
Simplified15.4%
*-commutative15.4%
add-sqr-sqrt15.4%
hypot-undefine15.4%
hypot-undefine15.4%
times-frac71.5%
+-commutative71.5%
fma-define71.5%
hypot-undefine23.9%
+-commutative23.9%
hypot-define71.5%
hypot-undefine23.9%
+-commutative23.9%
hypot-define71.5%
Applied egg-rr71.5%
Final simplification90.7%
(FPCore (a b c d) :precision binary64 (if (<= (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) 5e+297) (/ (/ (fma c a (* b d)) (hypot c d)) (hypot c d)) (/ (+ b (* a (/ c d))) d)))
double code(double a, double b, double c, double d) {
double tmp;
if ((((a * c) + (b * d)) / ((c * c) + (d * d))) <= 5e+297) {
tmp = (fma(c, a, (b * d)) / hypot(c, d)) / hypot(c, d);
} else {
tmp = (b + (a * (c / d))) / d;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) <= 5e+297) tmp = Float64(Float64(fma(c, a, Float64(b * d)) / hypot(c, d)) / hypot(c, d)); else tmp = Float64(Float64(b + Float64(a * Float64(c / d))) / d); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 5e+297], N[(N[(N[(c * a + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision], N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d} \leq 5 \cdot 10^{+297}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(c, a, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\
\end{array}
\end{array}
if (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 4.9999999999999998e297Initial program 82.0%
*-un-lft-identity82.0%
add-sqr-sqrt82.0%
times-frac82.0%
hypot-define82.0%
fma-define82.0%
hypot-define97.7%
Applied egg-rr97.7%
*-commutative97.7%
associate-*l/97.7%
div-inv97.8%
fma-undefine97.8%
*-commutative97.8%
fma-define97.8%
*-commutative97.8%
Applied egg-rr97.8%
if 4.9999999999999998e297 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) Initial program 15.4%
Taylor expanded in d around inf 59.6%
associate-/l*65.5%
Simplified65.5%
Final simplification89.1%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (+ (* a c) (* b d))))
(if (<= (/ t_0 (+ (* c c) (* d d))) 5e+297)
(/ (/ t_0 (hypot c d)) (hypot c d))
(/ (+ b (* a (/ c d))) d))))
double code(double a, double b, double c, double d) {
double t_0 = (a * c) + (b * d);
double tmp;
if ((t_0 / ((c * c) + (d * d))) <= 5e+297) {
tmp = (t_0 / hypot(c, d)) / hypot(c, d);
} else {
tmp = (b + (a * (c / d))) / d;
}
return tmp;
}
public static double code(double a, double b, double c, double d) {
double t_0 = (a * c) + (b * d);
double tmp;
if ((t_0 / ((c * c) + (d * d))) <= 5e+297) {
tmp = (t_0 / Math.hypot(c, d)) / Math.hypot(c, d);
} else {
tmp = (b + (a * (c / d))) / d;
}
return tmp;
}
def code(a, b, c, d): t_0 = (a * c) + (b * d) tmp = 0 if (t_0 / ((c * c) + (d * d))) <= 5e+297: tmp = (t_0 / math.hypot(c, d)) / math.hypot(c, d) else: tmp = (b + (a * (c / d))) / d return tmp
function code(a, b, c, d) t_0 = Float64(Float64(a * c) + Float64(b * d)) tmp = 0.0 if (Float64(t_0 / Float64(Float64(c * c) + Float64(d * d))) <= 5e+297) tmp = Float64(Float64(t_0 / hypot(c, d)) / hypot(c, d)); else tmp = Float64(Float64(b + Float64(a * Float64(c / d))) / d); end return tmp end
function tmp_2 = code(a, b, c, d) t_0 = (a * c) + (b * d); tmp = 0.0; if ((t_0 / ((c * c) + (d * d))) <= 5e+297) tmp = (t_0 / hypot(c, d)) / hypot(c, d); else tmp = (b + (a * (c / d))) / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(t$95$0 / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 5e+297], N[(N[(t$95$0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision], N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := a \cdot c + b \cdot d\\
\mathbf{if}\;\frac{t\_0}{c \cdot c + d \cdot d} \leq 5 \cdot 10^{+297}:\\
\;\;\;\;\frac{\frac{t\_0}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\
\end{array}
\end{array}
if (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 4.9999999999999998e297Initial program 82.0%
*-un-lft-identity82.0%
add-sqr-sqrt82.0%
times-frac82.0%
hypot-define82.0%
fma-define82.0%
hypot-define97.7%
Applied egg-rr97.7%
*-commutative97.7%
associate-*l/97.7%
div-inv97.8%
fma-undefine97.8%
*-commutative97.8%
fma-define97.8%
*-commutative97.8%
Applied egg-rr97.8%
fma-undefine97.8%
*-commutative97.8%
*-commutative97.8%
+-commutative97.8%
*-commutative97.8%
*-commutative97.8%
Applied egg-rr97.8%
if 4.9999999999999998e297 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) Initial program 15.4%
Taylor expanded in d around inf 59.6%
associate-/l*65.5%
Simplified65.5%
Final simplification89.1%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(t_1 (/ (+ b (* a (/ c d))) d)))
(if (<= d -6.1e+125)
t_1
(if (<= d -1.12e-153)
t_0
(if (<= d 3e-118)
(/ (fma b (/ d c) a) c)
(if (<= d 1.8e+29) t_0 t_1))))))
double code(double a, double b, double c, double d) {
double t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
double t_1 = (b + (a * (c / d))) / d;
double tmp;
if (d <= -6.1e+125) {
tmp = t_1;
} else if (d <= -1.12e-153) {
tmp = t_0;
} else if (d <= 3e-118) {
tmp = fma(b, (d / c), a) / c;
} else if (d <= 1.8e+29) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) t_1 = Float64(Float64(b + Float64(a * Float64(c / d))) / d) tmp = 0.0 if (d <= -6.1e+125) tmp = t_1; elseif (d <= -1.12e-153) tmp = t_0; elseif (d <= 3e-118) tmp = Float64(fma(b, Float64(d / c), a) / c); elseif (d <= 1.8e+29) tmp = t_0; else tmp = t_1; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -6.1e+125], t$95$1, If[LessEqual[d, -1.12e-153], t$95$0, If[LessEqual[d, 3e-118], N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 1.8e+29], t$95$0, t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
t_1 := \frac{b + a \cdot \frac{c}{d}}{d}\\
\mathbf{if}\;d \leq -6.1 \cdot 10^{+125}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;d \leq -1.12 \cdot 10^{-153}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 3 \cdot 10^{-118}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{elif}\;d \leq 1.8 \cdot 10^{+29}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if d < -6.09999999999999977e125 or 1.79999999999999988e29 < d Initial program 40.6%
Taylor expanded in d around inf 86.3%
associate-/l*89.4%
Simplified89.4%
if -6.09999999999999977e125 < d < -1.12000000000000005e-153 or 3.00000000000000018e-118 < d < 1.79999999999999988e29Initial program 87.5%
if -1.12000000000000005e-153 < d < 3.00000000000000018e-118Initial program 68.1%
Taylor expanded in c around inf 94.7%
+-commutative94.7%
associate-/l*94.9%
fma-define94.9%
Simplified94.9%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))
(t_1 (/ (+ b (* a (/ c d))) d)))
(if (<= d -7.8e+127)
t_1
(if (<= d -9.5e-149)
t_0
(if (<= d 1.55e-116)
(/ (+ a (* b (/ d c))) c)
(if (<= d 1.8e+29) t_0 t_1))))))
double code(double a, double b, double c, double d) {
double t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
double t_1 = (b + (a * (c / d))) / d;
double tmp;
if (d <= -7.8e+127) {
tmp = t_1;
} else if (d <= -9.5e-149) {
tmp = t_0;
} else if (d <= 1.55e-116) {
tmp = (a + (b * (d / c))) / c;
} else if (d <= 1.8e+29) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d))
t_1 = (b + (a * (c / d))) / d
if (d <= (-7.8d+127)) then
tmp = t_1
else if (d <= (-9.5d-149)) then
tmp = t_0
else if (d <= 1.55d-116) then
tmp = (a + (b * (d / c))) / c
else if (d <= 1.8d+29) then
tmp = t_0
else
tmp = t_1
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d));
double t_1 = (b + (a * (c / d))) / d;
double tmp;
if (d <= -7.8e+127) {
tmp = t_1;
} else if (d <= -9.5e-149) {
tmp = t_0;
} else if (d <= 1.55e-116) {
tmp = (a + (b * (d / c))) / c;
} else if (d <= 1.8e+29) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
def code(a, b, c, d): t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d)) t_1 = (b + (a * (c / d))) / d tmp = 0 if d <= -7.8e+127: tmp = t_1 elif d <= -9.5e-149: tmp = t_0 elif d <= 1.55e-116: tmp = (a + (b * (d / c))) / c elif d <= 1.8e+29: tmp = t_0 else: tmp = t_1 return tmp
function code(a, b, c, d) t_0 = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))) t_1 = Float64(Float64(b + Float64(a * Float64(c / d))) / d) tmp = 0.0 if (d <= -7.8e+127) tmp = t_1; elseif (d <= -9.5e-149) tmp = t_0; elseif (d <= 1.55e-116) tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / c); elseif (d <= 1.8e+29) tmp = t_0; else tmp = t_1; end return tmp end
function tmp_2 = code(a, b, c, d) t_0 = ((a * c) + (b * d)) / ((c * c) + (d * d)); t_1 = (b + (a * (c / d))) / d; tmp = 0.0; if (d <= -7.8e+127) tmp = t_1; elseif (d <= -9.5e-149) tmp = t_0; elseif (d <= 1.55e-116) tmp = (a + (b * (d / c))) / c; elseif (d <= 1.8e+29) tmp = t_0; else tmp = t_1; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -7.8e+127], t$95$1, If[LessEqual[d, -9.5e-149], t$95$0, If[LessEqual[d, 1.55e-116], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 1.8e+29], t$95$0, t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
t_1 := \frac{b + a \cdot \frac{c}{d}}{d}\\
\mathbf{if}\;d \leq -7.8 \cdot 10^{+127}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;d \leq -9.5 \cdot 10^{-149}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 1.55 \cdot 10^{-116}:\\
\;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\
\mathbf{elif}\;d \leq 1.8 \cdot 10^{+29}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if d < -7.79999999999999962e127 or 1.79999999999999988e29 < d Initial program 40.6%
Taylor expanded in d around inf 86.3%
associate-/l*89.4%
Simplified89.4%
if -7.79999999999999962e127 < d < -9.50000000000000034e-149 or 1.55000000000000009e-116 < d < 1.79999999999999988e29Initial program 87.5%
if -9.50000000000000034e-149 < d < 1.55000000000000009e-116Initial program 68.1%
Taylor expanded in c around inf 94.7%
+-commutative94.7%
associate-/l*94.9%
fma-define94.9%
Simplified94.9%
fma-undefine94.9%
Applied egg-rr94.9%
Final simplification90.6%
(FPCore (a b c d) :precision binary64 (if (or (<= d -1.12e-10) (not (<= d 2.9e-7))) (/ (+ b (* a (/ c d))) d) (/ (+ a (* b (/ d c))) c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -1.12e-10) || !(d <= 2.9e-7)) {
tmp = (b + (a * (c / d))) / d;
} else {
tmp = (a + (b * (d / c))) / c;
}
return tmp;
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
real(8) :: tmp
if ((d <= (-1.12d-10)) .or. (.not. (d <= 2.9d-7))) then
tmp = (b + (a * (c / d))) / d
else
tmp = (a + (b * (d / c))) / c
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -1.12e-10) || !(d <= 2.9e-7)) {
tmp = (b + (a * (c / d))) / d;
} else {
tmp = (a + (b * (d / c))) / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (d <= -1.12e-10) or not (d <= 2.9e-7): tmp = (b + (a * (c / d))) / d else: tmp = (a + (b * (d / c))) / c return tmp
function code(a, b, c, d) tmp = 0.0 if ((d <= -1.12e-10) || !(d <= 2.9e-7)) tmp = Float64(Float64(b + Float64(a * Float64(c / d))) / d); else tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / c); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if ((d <= -1.12e-10) || ~((d <= 2.9e-7))) tmp = (b + (a * (c / d))) / d; else tmp = (a + (b * (d / c))) / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -1.12e-10], N[Not[LessEqual[d, 2.9e-7]], $MachinePrecision]], N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.12 \cdot 10^{-10} \lor \neg \left(d \leq 2.9 \cdot 10^{-7}\right):\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\
\end{array}
\end{array}
if d < -1.12e-10 or 2.8999999999999998e-7 < d Initial program 53.3%
Taylor expanded in d around inf 82.9%
associate-/l*85.2%
Simplified85.2%
if -1.12e-10 < d < 2.8999999999999998e-7Initial program 74.8%
Taylor expanded in c around inf 87.4%
+-commutative87.4%
associate-/l*87.5%
fma-define87.5%
Simplified87.5%
fma-undefine87.5%
Applied egg-rr87.5%
Final simplification86.4%
(FPCore (a b c d) :precision binary64 (if (or (<= d -1.35e-9) (not (<= d 3.5e-7))) (/ (+ b (* a (/ c d))) d) (/ (+ a (/ (* b d) c)) c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -1.35e-9) || !(d <= 3.5e-7)) {
tmp = (b + (a * (c / d))) / d;
} else {
tmp = (a + ((b * d) / c)) / c;
}
return tmp;
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
real(8) :: tmp
if ((d <= (-1.35d-9)) .or. (.not. (d <= 3.5d-7))) then
tmp = (b + (a * (c / d))) / d
else
tmp = (a + ((b * d) / c)) / c
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -1.35e-9) || !(d <= 3.5e-7)) {
tmp = (b + (a * (c / d))) / d;
} else {
tmp = (a + ((b * d) / c)) / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (d <= -1.35e-9) or not (d <= 3.5e-7): tmp = (b + (a * (c / d))) / d else: tmp = (a + ((b * d) / c)) / c return tmp
function code(a, b, c, d) tmp = 0.0 if ((d <= -1.35e-9) || !(d <= 3.5e-7)) tmp = Float64(Float64(b + Float64(a * Float64(c / d))) / d); else tmp = Float64(Float64(a + Float64(Float64(b * d) / c)) / c); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if ((d <= -1.35e-9) || ~((d <= 3.5e-7))) tmp = (b + (a * (c / d))) / d; else tmp = (a + ((b * d) / c)) / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -1.35e-9], N[Not[LessEqual[d, 3.5e-7]], $MachinePrecision]], N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], N[(N[(a + N[(N[(b * d), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -1.35 \cdot 10^{-9} \lor \neg \left(d \leq 3.5 \cdot 10^{-7}\right):\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a + \frac{b \cdot d}{c}}{c}\\
\end{array}
\end{array}
if d < -1.3500000000000001e-9 or 3.49999999999999984e-7 < d Initial program 53.3%
Taylor expanded in d around inf 82.9%
associate-/l*85.2%
Simplified85.2%
if -1.3500000000000001e-9 < d < 3.49999999999999984e-7Initial program 74.8%
Taylor expanded in c around inf 87.4%
*-commutative87.4%
Simplified87.4%
Final simplification86.3%
(FPCore (a b c d) :precision binary64 (if (or (<= d -116000000000.0) (not (<= d 4.2e-7))) (/ b d) (/ (+ a (/ (* b d) c)) c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -116000000000.0) || !(d <= 4.2e-7)) {
tmp = b / d;
} else {
tmp = (a + ((b * d) / c)) / c;
}
return tmp;
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
real(8) :: tmp
if ((d <= (-116000000000.0d0)) .or. (.not. (d <= 4.2d-7))) then
tmp = b / d
else
tmp = (a + ((b * d) / c)) / c
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -116000000000.0) || !(d <= 4.2e-7)) {
tmp = b / d;
} else {
tmp = (a + ((b * d) / c)) / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (d <= -116000000000.0) or not (d <= 4.2e-7): tmp = b / d else: tmp = (a + ((b * d) / c)) / c return tmp
function code(a, b, c, d) tmp = 0.0 if ((d <= -116000000000.0) || !(d <= 4.2e-7)) tmp = Float64(b / d); else tmp = Float64(Float64(a + Float64(Float64(b * d) / c)) / c); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if ((d <= -116000000000.0) || ~((d <= 4.2e-7))) tmp = b / d; else tmp = (a + ((b * d) / c)) / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -116000000000.0], N[Not[LessEqual[d, 4.2e-7]], $MachinePrecision]], N[(b / d), $MachinePrecision], N[(N[(a + N[(N[(b * d), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -116000000000 \lor \neg \left(d \leq 4.2 \cdot 10^{-7}\right):\\
\;\;\;\;\frac{b}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a + \frac{b \cdot d}{c}}{c}\\
\end{array}
\end{array}
if d < -1.16e11 or 4.2e-7 < d Initial program 51.4%
Taylor expanded in c around 0 76.8%
if -1.16e11 < d < 4.2e-7Initial program 75.7%
Taylor expanded in c around inf 85.0%
*-commutative85.0%
Simplified85.0%
Final simplification81.1%
(FPCore (a b c d) :precision binary64 (if (or (<= d -2.3e-45) (not (<= d 4e-8))) (/ b d) (/ a c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -2.3e-45) || !(d <= 4e-8)) {
tmp = b / d;
} else {
tmp = a / c;
}
return tmp;
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
real(8) :: tmp
if ((d <= (-2.3d-45)) .or. (.not. (d <= 4d-8))) then
tmp = b / d
else
tmp = a / c
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -2.3e-45) || !(d <= 4e-8)) {
tmp = b / d;
} else {
tmp = a / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (d <= -2.3e-45) or not (d <= 4e-8): tmp = b / d else: tmp = a / c return tmp
function code(a, b, c, d) tmp = 0.0 if ((d <= -2.3e-45) || !(d <= 4e-8)) tmp = Float64(b / d); else tmp = Float64(a / c); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if ((d <= -2.3e-45) || ~((d <= 4e-8))) tmp = b / d; else tmp = a / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -2.3e-45], N[Not[LessEqual[d, 4e-8]], $MachinePrecision]], N[(b / d), $MachinePrecision], N[(a / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -2.3 \cdot 10^{-45} \lor \neg \left(d \leq 4 \cdot 10^{-8}\right):\\
\;\;\;\;\frac{b}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if d < -2.29999999999999992e-45 or 4.0000000000000001e-8 < d Initial program 55.0%
Taylor expanded in c around 0 73.1%
if -2.29999999999999992e-45 < d < 4.0000000000000001e-8Initial program 74.2%
Taylor expanded in c around inf 67.3%
Final simplification70.3%
(FPCore (a b c d) :precision binary64 (/ a c))
double code(double a, double b, double c, double d) {
return a / c;
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
code = a / c
end function
public static double code(double a, double b, double c, double d) {
return a / c;
}
def code(a, b, c, d): return a / c
function code(a, b, c, d) return Float64(a / c) end
function tmp = code(a, b, c, d) tmp = a / c; end
code[a_, b_, c_, d_] := N[(a / c), $MachinePrecision]
\begin{array}{l}
\\
\frac{a}{c}
\end{array}
Initial program 64.1%
Taylor expanded in c around inf 40.6%
(FPCore (a b c d) :precision binary64 (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d))))))
double code(double a, double b, double c, double d) {
double tmp;
if (fabs(d) < fabs(c)) {
tmp = (a + (b * (d / c))) / (c + (d * (d / c)));
} else {
tmp = (b + (a * (c / d))) / (d + (c * (c / d)));
}
return tmp;
}
real(8) function code(a, b, c, d)
real(8), intent (in) :: a
real(8), intent (in) :: b
real(8), intent (in) :: c
real(8), intent (in) :: d
real(8) :: tmp
if (abs(d) < abs(c)) then
tmp = (a + (b * (d / c))) / (c + (d * (d / c)))
else
tmp = (b + (a * (c / d))) / (d + (c * (c / d)))
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if (Math.abs(d) < Math.abs(c)) {
tmp = (a + (b * (d / c))) / (c + (d * (d / c)));
} else {
tmp = (b + (a * (c / d))) / (d + (c * (c / d)));
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if math.fabs(d) < math.fabs(c): tmp = (a + (b * (d / c))) / (c + (d * (d / c))) else: tmp = (b + (a * (c / d))) / (d + (c * (c / d))) return tmp
function code(a, b, c, d) tmp = 0.0 if (abs(d) < abs(c)) tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / Float64(c + Float64(d * Float64(d / c)))); else tmp = Float64(Float64(b + Float64(a * Float64(c / d))) / Float64(d + Float64(c * Float64(c / d)))); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if (abs(d) < abs(c)) tmp = (a + (b * (d / c))) / (c + (d * (d / c))); else tmp = (b + (a * (c / d))) / (d + (c * (c / d))); end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Less[N[Abs[d], $MachinePrecision], N[Abs[c], $MachinePrecision]], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(c + N[(d * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(d + N[(c * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\left|d\right| < \left|c\right|:\\
\;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\
\mathbf{else}:\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\
\end{array}
\end{array}
herbie shell --seed 2024132
(FPCore (a b c d)
:name "Complex division, real part"
:precision binary64
:alt
(! :herbie-platform default (if (< (fabs d) (fabs c)) (/ (+ a (* b (/ d c))) (+ c (* d (/ d c)))) (/ (+ b (* a (/ c d))) (+ d (* c (/ c d))))))
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d))))