
(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 (if (<= (/ (+ (* a c) (* b d)) (+ (* c c) (* d d))) 2e+295) (/ (/ (fma c a (* b d)) (hypot c d)) (hypot c d)) (/ (+ b (* a (/ c d))) d)))
double code(double a, double b, double c, double d) {
double tmp;
if ((((a * c) + (b * d)) / ((c * c) + (d * d))) <= 2e+295) {
tmp = (fma(c, a, (b * d)) / hypot(c, d)) / hypot(c, d);
} else {
tmp = (b + (a * (c / 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))) <= 2e+295) tmp = Float64(Float64(fma(c, a, Float64(b * d)) / hypot(c, d)) / hypot(c, d)); else tmp = Float64(Float64(b + Float64(a * Float64(c / 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], 2e+295], N[(N[(N[(c * a + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision], N[(N[(b + N[(a * N[(c / 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 2 \cdot 10^{+295}:\\
\;\;\;\;\frac{\frac{\mathsf{fma}\left(c, a, b \cdot d\right)}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\
\end{array}
\end{array}
if (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) < 2e295Initial program 78.6%
fma-define78.6%
fma-define78.7%
Simplified78.7%
*-un-lft-identity78.7%
fma-define78.6%
add-sqr-sqrt78.6%
times-frac78.6%
fma-define78.6%
hypot-define78.6%
fma-define78.6%
fma-define78.6%
hypot-define97.6%
Applied egg-rr97.6%
*-commutative97.6%
associate-*l/97.7%
div-inv97.8%
fma-undefine97.8%
*-commutative97.8%
*-commutative97.8%
fma-define97.8%
Applied egg-rr97.8%
if 2e295 < (/.f64 (+.f64 (*.f64 a c) (*.f64 b d)) (+.f64 (*.f64 c c) (*.f64 d d))) Initial program 12.0%
fma-define12.0%
fma-define12.0%
Simplified12.0%
Taylor expanded in d around inf 49.0%
associate-/l*58.6%
Simplified58.6%
Final simplification87.1%
(FPCore (a b c d)
:precision binary64
(if (<= c -1e+65)
(/ (+ a (* b (/ d c))) c)
(if (<= c -1.92e-149)
(/ (fma c a (* b d)) (+ (* c c) (* d d)))
(if (<= c 105.0)
(/ (+ b (/ (* a c) d)) d)
(/ (* a (+ 1.0 (* b (/ d (* a c))))) c)))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -1e+65) {
tmp = (a + (b * (d / c))) / c;
} else if (c <= -1.92e-149) {
tmp = fma(c, a, (b * d)) / ((c * c) + (d * d));
} else if (c <= 105.0) {
tmp = (b + ((a * c) / d)) / d;
} else {
tmp = (a * (1.0 + (b * (d / (a * c))))) / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -1e+65) tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / c); elseif (c <= -1.92e-149) tmp = Float64(fma(c, a, Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))); elseif (c <= 105.0) tmp = Float64(Float64(b + Float64(Float64(a * c) / d)) / d); else tmp = Float64(Float64(a * Float64(1.0 + Float64(b * Float64(d / Float64(a * c))))) / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -1e+65], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[c, -1.92e-149], N[(N[(c * a + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 105.0], N[(N[(b + N[(N[(a * c), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], N[(N[(a * N[(1.0 + N[(b * N[(d / N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -1 \cdot 10^{+65}:\\
\;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\
\mathbf{elif}\;c \leq -1.92 \cdot 10^{-149}:\\
\;\;\;\;\frac{\mathsf{fma}\left(c, a, b \cdot d\right)}{c \cdot c + d \cdot d}\\
\mathbf{elif}\;c \leq 105:\\
\;\;\;\;\frac{b + \frac{a \cdot c}{d}}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a \cdot \left(1 + b \cdot \frac{d}{a \cdot c}\right)}{c}\\
\end{array}
\end{array}
if c < -9.9999999999999999e64Initial program 43.3%
fma-define43.3%
fma-define43.3%
Simplified43.3%
Taylor expanded in c around inf 75.7%
*-commutative75.7%
Simplified75.7%
Taylor expanded in d around 0 75.7%
associate-*r/79.9%
Simplified79.9%
if -9.9999999999999999e64 < c < -1.92e-149Initial program 87.5%
*-commutative87.5%
fma-define87.5%
Applied egg-rr87.5%
if -1.92e-149 < c < 105Initial program 68.6%
fma-define68.6%
fma-define68.6%
Simplified68.6%
Taylor expanded in d around inf 91.6%
if 105 < c Initial program 43.4%
fma-define43.4%
fma-define43.4%
Simplified43.4%
Taylor expanded in c around inf 77.2%
*-commutative77.2%
Simplified77.2%
Taylor expanded in a around inf 77.1%
associate-/l*83.9%
*-commutative83.9%
associate-/r*82.6%
Simplified82.6%
Taylor expanded in b around 0 77.1%
associate-*r/83.9%
Simplified83.9%
(FPCore (a b c d)
:precision binary64
(if (<= c -9.2e+64)
(/ (+ a (* b (/ d c))) c)
(if (<= c -1.8e-147)
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))
(if (<= c 9.5)
(/ (+ b (/ (* a c) d)) d)
(/ (* a (+ 1.0 (* b (/ d (* a c))))) c)))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -9.2e+64) {
tmp = (a + (b * (d / c))) / c;
} else if (c <= -1.8e-147) {
tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
} else if (c <= 9.5) {
tmp = (b + ((a * c) / d)) / d;
} else {
tmp = (a * (1.0 + (b * (d / (a * c))))) / 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 <= (-9.2d+64)) then
tmp = (a + (b * (d / c))) / c
else if (c <= (-1.8d-147)) then
tmp = ((a * c) + (b * d)) / ((c * c) + (d * d))
else if (c <= 9.5d0) then
tmp = (b + ((a * c) / d)) / d
else
tmp = (a * (1.0d0 + (b * (d / (a * c))))) / c
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if (c <= -9.2e+64) {
tmp = (a + (b * (d / c))) / c;
} else if (c <= -1.8e-147) {
tmp = ((a * c) + (b * d)) / ((c * c) + (d * d));
} else if (c <= 9.5) {
tmp = (b + ((a * c) / d)) / d;
} else {
tmp = (a * (1.0 + (b * (d / (a * c))))) / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if c <= -9.2e+64: tmp = (a + (b * (d / c))) / c elif c <= -1.8e-147: tmp = ((a * c) + (b * d)) / ((c * c) + (d * d)) elif c <= 9.5: tmp = (b + ((a * c) / d)) / d else: tmp = (a * (1.0 + (b * (d / (a * c))))) / c return tmp
function code(a, b, c, d) tmp = 0.0 if (c <= -9.2e+64) tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / c); elseif (c <= -1.8e-147) tmp = Float64(Float64(Float64(a * c) + Float64(b * d)) / Float64(Float64(c * c) + Float64(d * d))); elseif (c <= 9.5) tmp = Float64(Float64(b + Float64(Float64(a * c) / d)) / d); else tmp = Float64(Float64(a * Float64(1.0 + Float64(b * Float64(d / Float64(a * c))))) / c); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if (c <= -9.2e+64) tmp = (a + (b * (d / c))) / c; elseif (c <= -1.8e-147) tmp = ((a * c) + (b * d)) / ((c * c) + (d * d)); elseif (c <= 9.5) tmp = (b + ((a * c) / d)) / d; else tmp = (a * (1.0 + (b * (d / (a * c))))) / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[LessEqual[c, -9.2e+64], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[c, -1.8e-147], N[(N[(N[(a * c), $MachinePrecision] + N[(b * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 9.5], N[(N[(b + N[(N[(a * c), $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], N[(N[(a * N[(1.0 + N[(b * N[(d / N[(a * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -9.2 \cdot 10^{+64}:\\
\;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\
\mathbf{elif}\;c \leq -1.8 \cdot 10^{-147}:\\
\;\;\;\;\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\\
\mathbf{elif}\;c \leq 9.5:\\
\;\;\;\;\frac{b + \frac{a \cdot c}{d}}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a \cdot \left(1 + b \cdot \frac{d}{a \cdot c}\right)}{c}\\
\end{array}
\end{array}
if c < -9.2e64Initial program 43.3%
fma-define43.3%
fma-define43.3%
Simplified43.3%
Taylor expanded in c around inf 75.7%
*-commutative75.7%
Simplified75.7%
Taylor expanded in d around 0 75.7%
associate-*r/79.9%
Simplified79.9%
if -9.2e64 < c < -1.80000000000000006e-147Initial program 87.5%
if -1.80000000000000006e-147 < c < 9.5Initial program 68.6%
fma-define68.6%
fma-define68.6%
Simplified68.6%
Taylor expanded in d around inf 91.6%
if 9.5 < c Initial program 43.4%
fma-define43.4%
fma-define43.4%
Simplified43.4%
Taylor expanded in c around inf 77.2%
*-commutative77.2%
Simplified77.2%
Taylor expanded in a around inf 77.1%
associate-/l*83.9%
*-commutative83.9%
associate-/r*82.6%
Simplified82.6%
Taylor expanded in b around 0 77.1%
associate-*r/83.9%
Simplified83.9%
(FPCore (a b c d) :precision binary64 (if (or (<= d -2.3e-27) (not (<= d 1720000000000.0))) (/ (+ b (* a (/ c d))) d) (/ (+ a (/ (* b d) c)) c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -2.3e-27) || !(d <= 1720000000000.0)) {
tmp = (b + (a * (c / d))) / d;
} else {
tmp = (a + ((b * d) / c)) / 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 ((d <= (-2.3d-27)) .or. (.not. (d <= 1720000000000.0d0))) then
tmp = (b + (a * (c / d))) / d
else
tmp = (a + ((b * d) / c)) / c
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -2.3e-27) || !(d <= 1720000000000.0)) {
tmp = (b + (a * (c / d))) / d;
} else {
tmp = (a + ((b * d) / c)) / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (d <= -2.3e-27) or not (d <= 1720000000000.0): tmp = (b + (a * (c / d))) / d else: tmp = (a + ((b * d) / c)) / c return tmp
function code(a, b, c, d) tmp = 0.0 if ((d <= -2.3e-27) || !(d <= 1720000000000.0)) tmp = Float64(Float64(b + Float64(a * Float64(c / d))) / d); else tmp = Float64(Float64(a + Float64(Float64(b * d) / c)) / c); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if ((d <= -2.3e-27) || ~((d <= 1720000000000.0))) tmp = (b + (a * (c / d))) / d; else tmp = (a + ((b * d) / c)) / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -2.3e-27], N[Not[LessEqual[d, 1720000000000.0]], $MachinePrecision]], N[(N[(b + N[(a * N[(c / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], N[(N[(a + N[(N[(b * d), $MachinePrecision] / c), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -2.3 \cdot 10^{-27} \lor \neg \left(d \leq 1720000000000\right):\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a + \frac{b \cdot d}{c}}{c}\\
\end{array}
\end{array}
if d < -2.2999999999999999e-27 or 1.72e12 < d Initial program 48.7%
fma-define48.7%
fma-define48.7%
Simplified48.7%
Taylor expanded in d around inf 70.8%
associate-/l*75.8%
Simplified75.8%
if -2.2999999999999999e-27 < d < 1.72e12Initial program 73.9%
fma-define73.9%
fma-define73.9%
Simplified73.9%
Taylor expanded in c around inf 87.9%
*-commutative87.9%
Simplified87.9%
Final simplification81.4%
(FPCore (a b c d) :precision binary64 (if (or (<= d -2.8e-12) (not (<= d 1.95e+167))) (/ b d) (/ (+ a (* b (/ d c))) c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -2.8e-12) || !(d <= 1.95e+167)) {
tmp = b / d;
} else {
tmp = (a + (b * (d / c))) / 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 ((d <= (-2.8d-12)) .or. (.not. (d <= 1.95d+167))) then
tmp = b / d
else
tmp = (a + (b * (d / c))) / c
end if
code = tmp
end function
public static double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -2.8e-12) || !(d <= 1.95e+167)) {
tmp = b / d;
} else {
tmp = (a + (b * (d / c))) / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (d <= -2.8e-12) or not (d <= 1.95e+167): tmp = b / d else: tmp = (a + (b * (d / c))) / c return tmp
function code(a, b, c, d) tmp = 0.0 if ((d <= -2.8e-12) || !(d <= 1.95e+167)) tmp = Float64(b / d); else tmp = Float64(Float64(a + Float64(b * Float64(d / c))) / c); end return tmp end
function tmp_2 = code(a, b, c, d) tmp = 0.0; if ((d <= -2.8e-12) || ~((d <= 1.95e+167))) tmp = b / d; else tmp = (a + (b * (d / c))) / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -2.8e-12], N[Not[LessEqual[d, 1.95e+167]], $MachinePrecision]], N[(b / d), $MachinePrecision], N[(N[(a + N[(b * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -2.8 \cdot 10^{-12} \lor \neg \left(d \leq 1.95 \cdot 10^{+167}\right):\\
\;\;\;\;\frac{b}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a + b \cdot \frac{d}{c}}{c}\\
\end{array}
\end{array}
if d < -2.8000000000000002e-12 or 1.9499999999999999e167 < d Initial program 44.7%
fma-define44.7%
fma-define44.7%
Simplified44.7%
Taylor expanded in c around 0 74.0%
if -2.8000000000000002e-12 < d < 1.9499999999999999e167Initial program 70.8%
fma-define70.8%
fma-define70.9%
Simplified70.9%
Taylor expanded in c around inf 77.5%
*-commutative77.5%
Simplified77.5%
Taylor expanded in d around 0 77.5%
associate-*r/78.8%
Simplified78.8%
Final simplification76.9%
(FPCore (a b c d) :precision binary64 (if (<= d -8.6e-26) (/ (+ b (* c (/ a d))) d) (if (<= d 10500000.0) (/ (+ a (/ (* b d) c)) c) (/ (+ b (* a (/ c d))) d))))
double code(double a, double b, double c, double d) {
double tmp;
if (d <= -8.6e-26) {
tmp = (b + (c * (a / d))) / d;
} else if (d <= 10500000.0) {
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 (d <= (-8.6d-26)) then
tmp = (b + (c * (a / d))) / d
else if (d <= 10500000.0d0) 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 (d <= -8.6e-26) {
tmp = (b + (c * (a / d))) / d;
} else if (d <= 10500000.0) {
tmp = (a + ((b * d) / c)) / c;
} else {
tmp = (b + (a * (c / d))) / d;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if d <= -8.6e-26: tmp = (b + (c * (a / d))) / d elif d <= 10500000.0: 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 (d <= -8.6e-26) tmp = Float64(Float64(b + Float64(c * Float64(a / d))) / d); elseif (d <= 10500000.0) tmp = Float64(Float64(a + Float64(Float64(b * 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 (d <= -8.6e-26) tmp = (b + (c * (a / d))) / d; elseif (d <= 10500000.0) tmp = (a + ((b * d) / c)) / c; else tmp = (b + (a * (c / d))) / d; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[LessEqual[d, -8.6e-26], N[(N[(b + N[(c * N[(a / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / d), $MachinePrecision], If[LessEqual[d, 10500000.0], N[(N[(a + N[(N[(b * d), $MachinePrecision] / c), $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}\;d \leq -8.6 \cdot 10^{-26}:\\
\;\;\;\;\frac{b + c \cdot \frac{a}{d}}{d}\\
\mathbf{elif}\;d \leq 10500000:\\
\;\;\;\;\frac{a + \frac{b \cdot d}{c}}{c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b + a \cdot \frac{c}{d}}{d}\\
\end{array}
\end{array}
if d < -8.59999999999999976e-26Initial program 55.7%
fma-define55.7%
fma-define55.7%
Simplified55.7%
Taylor expanded in d around inf 72.7%
*-commutative72.7%
associate-/l*77.5%
Applied egg-rr77.5%
if -8.59999999999999976e-26 < d < 1.05e7Initial program 73.9%
fma-define73.9%
fma-define73.9%
Simplified73.9%
Taylor expanded in c around inf 87.9%
*-commutative87.9%
Simplified87.9%
if 1.05e7 < d Initial program 39.8%
fma-define39.8%
fma-define39.8%
Simplified39.8%
Taylor expanded in d around inf 68.3%
associate-/l*74.8%
Simplified74.8%
Final simplification81.7%
(FPCore (a b c d) :precision binary64 (if (or (<= d -2e-13) (not (<= d 750000000000.0))) (/ b d) (/ a c)))
double code(double a, double b, double c, double d) {
double tmp;
if ((d <= -2e-13) || !(d <= 750000000000.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 ((d <= (-2d-13)) .or. (.not. (d <= 750000000000.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 ((d <= -2e-13) || !(d <= 750000000000.0)) {
tmp = b / d;
} else {
tmp = a / c;
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if (d <= -2e-13) or not (d <= 750000000000.0): tmp = b / d else: tmp = a / c return tmp
function code(a, b, c, d) tmp = 0.0 if ((d <= -2e-13) || !(d <= 750000000000.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 ((d <= -2e-13) || ~((d <= 750000000000.0))) tmp = b / d; else tmp = a / c; end tmp_2 = tmp; end
code[a_, b_, c_, d_] := If[Or[LessEqual[d, -2e-13], N[Not[LessEqual[d, 750000000000.0]], $MachinePrecision]], N[(b / d), $MachinePrecision], N[(a / c), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;d \leq -2 \cdot 10^{-13} \lor \neg \left(d \leq 750000000000\right):\\
\;\;\;\;\frac{b}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{a}{c}\\
\end{array}
\end{array}
if d < -2.0000000000000001e-13 or 7.5e11 < d Initial program 47.2%
fma-define47.2%
fma-define47.2%
Simplified47.2%
Taylor expanded in c around 0 66.3%
if -2.0000000000000001e-13 < d < 7.5e11Initial program 74.7%
fma-define74.7%
fma-define74.7%
Simplified74.7%
Taylor expanded in c around inf 68.3%
Final simplification67.3%
(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 60.4%
fma-define60.4%
fma-define60.4%
Simplified60.4%
Taylor expanded in c around inf 43.9%
(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 2024172
(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))))