
(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 (* b (/ d c))))
(if (<= c -8000000000000.0)
(/ (+ a t_0) c)
(if (<= c 1.05e-63)
(/ (+ b (/ (* a c) d)) d)
(if (<= c 2.1e+99)
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))
(/ (+ a (- t_0 (* a (pow (/ d c) 2.0)))) c))))))
double code(double a, double b, double c, double d) {
double t_0 = b * (d / c);
double tmp;
if (c <= -8000000000000.0) {
tmp = (a + t_0) / c;
} else if (c <= 1.05e-63) {
tmp = (b + ((a * c) / d)) / d;
} else if (c <= 2.1e+99) {
tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
} else {
tmp = (a + (t_0 - (a * pow((d / c), 2.0)))) / 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) :: t_0
real(8) :: tmp
t_0 = b * (d / c)
if (c <= (-8000000000000.0d0)) then
tmp = (a + t_0) / c
else if (c <= 1.05d-63) then
tmp = (b + ((a * c) / d)) / d
else if (c <= 2.1d+99) then
tmp = ((a * c) + (b * d)) / ((c * c) + (d * d))
else
tmp = (a + (t_0 - (a * ((d / c) ** 2.0d0)))) / c
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double t_0 = b * (d / c);
double tmp;
if (c <= -8000000000000.0) {
tmp = (a + t_0) / c;
} else if (c <= 1.05e-63) {
tmp = (b + ((a * c) / d)) / d;
} else if (c <= 2.1e+99) {
tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
} else {
tmp = (a + (t_0 - (a * Math.pow((d / c), 2.0)))) / c;
}
return tmp;
}
def code(a, b, c, d): t_0 = b * (d / c) tmp = 0 if c <= -8000000000000.0: tmp = (a + t_0) / c elif c <= 1.05e-63: tmp = (b + ((a * c) / d)) / d elif c <= 2.1e+99: tmp = ((a * c) + (b * d)) / ((c * c) + (d * d)) else: tmp = (a + (t_0 - (a * math.pow((d / c), 2.0)))) / c return tmp
function code(a, b, c, d) t_0 = Float64(b * Float64(d / c)) tmp = 0.0 if (c <= -8000000000000.0) tmp = Float64(Float64(a + t_0) / c); elseif (c <= 1.05e-63) tmp = Float64(Float64(b + Float64(Float64(a * c) / d)) / d); elseif (c <= 2.1e+99) tmp = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))); else tmp = Float64(Float64(a + Float64(t_0 - Float64(a * (Float64(d / c) ^ 2.0)))) / c); end return tmp end
function tmp_2 = code(a, b, c, d) t_0 = b * (d / c); tmp = 0.0; if (c <= -8000000000000.0) tmp = (a + t_0) / c; elseif (c <= 1.05e-63) tmp = (b + ((a * c) / d)) / d; elseif (c <= 2.1e+99) tmp = ((a * c) + (b * d)) / ((c * c) + (d * d)); else tmp = (a + (t_0 - (a * ((d / c) ^ 2.0)))) / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[c, -8000000000000.0], N[(N[(a + t$95$0), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[c, 1.05e-63], N[(N[(b + N[(N[(a * c), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, 2.1e+99], N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(a + N[(t$95$0 - N[(a * N[Power[N[(d / c), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := b \cdot \frac{d}{c}\\
\mathbf{if}\;c \leq -8000000000000:\\
\;\;\;\;\frac{a + t\_0}{c}\\
\mathbf{elif}\;c \leq 1.05 \cdot 10^{-63}:\\
\;\;\;\;\frac{b + \frac{a \cdot c}{d}}{d}\\
\mathbf{elif}\;c \leq 2.1 \cdot 10^{+99}:\\
\;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a + \left(t\_0 - a \cdot {\left(\frac{d}{c}\right)}^{2}\right)}{c}\\
\end{array}
\end{array}
if c < -8e12Initial program 44.6%
Taylor expanded in c around inf 72.9%
associate-/l*76.2%
Simplified76.2%
if -8e12 < c < 1.05e-63Initial program 68.6%
Taylor expanded in d around inf 89.8%
if 1.05e-63 < c < 2.1000000000000001e99Initial program 82.7%
if 2.1000000000000001e99 < c Initial program 39.5%
*-un-lft-identity39.5%
associate-*r/39.5%
fma-define39.5%
add-sqr-sqrt39.5%
times-frac39.5%
fma-define39.5%
hypot-define39.5%
fma-define39.5%
fma-define39.5%
hypot-define60.0%
Applied egg-rr60.0%
Taylor expanded in c around inf 71.4%
associate-*r/73.5%
+-commutative73.5%
mul-1-neg73.5%
unsub-neg73.5%
associate-/l*77.5%
unpow277.5%
unpow277.5%
times-frac88.3%
unpow288.3%
Simplified88.3%
(FPCore (a b c d) :precision binary64 (if (<= (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) 1e+307) (* (/ 1.0 (hypot c d)) (/ (fma a c (* b d)) (hypot c d))) (/ (+ b (* c (/ a d))) d)))
double code(double a, double b, double c, double d) {
double tmp;
if ((((a * c) + (b * d)) / ((c * c) + (d * d))) <= 1e+307) {
tmp = (1.0 / hypot(c, d)) * (fma(a, c, (b * d)) / hypot(c, d));
} else {
tmp = (b + (c * (a / 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))) <= 1e+307) tmp = Float64(Float64(1.0 / hypot(c, d)) * Float64(fma(a, c, Float64(b * d)) / hypot(c, d))); else tmp = Float64(Float64(b + Float64(c * Float64(a / 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], 1e+307], N[(N[(1.0 / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(N[(a * c + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(b + N[(c * N[(a / 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 10^{+307}:\\
\;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{\mathsf{fma}\left(a, c, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\
\end{array}
\end{array}
if (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 9.99999999999999986e306Initial program 76.1%
*-un-lft-identity76.1%
associate-*r/76.1%
fma-define76.1%
add-sqr-sqrt76.1%
times-frac76.0%
fma-define76.0%
hypot-define76.0%
fma-define76.0%
fma-define76.0%
hypot-define95.3%
Applied egg-rr95.3%
if 9.99999999999999986e306 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) Initial program 18.8%
Taylor expanded in d around inf 52.8%
*-commutative52.8%
*-un-lft-identity52.8%
times-frac60.9%
Applied egg-rr60.9%
Final simplification85.2%
(FPCore (a b c d)
:precision binary64
(let* ((t_0 (/ (+ a (* b (/ d c))) c)))
(if (<= c -13500000000000.0)
t_0
(if (<= c 2.3e-64)
(/ (+ b (/ (* a c) d)) d)
(if (<= c 3.8e+40) (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) t_0)))))
double code(double a, double b, double c, double d) {
double t_0 = (a + (b * (d / c))) / c;
double tmp;
if (c <= -13500000000000.0) {
tmp = t_0;
} else if (c <= 2.3e-64) {
tmp = (b + ((a * c) / d)) / d;
} else if (c <= 3.8e+40) {
tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
} else {
tmp = t_0;
}
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) :: tmp
t_0 = (a + (b * (d / c))) / c
if (c <= (-13500000000000.0d0)) then
tmp = t_0
else if (c <= 2.3d-64) then
tmp = (b + ((a * c) / d)) / d
else if (c <= 3.8d+40) then
tmp = ((a * c) + (b * d)) / ((c * c) + (d * d))
else
tmp = t_0
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double t_0 = (a + (b * (d / c))) / c;
double tmp;
if (c <= -13500000000000.0) {
tmp = t_0;
} else if (c <= 2.3e-64) {
tmp = (b + ((a * c) / d)) / d;
} else if (c <= 3.8e+40) {
tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
} else {
tmp = t_0;
}
return tmp;
}
def code(a, b, c, d): t_0 = (a + (b * (d / c))) / c tmp = 0 if c <= -13500000000000.0: tmp = t_0 elif c <= 2.3e-64: tmp = (b + ((a * c) / d)) / d elif c <= 3.8e+40: tmp = ((a * c) + (b * d)) / ((c * c) + (d * d)) else: tmp = t_0 return tmp
function code(a, b, c, d) t_0 = Float64(Float64(a + Float64(b * Float64(d / c))) / c) tmp = 0.0 if (c <= -13500000000000.0) tmp = t_0; elseif (c <= 2.3e-64) tmp = Float64(Float64(b + Float64(Float64(a * c) / d)) / d); elseif (c <= 3.8e+40) tmp = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))); else tmp = t_0; end return tmp end
function tmp_2 = code(a, b, c, d) t_0 = (a + (b * (d / c))) / c; tmp = 0.0; if (c <= -13500000000000.0) tmp = t_0; elseif (c <= 2.3e-64) tmp = (b + ((a * c) / d)) / d; elseif (c <= 3.8e+40) tmp = ((a * c) + (b * d)) / ((c * c) + (d * d)); else tmp = t_0; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := Block[{t$95$0 = N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]}, If[LessEqual[c, -13500000000000.0], t$95$0, If[LessEqual[c, 2.3e-64], N[(N[(b + N[(N[(a * c), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[c, 3.8e+40], N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], t$95$0]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{a + b \cdot \frac{d}{c}}{c}\\
\mathbf{if}\;c \leq -13500000000000:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;c \leq 2.3 \cdot 10^{-64}:\\
\;\;\;\;\frac{b + \frac{a \cdot c}{d}}{d}\\
\mathbf{elif}\;c \leq 3.8 \cdot 10^{+40}:\\
\;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if c < -1.35e13 or 3.80000000000000004e40 < c Initial program 44.6%
Taylor expanded in c around inf 76.0%
associate-/l*81.1%
Simplified81.1%
if -1.35e13 < c < 2.3000000000000001e-64Initial program 68.6%
Taylor expanded in d around inf 89.8%
if 2.3000000000000001e-64 < c < 3.80000000000000004e40Initial program 84.8%
(FPCore (a b c d) :precision binary64 (if (or (<= c -9500000000000.0) (not (<= c 6e+27))) (/ (+ a (* b (/ d c))) c) (/ (+ b (* a (/ c d))) d)))
double code(double a, double b, double c, double d) {
double tmp;
if ((c <= -9500000000000.0) || !(c <= 6e+27)) {
tmp = (a + (b * (d / c))) / c;
} else {
tmp = (b + (a * (c / d))) / 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 ((c <= (-9500000000000.0d0)) .or. (.not. (c <= 6d+27))) then
tmp = (a + (b * (d / c))) / c
else
tmp = (b + (a * (c / d))) / d
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if ((c <= -9500000000000.0) || !(c <= 6e+27)) {
tmp = (a + (b * (d / c))) / c;
} else {
tmp = (b + (a * (c / d))) / d;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (c <= -9500000000000.0) or not (c <= 6e+27): tmp = (a + (b * (d / c))) / c else: tmp = (b + (a * (c / d))) / d return tmp
function code(a, b, c, d) tmp = 0.0 if ((c <= -9500000000000.0) || !(c <= 6e+27)) tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / c); else tmp = Float64(Float64(b + Float64(a * Float64(c / d))) / d); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if ((c <= -9500000000000.0) || ~((c <= 6e+27))) tmp = (a + (b * (d / c))) / c; else tmp = (b + (a * (c / d))) / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[c, -9500000000000.0], N[Not[LessEqual[c, 6e+27]], $MachinePrecision]], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -9500000000000 \lor \neg \left(c \leq 6 \cdot 10^{+27}\right):\\
\;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\
\end{array}
\end{array}
if c < -9.5e12 or 5.99999999999999953e27 < c Initial program 46.9%
Taylor expanded in c around inf 75.8%
associate-/l*80.7%
Simplified80.7%
if -9.5e12 < c < 5.99999999999999953e27Initial program 70.9%
Taylor expanded in d around inf 85.6%
associate-/l*85.6%
Simplified85.6%
Final simplification83.2%
(FPCore (a b c d) :precision binary64 (if (or (<= c -10000000000000.0) (not (<= c 5.4e-124))) (/ (+ a (* b (/ d c))) c) (/ b d)))
double code(double a, double b, double c, double d) {
double tmp;
if ((c <= -10000000000000.0) || !(c <= 5.4e-124)) {
tmp = (a + (b * (d / c))) / 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 ((c <= (-10000000000000.0d0)) .or. (.not. (c <= 5.4d-124))) then
tmp = (a + (b * (d / c))) / 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 ((c <= -10000000000000.0) || !(c <= 5.4e-124)) {
tmp = (a + (b * (d / c))) / c;
} else {
tmp = b / d;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (c <= -10000000000000.0) or not (c <= 5.4e-124): tmp = (a + (b * (d / c))) / c else: tmp = b / d return tmp
function code(a, b, c, d) tmp = 0.0 if ((c <= -10000000000000.0) || !(c <= 5.4e-124)) tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / c); else tmp = Float64(b / d); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if ((c <= -10000000000000.0) || ~((c <= 5.4e-124))) tmp = (a + (b * (d / c))) / c; else tmp = b / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[c, -10000000000000.0], N[Not[LessEqual[c, 5.4e-124]], $MachinePrecision]], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], N[(b / d), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -10000000000000 \lor \neg \left(c \leq 5.4 \cdot 10^{-124}\right):\\
\;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if c < -1e13 or 5.40000000000000035e-124 < c Initial program 53.4%
Taylor expanded in c around inf 70.9%
associate-/l*74.8%
Simplified74.8%
if -1e13 < c < 5.40000000000000035e-124Initial program 68.4%
Taylor expanded in c around 0 71.8%
Final simplification73.6%
(FPCore (a b c d) :precision binary64 (if (or (<= c -14500000000000.0) (not (<= c 4.1e-56))) (/ a c) (/ b d)))
double code(double a, double b, double c, double d) {
double tmp;
if ((c <= -14500000000000.0) || !(c <= 4.1e-56)) {
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 ((c <= (-14500000000000.0d0)) .or. (.not. (c <= 4.1d-56))) 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 ((c <= -14500000000000.0) || !(c <= 4.1e-56)) {
tmp = a / c;
} else {
tmp = b / d;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (c <= -14500000000000.0) or not (c <= 4.1e-56): tmp = a / c else: tmp = b / d return tmp
function code(a, b, c, d) tmp = 0.0 if ((c <= -14500000000000.0) || !(c <= 4.1e-56)) 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 ((c <= -14500000000000.0) || ~((c <= 4.1e-56))) tmp = a / c; else tmp = b / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[c, -14500000000000.0], N[Not[LessEqual[c, 4.1e-56]], $MachinePrecision]], N[(a / c), $MachinePrecision], N[(b / d), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -14500000000000 \lor \neg \left(c \leq 4.1 \cdot 10^{-56}\right):\\
\;\;\;\;\frac{a}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{d}\\
\end{array}
\end{array}
if c < -1.45e13 or 4.1000000000000001e-56 < c Initial program 51.2%
Taylor expanded in c around inf 66.4%
if -1.45e13 < c < 4.1000000000000001e-56Initial program 69.1%
Taylor expanded in c around 0 67.9%
Final simplification67.1%
(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 59.3%
Taylor expanded in c around inf 46.0%
(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 2024116
(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))))