
(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)))
(if (<= c -3.2e+92)
t_0
(if (<= c -3.3e-14)
(/ (fma d b (* c a)) (+ (* c c) (* d d)))
(if (<= c 6e+17) (/ (fma a (/ c d) b) d) t_0)))))
double code(double a, double b, double c, double d) {
double t_0 = fma(d, (b / c), a) / c;
double tmp;
if (c <= -3.2e+92) {
tmp = t_0;
} else if (c <= -3.3e-14) {
tmp = fma(d, b, (c * a)) / ((c * c) + (d * d));
} else if (c <= 6e+17) {
tmp = fma(a, (c / d), b) / d;
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(d, Float64(b / c), a) / c) tmp = 0.0 if (c <= -3.2e+92) tmp = t_0; elseif (c <= -3.3e-14) tmp = Float64(fma(d, b, Float64(c * a)) / Float64(Float64(c * c) + Float64(d * d))); elseif (c <= 6e+17) 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[(d * N[(b / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -3.2e+92], t$95$0, If[LessEqual[c, -3.3e-14], N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 6e+17], 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(d, \frac{b}{c}, a\right)}{c}\\
\mathbf{if}\;c \leq -3.2 \cdot 10^{+92}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq -3.3 \cdot 10^{-14}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{c \cdot c + d \cdot d}\\
\mathbf{elif}\;c \leq 6 \cdot 10^{+17}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if c < -3.20000000000000025e92 or 6e17 < c Initial program 41.5%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6441.6
Applied rewrites41.6%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6486.4
Applied rewrites86.4%
if -3.20000000000000025e92 < c < -3.2999999999999998e-14Initial program 79.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6479.1
Applied rewrites79.1%
if -3.2999999999999998e-14 < c < 6e17Initial program 61.0%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6483.6
Applied rewrites83.6%
Final simplification84.0%
(FPCore (a b c d)
:precision binary64
(if (<= c -3.2e+81)
(/ a c)
(if (<= c -1.09e-13)
(/ (fma d b (* c a)) (* c c))
(if (<= c 6.5e-25)
(/ b d)
(if (<= c 4.5e+79) (/ (* c a) (fma d d (* c c))) (/ a c))))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -3.2e+81) {
tmp = a / c;
} else if (c <= -1.09e-13) {
tmp = fma(d, b, (c * a)) / (c * c);
} else if (c <= 6.5e-25) {
tmp = b / d;
} else if (c <= 4.5e+79) {
tmp = (c * a) / fma(d, d, (c * c));
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -3.2e+81) tmp = Float64(a / c); elseif (c <= -1.09e-13) tmp = Float64(fma(d, b, Float64(c * a)) / Float64(c * c)); elseif (c <= 6.5e-25) tmp = Float64(b / d); elseif (c <= 4.5e+79) tmp = Float64(Float64(c * a) / fma(d, d, Float64(c * c))); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -3.2e+81], N[(a / c), $MachinePrecision], If[LessEqual[c, -1.09e-13], N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 6.5e-25], N[(b / d), $MachinePrecision], If[LessEqual[c, 4.5e+79], N[(N[(c * a), $MachinePrecision] / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a / c), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -3.2 \cdot 10^{+81}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq -1.09 \cdot 10^{-13}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{c \cdot c}\\
\mathbf{elif}\;c \leq 6.5 \cdot 10^{-25}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;c \leq 4.5 \cdot 10^{+79}:\\
\;\;\;\;\frac{c \cdot a}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -3.2e81 or 4.49999999999999994e79 < c Initial program 36.0%
Taylor expanded in c around inf
lower-/.f6475.7
Applied rewrites75.7%
if -3.2e81 < c < -1.09000000000000003e-13Initial program 83.1%
Taylor expanded in c around inf
unpow2N/A
lower-*.f6472.0
Applied rewrites72.0%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
+-commutativeN/A
lift-fma.f6472.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f6472.0
Applied rewrites72.0%
if -1.09000000000000003e-13 < c < 6.5e-25Initial program 60.1%
Taylor expanded in c around 0
lower-/.f6465.8
Applied rewrites65.8%
if 6.5e-25 < c < 4.49999999999999994e79Initial program 80.7%
Taylor expanded in a around inf
lower-/.f64N/A
lower-*.f64N/A
+-commutativeN/A
unpow2N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6475.9
Applied rewrites75.9%
Final simplification70.5%
(FPCore (a b c d)
:precision binary64
(if (<= c -3.2e+81)
(/ a c)
(if (<= c -7.5e-10)
(/ (fma d b (* c a)) (* c c))
(if (<= c 13500000.0) (/ (fma a (/ c d) b) d) (/ a c)))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -3.2e+81) {
tmp = a / c;
} else if (c <= -7.5e-10) {
tmp = fma(d, b, (c * a)) / (c * c);
} else if (c <= 13500000.0) {
tmp = fma(a, (c / d), b) / d;
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -3.2e+81) tmp = Float64(a / c); elseif (c <= -7.5e-10) tmp = Float64(fma(d, b, Float64(c * a)) / Float64(c * c)); elseif (c <= 13500000.0) 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, -3.2e+81], N[(a / c), $MachinePrecision], If[LessEqual[c, -7.5e-10], N[(N[(d * b + N[(c * a), $MachinePrecision]), $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 13500000.0], 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 -3.2 \cdot 10^{+81}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq -7.5 \cdot 10^{-10}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, c \cdot a\right)}{c \cdot c}\\
\mathbf{elif}\;c \leq 13500000:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -3.2e81 or 1.35e7 < c Initial program 43.1%
Taylor expanded in c around inf
lower-/.f6473.4
Applied rewrites73.4%
if -3.2e81 < c < -7.49999999999999995e-10Initial program 83.1%
Taylor expanded in c around inf
unpow2N/A
lower-*.f6472.0
Applied rewrites72.0%
lift-+.f64N/A
lift-*.f64N/A
*-commutativeN/A
+-commutativeN/A
lift-fma.f6472.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f6472.0
Applied rewrites72.0%
if -7.49999999999999995e-10 < c < 1.35e7Initial program 60.4%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6484.1
Applied rewrites84.1%
(FPCore (a b c d)
:precision binary64
(if (<= c -1.12e-13)
(/ a c)
(if (<= c 6.5e-25)
(/ b d)
(if (<= c 4.5e+79) (/ (* c a) (fma d d (* c c))) (/ a c)))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -1.12e-13) {
tmp = a / c;
} else if (c <= 6.5e-25) {
tmp = b / d;
} else if (c <= 4.5e+79) {
tmp = (c * a) / fma(d, d, (c * c));
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -1.12e-13) tmp = Float64(a / c); elseif (c <= 6.5e-25) tmp = Float64(b / d); elseif (c <= 4.5e+79) tmp = Float64(Float64(c * a) / fma(d, d, Float64(c * c))); else tmp = Float64(a / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -1.12e-13], N[(a / c), $MachinePrecision], If[LessEqual[c, 6.5e-25], N[(b / d), $MachinePrecision], If[LessEqual[c, 4.5e+79], N[(N[(c * a), $MachinePrecision] / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(a / c), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -1.12 \cdot 10^{-13}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 6.5 \cdot 10^{-25}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{elif}\;c \leq 4.5 \cdot 10^{+79}:\\
\;\;\;\;\frac{c \cdot a}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -1.12e-13 or 4.49999999999999994e79 < c Initial program 47.6%
Taylor expanded in c around inf
lower-/.f6469.2
Applied rewrites69.2%
if -1.12e-13 < c < 6.5e-25Initial program 60.1%
Taylor expanded in c around 0
lower-/.f6465.8
Applied rewrites65.8%
if 6.5e-25 < c < 4.49999999999999994e79Initial program 80.7%
Taylor expanded in a around inf
lower-/.f64N/A
lower-*.f64N/A
+-commutativeN/A
unpow2N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6475.9
Applied rewrites75.9%
Final simplification68.1%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (/ (fma d (/ b c) a) c))) (if (<= c -7.5e-10) t_0 (if (<= c 6e+17) (/ (fma a (/ c d) b) d) t_0))))
double code(double a, double b, double c, double d) {
double t_0 = fma(d, (b / c), a) / c;
double tmp;
if (c <= -7.5e-10) {
tmp = t_0;
} else if (c <= 6e+17) {
tmp = fma(a, (c / d), b) / d;
} else {
tmp = t_0;
}
return tmp;
}
function code(a, b, c, d) t_0 = Float64(fma(d, Float64(b / c), a) / c) tmp = 0.0 if (c <= -7.5e-10) tmp = t_0; elseif (c <= 6e+17) 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[(d * N[(b / c), $MachinePrecision] + a), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -7.5e-10], t$95$0, If[LessEqual[c, 6e+17], 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(d, \frac{b}{c}, a\right)}{c}\\
\mathbf{if}\;c \leq -7.5 \cdot 10^{-10}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq 6 \cdot 10^{+17}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if c < -7.49999999999999995e-10 or 6e17 < c Initial program 51.1%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6451.2
Applied rewrites51.2%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6481.3
Applied rewrites81.3%
if -7.49999999999999995e-10 < c < 6e17Initial program 61.0%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6483.6
Applied rewrites83.6%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (/ (fma b (/ d c) a) c))) (if (<= c -7.5e-10) t_0 (if (<= c 6e+17) (/ (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 <= -7.5e-10) {
tmp = t_0;
} else if (c <= 6e+17) {
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 <= -7.5e-10) tmp = t_0; elseif (c <= 6e+17) 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, -7.5e-10], t$95$0, If[LessEqual[c, 6e+17], 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 -7.5 \cdot 10^{-10}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq 6 \cdot 10^{+17}:\\
\;\;\;\;\frac{\mathsf{fma}\left(a, \frac{c}{d}, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if c < -7.49999999999999995e-10 or 6e17 < c Initial program 51.1%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6480.5
Applied rewrites80.5%
if -7.49999999999999995e-10 < c < 6e17Initial program 61.0%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f6483.6
Applied rewrites83.6%
(FPCore (a b c d) :precision binary64 (if (<= c -1.12e-13) (/ a c) (if (<= c 3600000.0) (/ b d) (/ a c))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -1.12e-13) {
tmp = a / c;
} else if (c <= 3600000.0) {
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 <= (-1.12d-13)) then
tmp = a / c
else if (c <= 3600000.0d0) 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 <= -1.12e-13) {
tmp = a / c;
} else if (c <= 3600000.0) {
tmp = b / d;
} else {
tmp = a / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if c <= -1.12e-13: tmp = a / c elif c <= 3600000.0: tmp = b / d else: tmp = a / c return tmp
function code(a, b, c, d) tmp = 0.0 if (c <= -1.12e-13) tmp = Float64(a / c); elseif (c <= 3600000.0) 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 <= -1.12e-13) tmp = a / c; elseif (c <= 3600000.0) tmp = b / d; else tmp = a / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[LessEqual[c, -1.12e-13], N[(a / c), $MachinePrecision], If[LessEqual[c, 3600000.0], N[(b / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -1.12 \cdot 10^{-13}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 3600000:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -1.12e-13 or 3.6e6 < c Initial program 52.3%
Taylor expanded in c around inf
lower-/.f6467.6
Applied rewrites67.6%
if -1.12e-13 < c < 3.6e6Initial program 60.1%
Taylor expanded in c around 0
lower-/.f6464.9
Applied rewrites64.9%
(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 56.2%
Taylor expanded in c around inf
lower-/.f6442.5
Applied rewrites42.5%
(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 2024234
(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))))