
(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 7 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 (/ (fma (/ a d) c b) d)))
(if (<= d -2.2e+86)
t_0
(if (<= d -1.35e-58)
(/ (+ (* b d) (* c a)) (fma c c (* d d)))
(if (<= d 8.8e-194)
(/ (fma (/ d c) b a) c)
(if (<= d 7.5e+95)
(/ 1.0 (/ (fma d d (* c c)) (fma d b (* c a))))
t_0))))))
double code(double a, double b, double c, double d) {
double t_0 = fma((a / d), c, b) / d;
double tmp;
if (d <= -2.2e+86) {
tmp = t_0;
} else if (d <= -1.35e-58) {
tmp = ((b * d) + (c * a)) / fma(c, c, (d * d));
} else if (d <= 8.8e-194) {
tmp = fma((d / c), b, a) / c;
} else if (d <= 7.5e+95) {
tmp = 1.0 / (fma(d, d, (c * c)) / fma(d, b, (c * a)));
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(Float64(a / d), c, b) / d) tmp = 0.0 if (d <= -2.2e+86) tmp = t_0; elseif (d <= -1.35e-58) tmp = Float64(Float64(Float64(b * d) + Float64(c * a)) / fma(c, c, Float64(d * d))); elseif (d <= 8.8e-194) tmp = Float64(fma(Float64(d / c), b, a) / c); elseif (d <= 7.5e+95) tmp = Float64(1.0 / Float64(fma(d, d, Float64(c * c)) / fma(d, b, Float64(c * a)))); else tmp = t_0; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -2.2e+86], t$95$0, If[LessEqual[d, -1.35e-58], N[(N[(N[(b * d), $MachinePrecision] + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[d, 8.8e-194], N[(N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 7.5e+95], N[(1.0 / N[(N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision] / N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{if}\;d \leq -2.2 \cdot 10^{+86}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq -1.35 \cdot 10^{-58}:\\
\;\;\;\;\frac{b \cdot d + c \cdot a}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\
\mathbf{elif}\;d \leq 8.8 \cdot 10^{-194}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\
\mathbf{elif}\;d \leq 7.5 \cdot 10^{+95}:\\
\;\;\;\;\frac{1}{\frac{\mathsf{fma}\left(d, d, c \cdot c\right)}{\mathsf{fma}\left(d, b, c \cdot a\right)}}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -2.20000000000000003e86 or 7.5000000000000001e95 < d Initial program 36.6%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6482.8
Applied rewrites82.8%
if -2.20000000000000003e86 < d < -1.3499999999999999e-58Initial program 83.8%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6483.9
Applied rewrites83.9%
if -1.3499999999999999e-58 < d < 8.8000000000000005e-194Initial program 69.6%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6469.6
Applied rewrites69.6%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6495.9
Applied rewrites95.9%
if 8.8000000000000005e-194 < d < 7.5000000000000001e95Initial program 80.8%
lift-/.f64N/A
clear-numN/A
lower-/.f64N/A
lower-/.f6480.8
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6480.8
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6480.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6480.8
Applied rewrites80.8%
Final simplification86.5%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (+ (* b d) (* c a)) (fma c c (* d d))))
(t_1 (/ (fma (/ a d) c b) d)))
(if (<= d -2.2e+86)
t_1
(if (<= d -1.35e-58)
t_0
(if (<= d 8.8e-194)
(/ (fma (/ d c) b a) c)
(if (<= d 7.5e+95) t_0 t_1))))))
double code(double a, double b, double c, double d) {
double t_0 = ((b * d) + (c * a)) / fma(c, c, (d * d));
double t_1 = fma((a / d), c, b) / d;
double tmp;
if (d <= -2.2e+86) {
tmp = t_1;
} else if (d <= -1.35e-58) {
tmp = t_0;
} else if (d <= 8.8e-194) {
tmp = fma((d / c), b, a) / c;
} else if (d <= 7.5e+95) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(Float64(Float64(b * d) + Float64(c * a)) / fma(c, c, Float64(d * d))) t_1 = Float64(fma(Float64(a / d), c, b) / d) tmp = 0.0 if (d <= -2.2e+86) tmp = t_1; elseif (d <= -1.35e-58) tmp = t_0; elseif (d <= 8.8e-194) tmp = Float64(fma(Float64(d / c), b, a) / c); elseif (d <= 7.5e+95) tmp = t_0; else tmp = t_1; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(b * d), $MachinePrecision] + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -2.2e+86], t$95$1, If[LessEqual[d, -1.35e-58], t$95$0, If[LessEqual[d, 8.8e-194], N[(N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[d, 7.5e+95], t$95$0, t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{b \cdot d + c \cdot a}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\
t_1 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{if}\;d \leq -2.2 \cdot 10^{+86}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;d \leq -1.35 \cdot 10^{-58}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 8.8 \cdot 10^{-194}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\
\mathbf{elif}\;d \leq 7.5 \cdot 10^{+95}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if d < -2.20000000000000003e86 or 7.5000000000000001e95 < d Initial program 36.6%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6482.8
Applied rewrites82.8%
if -2.20000000000000003e86 < d < -1.3499999999999999e-58 or 8.8000000000000005e-194 < d < 7.5000000000000001e95Initial program 81.6%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6481.6
Applied rewrites81.6%
if -1.3499999999999999e-58 < d < 8.8000000000000005e-194Initial program 69.6%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6469.6
Applied rewrites69.6%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6495.9
Applied rewrites95.9%
Final simplification86.5%
(FPCore (a b c d)
:precision binary64
(if (<= c -3e+99)
(/ a c)
(if (<= c -3.7e-47)
(* (/ c (fma c c (* d d))) a)
(if (<= c 4.7e+14) (/ (fma (/ a d) c b) d) (/ a c)))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -3e+99) {
tmp = a / c;
} else if (c <= -3.7e-47) {
tmp = (c / fma(c, c, (d * d))) * a;
} else if (c <= 4.7e+14) {
tmp = fma((a / d), c, b) / d;
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -3e+99) tmp = Float64(a / c); elseif (c <= -3.7e-47) tmp = Float64(Float64(c / fma(c, c, Float64(d * d))) * a); elseif (c <= 4.7e+14) tmp = Float64(fma(Float64(a / d), c, b) / d); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -3e+99], N[(a / c), $MachinePrecision], If[LessEqual[c, -3.7e-47], N[(N[(c / N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * a), $MachinePrecision], If[LessEqual[c, 4.7e+14], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -3 \cdot 10^{+99}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq -3.7 \cdot 10^{-47}:\\
\;\;\;\;\frac{c}{\mathsf{fma}\left(c, c, d \cdot d\right)} \cdot a\\
\mathbf{elif}\;c \leq 4.7 \cdot 10^{+14}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -3.00000000000000014e99 or 4.7e14 < c Initial program 48.5%
Taylor expanded in c around inf
lower-/.f6471.4
Applied rewrites71.4%
if -3.00000000000000014e99 < c < -3.7e-47Initial program 77.4%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6477.4
Applied rewrites77.4%
Taylor expanded in b around 0
*-commutativeN/A
associate-*l/N/A
lower-*.f64N/A
lower-/.f64N/A
unpow2N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6472.6
Applied rewrites72.6%
if -3.7e-47 < c < 4.7e14Initial program 71.0%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6481.4
Applied rewrites81.4%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (/ (fma (/ a d) c b) d))) (if (<= d -2e-14) t_0 (if (<= d 5.1e+95) (/ (fma (/ d c) b a) c) t_0))))
double code(double a, double b, double c, double d) {
double t_0 = fma((a / d), c, b) / d;
double tmp;
if (d <= -2e-14) {
tmp = t_0;
} else if (d <= 5.1e+95) {
tmp = fma((d / c), b, a) / c;
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(Float64(a / d), c, b) / d) tmp = 0.0 if (d <= -2e-14) tmp = t_0; elseif (d <= 5.1e+95) tmp = Float64(fma(Float64(d / c), b, a) / c); else tmp = t_0; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision]}, If[LessEqual[d, -2e-14], t$95$0, If[LessEqual[d, 5.1e+95], N[(N[(N[(d / c), $MachinePrecision] * b + a), $MachinePrecision] / c), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{if}\;d \leq -2 \cdot 10^{-14}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;d \leq 5.1 \cdot 10^{+95}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{d}{c}, b, a\right)}{c}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if d < -2e-14 or 5.10000000000000003e95 < d Initial program 44.2%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6480.2
Applied rewrites80.2%
if -2e-14 < d < 5.10000000000000003e95Initial program 75.2%
lift-+.f64N/A
lift-*.f64N/A
lower-fma.f6475.2
Applied rewrites75.2%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6483.0
Applied rewrites83.0%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (fma (/ b c) d a) c)))
(if (<= c -1750000000000.0)
t_0
(if (<= c 4.7e+14) (/ (fma (/ a d) c b) d) t_0))))
double code(double a, double b, double c, double d) {
double t_0 = fma((b / c), d, a) / c;
double tmp;
if (c <= -1750000000000.0) {
tmp = t_0;
} else if (c <= 4.7e+14) {
tmp = fma((a / d), c, b) / d;
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(Float64(b / c), d, a) / c) tmp = 0.0 if (c <= -1750000000000.0) tmp = t_0; elseif (c <= 4.7e+14) tmp = Float64(fma(Float64(a / d), c, b) / d); else tmp = t_0; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(N[(b / c), $MachinePrecision] * d + a), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -1750000000000.0], t$95$0, If[LessEqual[c, 4.7e+14], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\
\mathbf{if}\;c \leq -1750000000000:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq 4.7 \cdot 10^{+14}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if c < -1.75e12 or 4.7e14 < c Initial program 52.8%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6483.1
Applied rewrites83.1%
if -1.75e12 < c < 4.7e14Initial program 71.9%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6477.2
Applied rewrites77.2%
(FPCore (a b c d) :precision binary64 (if (<= d -16500000000000.0) (/ b d) (if (<= d 2.7e+118) (/ a c) (/ b d))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -16500000000000.0) {
tmp = b / d;
} else if (d <= 2.7e+118) {
tmp = a / c;
} else {
tmp = b / 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 (d <= (-16500000000000.0d0)) then
tmp = b / d
else if (d <= 2.7d+118) then
tmp = a / c
else
tmp = b / d
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if (d <= -16500000000000.0) {
tmp = b / d;
} else if (d <= 2.7e+118) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if d <= -16500000000000.0: tmp = b / d elif d <= 2.7e+118: tmp = a / c else: tmp = b / d return tmp
function code(a, b, c, d) tmp = 0.0 if (d <= -16500000000000.0) tmp = Float64(b / d); elseif (d <= 2.7e+118) tmp = Float64(a / c); else tmp = Float64(b / d); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if (d <= -16500000000000.0) tmp = b / d; elseif (d <= 2.7e+118) tmp = a / c; else tmp = b / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[LessEqual[d, -16500000000000.0], N[(b / d), $MachinePrecision], If[LessEqual[d, 2.7e+118], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -16500000000000:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;d \leq 2.7 \cdot 10^{+118}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if d < -1.65e13 or 2.7e118 < d Initial program 41.4%
Taylor expanded in c around 0
lower-/.f6478.7
Applied rewrites78.7%
if -1.65e13 < d < 2.7e118Initial program 74.9%
Taylor expanded in c around inf
lower-/.f6463.8
Applied rewrites63.8%
(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 62.7%
Taylor expanded in c around inf
lower-/.f6446.6
Applied rewrites46.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 2024267
(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))))