
(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 8 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 d b (* c a)) (+ (* c c) (* d d))))
(t_1 (/ (fma b (/ d c) a) c)))
(if (<= c -1.35e+126)
t_1
(if (<= c -8e-117)
t_0
(if (<= c 2.7e-154)
(/ (+ b (/ (* c a) d)) d)
(if (<= c 5.5e+59) t_0 t_1))))))
double code(double a, double b, double c, double d) {
double t_0 = fma(d, b, (c * a)) / ((c * c) + (d * d));
double t_1 = fma(b, (d / c), a) / c;
double tmp;
if (c <= -1.35e+126) {
tmp = t_1;
} else if (c <= -8e-117) {
tmp = t_0;
} else if (c <= 2.7e-154) {
tmp = (b + ((c * a) / d)) / d;
} else if (c <= 5.5e+59) {
tmp = t_0;
} else {
tmp = t_1;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(d, b, Float64(c * a)) / Float64(Float64(c * c) + Float64(d * d))) t_1 = Float64(fma(b, Float64(d / c), a) / c) tmp = 0.0 if (c <= -1.35e+126) tmp = t_1; elseif (c <= -8e-117) tmp = t_0; elseif (c <= 2.7e-154) tmp = Float64(Float64(b + Float64(Float64(c * a) / d)) / d); elseif (c <= 5.5e+59) tmp = t_0; else tmp = t_1; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -1.35e+126], t$95$1, If[LessEqual[c, -8e-117], t$95$0, If[LessEqual[c, 2.7e-154], N[(N[(b + N[(N[(c * a), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, 5.5e+59], t$95$0, t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{c \cdot c + d \cdot d}\\
t_1 := \frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{if}\;c \leq -1.35 \cdot 10^{+126}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;c \leq -8 \cdot 10^{-117}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq 2.7 \cdot 10^{-154}:\\
\;\;\;\;\frac{b + \frac{c \cdot a}{d}}{d}\\
\mathbf{elif}\;c \leq 5.5 \cdot 10^{+59}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if c < -1.35000000000000001e126 or 5.4999999999999999e59 < c Initial program 48.7%
Taylor expanded in c around inf
/-lowering-/.f64N/A
+-commutativeN/A
associate-/l*N/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6486.8
Simplified86.8%
if -1.35000000000000001e126 < c < -8.00000000000000024e-117 or 2.69999999999999989e-154 < c < 5.4999999999999999e59Initial program 84.7%
+-commutativeN/A
*-commutativeN/A
accelerator-lowering-fma.f64N/A
*-lowering-*.f6484.7
Applied egg-rr84.7%
if -8.00000000000000024e-117 < c < 2.69999999999999989e-154Initial program 72.4%
Taylor expanded in d around inf
/-lowering-/.f64N/A
+-commutativeN/A
associate-/l*N/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6494.1
Simplified94.1%
+-lowering-+.f64N/A
associate-*r/N/A
/-lowering-/.f64N/A
*-commutativeN/A
*-lowering-*.f6494.2
Applied egg-rr94.2%
Final simplification88.0%
(FPCore (a b c d) :precision binary64 (if (<= c -0.31) (/ (fma d (/ b c) a) c) (if (<= c 2.6e-30) (/ (+ b (/ (* c a) d)) d) (/ (fma b (/ d c) a) c))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -0.31) {
tmp = fma(d, (b / c), a) / c;
} else if (c <= 2.6e-30) {
tmp = (b + ((c * a) / d)) / d;
} else {
tmp = fma(b, (d / c), a) / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -0.31) tmp = Float64(fma(d, Float64(b / c), a) / c); elseif (c <= 2.6e-30) tmp = Float64(Float64(b + Float64(Float64(c * a) / d)) / d); else tmp = Float64(fma(b, Float64(d / c), a) / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -0.31], N[(N[(d * N[(b / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[c, 2.6e-30], N[(N[(b + N[(N[(c * a), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -0.31:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, \frac{b}{c}, a\right)}{c}\\
\mathbf{elif}\;c \leq 2.6 \cdot 10^{-30}:\\
\;\;\;\;\frac{b + \frac{c \cdot a}{d}}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\end{array}
\end{array}
if c < -0.309999999999999998Initial program 57.0%
+-commutativeN/A
*-commutativeN/A
accelerator-lowering-fma.f64N/A
*-lowering-*.f6457.0
Applied egg-rr57.0%
Taylor expanded in c around inf
/-lowering-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6482.1
Simplified82.1%
if -0.309999999999999998 < c < 2.59999999999999987e-30Initial program 77.3%
Taylor expanded in d around inf
/-lowering-/.f64N/A
+-commutativeN/A
associate-/l*N/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6484.3
Simplified84.3%
+-lowering-+.f64N/A
associate-*r/N/A
/-lowering-/.f64N/A
*-commutativeN/A
*-lowering-*.f6484.4
Applied egg-rr84.4%
if 2.59999999999999987e-30 < c Initial program 61.9%
Taylor expanded in c around inf
/-lowering-/.f64N/A
+-commutativeN/A
associate-/l*N/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6480.0
Simplified80.0%
Final simplification82.5%
(FPCore (a b c d) :precision binary64 (if (<= c -0.46) (/ (fma d (/ b c) a) c) (if (<= c 2.2e-30) (/ (fma a (/ c d) b) d) (/ (fma b (/ d c) a) c))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -0.46) {
tmp = fma(d, (b / c), a) / c;
} else if (c <= 2.2e-30) {
tmp = fma(a, (c / d), b) / d;
} else {
tmp = fma(b, (d / c), a) / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -0.46) tmp = Float64(fma(d, Float64(b / c), a) / c); elseif (c <= 2.2e-30) tmp = Float64(fma(a, Float64(c / d), b) / d); else tmp = Float64(fma(b, Float64(d / c), a) / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -0.46], N[(N[(d * N[(b / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[c, 2.2e-30], N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -0.46:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, \frac{b}{c}, a\right)}{c}\\
\mathbf{elif}\;c \leq 2.2 \cdot 10^{-30}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\end{array}
\end{array}
if c < -0.46000000000000002Initial program 57.0%
+-commutativeN/A
*-commutativeN/A
accelerator-lowering-fma.f64N/A
*-lowering-*.f6457.0
Applied egg-rr57.0%
Taylor expanded in c around inf
/-lowering-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6482.1
Simplified82.1%
if -0.46000000000000002 < c < 2.19999999999999983e-30Initial program 77.3%
Taylor expanded in d around inf
/-lowering-/.f64N/A
+-commutativeN/A
associate-/l*N/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6484.3
Simplified84.3%
if 2.19999999999999983e-30 < c Initial program 61.9%
Taylor expanded in c around inf
/-lowering-/.f64N/A
+-commutativeN/A
associate-/l*N/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6480.0
Simplified80.0%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (/ (fma b (/ d c) a) c))) (if (<= c -0.48) t_0 (if (<= c 2.6e-30) (/ (fma a (/ c d) b) d) t_0))))
double code(double a, double b, double c, double d) {
double t_0 = fma(b, (d / c), a) / c;
double tmp;
if (c <= -0.48) {
tmp = t_0;
} else if (c <= 2.6e-30) {
tmp = fma(a, (c / d), b) / d;
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(b, Float64(d / c), a) / c) tmp = 0.0 if (c <= -0.48) tmp = t_0; elseif (c <= 2.6e-30) tmp = Float64(fma(a, Float64(c / d), b) / d); else tmp = t_0; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(b * N[(d / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -0.48], t$95$0, If[LessEqual[c, 2.6e-30], N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{\mathsf{fma}\left(b, \frac{d}{c}, a\right)}{c}\\
\mathbf{if}\;c \leq -0.48:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq 2.6 \cdot 10^{-30}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if c < -0.47999999999999998 or 2.59999999999999987e-30 < c Initial program 59.5%
Taylor expanded in c around inf
/-lowering-/.f64N/A
+-commutativeN/A
associate-/l*N/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6480.4
Simplified80.4%
if -0.47999999999999998 < c < 2.59999999999999987e-30Initial program 77.3%
Taylor expanded in d around inf
/-lowering-/.f64N/A
+-commutativeN/A
associate-/l*N/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6484.3
Simplified84.3%
(FPCore (a b c d) :precision binary64 (if (<= c -0.62) (/ a c) (if (<= c 2e+41) (/ (fma a (/ c d) b) d) (/ a c))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -0.62) {
tmp = a / c;
} else if (c <= 2e+41) {
tmp = fma(a, (c / d), b) / d;
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -0.62) tmp = Float64(a / c); elseif (c <= 2e+41) tmp = Float64(fma(a, Float64(c / d), b) / d); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -0.62], N[(a / c), $MachinePrecision], If[LessEqual[c, 2e+41], N[(N[(a * N[(c / d), $MachinePrecision] + b), $MachinePrecision] / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -0.62:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 2 \cdot 10^{+41}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -0.619999999999999996 or 2.00000000000000001e41 < c Initial program 58.3%
Taylor expanded in c around inf
/-lowering-/.f6474.3
Simplified74.3%
if -0.619999999999999996 < c < 2.00000000000000001e41Initial program 76.9%
Taylor expanded in d around inf
/-lowering-/.f64N/A
+-commutativeN/A
associate-/l*N/A
accelerator-lowering-fma.f64N/A
/-lowering-/.f6481.2
Simplified81.2%
(FPCore (a b c d)
:precision binary64
(if (<= c -4.5e+115)
(/ a c)
(if (<= c -3e-14)
(* a (/ c (fma c c (* d d))))
(if (<= c 3.1e+22) (/ b d) (/ a c)))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -4.5e+115) {
tmp = a / c;
} else if (c <= -3e-14) {
tmp = a * (c / fma(c, c, (d * d)));
} else if (c <= 3.1e+22) {
tmp = b / d;
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -4.5e+115) tmp = Float64(a / c); elseif (c <= -3e-14) tmp = Float64(a * Float64(c / fma(c, c, Float64(d * d)))); elseif (c <= 3.1e+22) tmp = Float64(b / d); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -4.5e+115], N[(a / c), $MachinePrecision], If[LessEqual[c, -3e-14], N[(a * N[(c / N[(c * c + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 3.1e+22], N[(b / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -4.5 \cdot 10^{+115}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq -3 \cdot 10^{-14}:\\
\;\;\;\;a \cdot \frac{c}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\
\mathbf{elif}\;c \leq 3.1 \cdot 10^{+22}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -4.49999999999999963e115 or 3.1000000000000002e22 < c Initial program 51.2%
Taylor expanded in c around inf
/-lowering-/.f6477.3
Simplified77.3%
if -4.49999999999999963e115 < c < -2.9999999999999998e-14Initial program 89.8%
Taylor expanded in a around inf
/-lowering-/.f64N/A
*-lowering-*.f64N/A
+-commutativeN/A
unpow2N/A
accelerator-lowering-fma.f64N/A
unpow2N/A
*-lowering-*.f6467.1
Simplified67.1%
associate-/l*N/A
*-commutativeN/A
*-lowering-*.f64N/A
/-lowering-/.f64N/A
+-commutativeN/A
accelerator-lowering-fma.f64N/A
*-lowering-*.f6470.3
Applied egg-rr70.3%
if -2.9999999999999998e-14 < c < 3.1000000000000002e22Initial program 77.0%
Taylor expanded in c around 0
/-lowering-/.f6471.8
Simplified71.8%
Final simplification73.9%
(FPCore (a b c d) :precision binary64 (if (<= c -0.46) (/ a c) (if (<= c 2.65e+22) (/ b d) (/ a c))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -0.46) {
tmp = a / c;
} else if (c <= 2.65e+22) {
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 (c <= (-0.46d0)) then
tmp = a / c
else if (c <= 2.65d+22) 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 (c <= -0.46) {
tmp = a / c;
} else if (c <= 2.65e+22) {
tmp = b / d;
} else {
tmp = a / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if c <= -0.46: tmp = a / c elif c <= 2.65e+22: tmp = b / d else: tmp = a / c return tmp
function code(a, b, c, d) tmp = 0.0 if (c <= -0.46) tmp = Float64(a / c); elseif (c <= 2.65e+22) 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 (c <= -0.46) tmp = a / c; elseif (c <= 2.65e+22) tmp = b / d; else tmp = a / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[LessEqual[c, -0.46], N[(a / c), $MachinePrecision], If[LessEqual[c, 2.65e+22], N[(b / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -0.46:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 2.65 \cdot 10^{+22}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -0.46000000000000002 or 2.6499999999999999e22 < c Initial program 58.9%
Taylor expanded in c around inf
/-lowering-/.f6473.7
Simplified73.7%
if -0.46000000000000002 < c < 2.6499999999999999e22Initial program 76.9%
Taylor expanded in c around 0
/-lowering-/.f6470.4
Simplified70.4%
(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 67.6%
Taylor expanded in c around inf
/-lowering-/.f6447.1
Simplified47.1%
(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 2024199
(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))))