
(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 (/ c d) a b)) (t_1 (/ (fma (/ b c) d a) c)))
(if (<= c -3.1e+125)
t_1
(if (<= c -4.8e+113)
(/ 1.0 (/ d t_0))
(if (<= c -9.6e-127)
(/ (fma d b (* a c)) (fma d d (* c c)))
(if (<= c 7.8e-53) (/ t_0 d) t_1))))))
double code(double a, double b, double c, double d) {
double t_0 = fma((c / d), a, b);
double t_1 = fma((b / c), d, a) / c;
double tmp;
if (c <= -3.1e+125) {
tmp = t_1;
} else if (c <= -4.8e+113) {
tmp = 1.0 / (d / t_0);
} else if (c <= -9.6e-127) {
tmp = fma(d, b, (a * c)) / fma(d, d, (c * c));
} else if (c <= 7.8e-53) {
tmp = t_0 / d;
} else {
tmp = t_1;
}
return tmp;
}
function code(a, b, c, d) t_0 = fma(Float64(c / d), a, b) t_1 = Float64(fma(Float64(b / c), d, a) / c) tmp = 0.0 if (c <= -3.1e+125) tmp = t_1; elseif (c <= -4.8e+113) tmp = Float64(1.0 / Float64(d / t_0)); elseif (c <= -9.6e-127) tmp = Float64(fma(d, b, Float64(a * c)) / fma(d, d, Float64(c * c))); elseif (c <= 7.8e-53) tmp = Float64(t_0 / d); else tmp = t_1; end return tmp end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(c / d), $MachinePrecision] * a + b), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(b / c), $MachinePrecision] * d + a), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -3.1e+125], t$95$1, If[LessEqual[c, -4.8e+113], N[(1.0 / N[(d / t$95$0), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, -9.6e-127], N[(N[(d * b + N[(a * c), $MachinePrecision]), $MachinePrecision] / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 7.8e-53], N[(t$95$0 / d), $MachinePrecision], t$95$1]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\frac{c}{d}, a, b\right)\\
t_1 := \frac{\mathsf{fma}\left(\frac{b}{c}, d, a\right)}{c}\\
\mathbf{if}\;c \leq -3.1 \cdot 10^{+125}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;c \leq -4.8 \cdot 10^{+113}:\\
\;\;\;\;\frac{1}{\frac{d}{t\_0}}\\
\mathbf{elif}\;c \leq -9.6 \cdot 10^{-127}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, a \cdot c\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{elif}\;c \leq 7.8 \cdot 10^{-53}:\\
\;\;\;\;\frac{t\_0}{d}\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if c < -3.1e125 or 7.8000000000000004e-53 < c Initial program 57.0%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6486.4
Applied rewrites86.4%
if -3.1e125 < c < -4.79999999999999966e113Initial program 14.8%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6414.8
lift-*.f64N/A
*-commutativeN/A
lower-*.f6414.8
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6414.8
Applied rewrites14.8%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6485.7
Applied rewrites85.7%
Applied rewrites85.9%
if -4.79999999999999966e113 < c < -9.59999999999999929e-127Initial program 81.3%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6481.3
lift-*.f64N/A
*-commutativeN/A
lower-*.f6481.3
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6481.3
Applied rewrites81.3%
if -9.59999999999999929e-127 < c < 7.8000000000000004e-53Initial program 68.6%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6468.6
lift-*.f64N/A
*-commutativeN/A
lower-*.f6468.6
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6468.6
Applied rewrites68.6%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6492.0
Applied rewrites92.0%
Final simplification86.9%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (fma (/ b c) d a) c)))
(if (<= c -3.1e+125)
t_0
(if (<= c -4.8e+113)
(/ (fma (/ a d) c b) d)
(if (<= c -9.6e-127)
(/ (fma d b (* a c)) (fma d d (* c c)))
(if (<= c 7.8e-53) (/ (fma (/ c d) a 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 <= -3.1e+125) {
tmp = t_0;
} else if (c <= -4.8e+113) {
tmp = fma((a / d), c, b) / d;
} else if (c <= -9.6e-127) {
tmp = fma(d, b, (a * c)) / fma(d, d, (c * c));
} else if (c <= 7.8e-53) {
tmp = fma((c / d), a, 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 <= -3.1e+125) tmp = t_0; elseif (c <= -4.8e+113) tmp = Float64(fma(Float64(a / d), c, b) / d); elseif (c <= -9.6e-127) tmp = Float64(fma(d, b, Float64(a * c)) / fma(d, d, Float64(c * c))); elseif (c <= 7.8e-53) tmp = Float64(fma(Float64(c / d), a, 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, -3.1e+125], t$95$0, If[LessEqual[c, -4.8e+113], N[(N[(N[(a / d), $MachinePrecision] * c + b), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, -9.6e-127], N[(N[(d * b + N[(a * c), $MachinePrecision]), $MachinePrecision] / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 7.8e-53], N[(N[(N[(c / d), $MachinePrecision] * a + 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 -3.1 \cdot 10^{+125}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq -4.8 \cdot 10^{+113}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{elif}\;c \leq -9.6 \cdot 10^{-127}:\\
\;\;\;\;\frac{\mathsf{fma}\left(d, b, a \cdot c\right)}{\mathsf{fma}\left(d, d, c \cdot c\right)}\\
\mathbf{elif}\;c \leq 7.8 \cdot 10^{-53}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if c < -3.1e125 or 7.8000000000000004e-53 < c Initial program 57.0%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6486.4
Applied rewrites86.4%
if -3.1e125 < c < -4.79999999999999966e113Initial program 14.8%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6485.7
Applied rewrites85.7%
if -4.79999999999999966e113 < c < -9.59999999999999929e-127Initial program 81.3%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6481.3
lift-*.f64N/A
*-commutativeN/A
lower-*.f6481.3
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6481.3
Applied rewrites81.3%
if -9.59999999999999929e-127 < c < 7.8000000000000004e-53Initial program 68.6%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6468.6
lift-*.f64N/A
*-commutativeN/A
lower-*.f6468.6
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6468.6
Applied rewrites68.6%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6492.0
Applied rewrites92.0%
Final simplification86.9%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (/ (fma (/ b c) d a) c))) (if (<= c -2.8e+47) t_0 (if (<= c 7.8e-53) (/ (fma (/ c d) a 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 <= -2.8e+47) {
tmp = t_0;
} else if (c <= 7.8e-53) {
tmp = fma((c / d), a, 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 <= -2.8e+47) tmp = t_0; elseif (c <= 7.8e-53) tmp = Float64(fma(Float64(c / d), a, 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, -2.8e+47], t$95$0, If[LessEqual[c, 7.8e-53], N[(N[(N[(c / d), $MachinePrecision] * a + 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 -2.8 \cdot 10^{+47}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq 7.8 \cdot 10^{-53}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{c}{d}, a, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if c < -2.79999999999999988e47 or 7.8000000000000004e-53 < c Initial program 57.3%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6481.0
Applied rewrites81.0%
if -2.79999999999999988e47 < c < 7.8000000000000004e-53Initial program 73.2%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6473.2
lift-*.f64N/A
*-commutativeN/A
lower-*.f6473.2
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6473.2
Applied rewrites73.2%
Taylor expanded in d around inf
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6483.3
Applied rewrites83.3%
(FPCore (a b c d) :precision binary64 (let* ((t_0 (/ (fma (/ b c) d a) c))) (if (<= c -2.8e+47) t_0 (if (<= c 7.8e-53) (/ (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 <= -2.8e+47) {
tmp = t_0;
} else if (c <= 7.8e-53) {
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 <= -2.8e+47) tmp = t_0; elseif (c <= 7.8e-53) 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, -2.8e+47], t$95$0, If[LessEqual[c, 7.8e-53], 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 -2.8 \cdot 10^{+47}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq 7.8 \cdot 10^{-53}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if c < -2.79999999999999988e47 or 7.8000000000000004e-53 < c Initial program 57.3%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f6481.0
Applied rewrites81.0%
if -2.79999999999999988e47 < c < 7.8000000000000004e-53Initial program 73.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-/.f6482.8
Applied rewrites82.8%
(FPCore (a b c d) :precision binary64 (if (<= c -4.7e+131) (/ a c) (if (<= c 7.8e-53) (/ (fma (/ a d) c b) d) (/ a c))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -4.7e+131) {
tmp = a / c;
} else if (c <= 7.8e-53) {
tmp = fma((a / d), c, b) / d;
} else {
tmp = a / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -4.7e+131) tmp = Float64(a / c); elseif (c <= 7.8e-53) 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, -4.7e+131], N[(a / c), $MachinePrecision], If[LessEqual[c, 7.8e-53], 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 -4.7 \cdot 10^{+131}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 7.8 \cdot 10^{-53}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{a}{d}, c, b\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -4.7e131 or 7.8000000000000004e-53 < c Initial program 57.0%
Taylor expanded in c around inf
lower-/.f6475.7
Applied rewrites75.7%
if -4.7e131 < c < 7.8000000000000004e-53Initial program 70.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.7
Applied rewrites77.7%
(FPCore (a b c d) :precision binary64 (if (<= c -3e+131) (/ a c) (if (<= c 5.5e-69) (/ b d) (/ a c))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -3e+131) {
tmp = a / c;
} else if (c <= 5.5e-69) {
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 <= (-3d+131)) then
tmp = a / c
else if (c <= 5.5d-69) 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 <= -3e+131) {
tmp = a / c;
} else if (c <= 5.5e-69) {
tmp = b / d;
} else {
tmp = a / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if c <= -3e+131: tmp = a / c elif c <= 5.5e-69: tmp = b / d else: tmp = a / c return tmp
function code(a, b, c, d) tmp = 0.0 if (c <= -3e+131) tmp = Float64(a / c); elseif (c <= 5.5e-69) 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 <= -3e+131) tmp = a / c; elseif (c <= 5.5e-69) tmp = b / d; else tmp = a / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[LessEqual[c, -3e+131], N[(a / c), $MachinePrecision], If[LessEqual[c, 5.5e-69], N[(b / d), $MachinePrecision], N[(a / c), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -3 \cdot 10^{+131}:\\
\;\;\;\;\frac{a}{c}\\
\mathbf{elif}\;c \leq 5.5 \cdot 10^{-69}:\\
\;\;\;\;\frac{b}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if c < -3.0000000000000001e131 or 5.50000000000000006e-69 < c Initial program 58.4%
Taylor expanded in c around inf
lower-/.f6474.9
Applied rewrites74.9%
if -3.0000000000000001e131 < c < 5.50000000000000006e-69Initial program 70.0%
Taylor expanded in c around 0
lower-/.f6465.7
Applied rewrites65.7%
(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.5%
Taylor expanded in c around inf
lower-/.f6444.8
Applied rewrites44.8%
(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 2024304
(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))))