
(FPCore (a b c d) :precision binary64 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
return ((b * c) - (a * 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 = ((b * c) - (a * d)) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
def code(a, b, c, d): return ((b * c) - (a * d)) / ((c * c) + (d * d))
function code(a, b, c, d) return Float64(Float64(Float64(b * c) - Float64(a * d)) / Float64(Float64(c * c) + Float64(d * d))) end
function tmp = code(a, b, c, d) tmp = ((b * c) - (a * d)) / ((c * c) + (d * d)); end
code[a_, b_, c_, d_] := N[(N[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (a b c d) :precision binary64 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
double code(double a, double b, double c, double d) {
return ((b * c) - (a * 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 = ((b * c) - (a * d)) / ((c * c) + (d * d))
end function
public static double code(double a, double b, double c, double d) {
return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
def code(a, b, c, d): return ((b * c) - (a * d)) / ((c * c) + (d * d))
function code(a, b, c, d) return Float64(Float64(Float64(b * c) - Float64(a * d)) / Float64(Float64(c * c) + Float64(d * d))) end
function tmp = code(a, b, c, d) tmp = ((b * c) - (a * d)) / ((c * c) + (d * d)); end
code[a_, b_, c_, d_] := N[(N[(N[(b * c), $MachinePrecision] - N[(a * d), $MachinePrecision]), $MachinePrecision] / N[(N[(c * c), $MachinePrecision] + N[(d * d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\end{array}
(FPCore (a b c d)
:precision binary64
(if (<= c -1.05e+46)
(/ (fma (/ (- d) c) a b) c)
(if (<= c -5.2e-76)
(/ (- (* b c) (* d a)) (+ (* d d) (* c c)))
(if (<= c 3.4e-26)
(/ (fma b (/ c d) (- a)) d)
(/ (fma (- d) (/ a c) b) c)))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -1.05e+46) {
tmp = fma((-d / c), a, b) / c;
} else if (c <= -5.2e-76) {
tmp = ((b * c) - (d * a)) / ((d * d) + (c * c));
} else if (c <= 3.4e-26) {
tmp = fma(b, (c / d), -a) / d;
} else {
tmp = fma(-d, (a / c), b) / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -1.05e+46) tmp = Float64(fma(Float64(Float64(-d) / c), a, b) / c); elseif (c <= -5.2e-76) tmp = Float64(Float64(Float64(b * c) - Float64(d * a)) / Float64(Float64(d * d) + Float64(c * c))); elseif (c <= 3.4e-26) tmp = Float64(fma(b, Float64(c / d), Float64(-a)) / d); else tmp = Float64(fma(Float64(-d), Float64(a / c), b) / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -1.05e+46], N[(N[(N[((-d) / c), $MachinePrecision] * a + b), $MachinePrecision] / c), $MachinePrecision], If[LessEqual[c, -5.2e-76], N[(N[(N[(b * c), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision] / N[(N[(d * d), $MachinePrecision] + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[c, 3.4e-26], N[(N[(b * N[(c / d), $MachinePrecision] + (-a)), $MachinePrecision] / d), $MachinePrecision], N[(N[((-d) * N[(a / c), $MachinePrecision] + b), $MachinePrecision] / c), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -1.05 \cdot 10^{+46}:\\
\;\;\;\;\frac{\mathsf{fma}\left(\frac{-d}{c}, a, b\right)}{c}\\
\mathbf{elif}\;c \leq -5.2 \cdot 10^{-76}:\\
\;\;\;\;\frac{b \cdot c - d \cdot a}{d \cdot d + c \cdot c}\\
\mathbf{elif}\;c \leq 3.4 \cdot 10^{-26}:\\
\;\;\;\;\frac{\mathsf{fma}\left(b, \frac{c}{d}, -a\right)}{d}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(-d, \frac{a}{c}, b\right)}{c}\\
\end{array}
\end{array}
if c < -1.05e46Initial program 49.8%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
mul-1-negN/A
associate-/l*N/A
*-commutativeN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
distribute-neg-fracN/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6487.9
Applied rewrites87.9%
if -1.05e46 < c < -5.1999999999999999e-76Initial program 88.7%
if -5.1999999999999999e-76 < c < 3.40000000000000013e-26Initial program 64.4%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
mul-1-negN/A
associate-/l*N/A
*-commutativeN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
distribute-neg-fracN/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6431.2
Applied rewrites31.2%
Taylor expanded in c around 0
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
unpow2N/A
associate-/r*N/A
div-subN/A
sub-negN/A
mul-1-negN/A
+-commutativeN/A
lower-/.f64N/A
+-commutativeN/A
associate-/l*N/A
lower-fma.f64N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6483.7
Applied rewrites83.7%
if 3.40000000000000013e-26 < c Initial program 43.6%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
mul-1-negN/A
associate-/l*N/A
*-commutativeN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
distribute-neg-fracN/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6485.0
Applied rewrites85.0%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
*-commutativeN/A
*-commutativeN/A
associate-*r/N/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f64N/A
mul-1-negN/A
lower-neg.f64N/A
lower-/.f6485.0
Applied rewrites85.0%
Final simplification85.5%
(FPCore (a b c d)
:precision binary64
(if (<= c -3.2e+156)
(/ b c)
(if (<= c -3.4e-151)
(* (/ c (fma d d (* c c))) b)
(if (<= c 1.8e-26)
(/ (- a) d)
(if (<= c 3.2e+93) (/ (- (* b c) (* d a)) (* c c)) (/ b c))))))
double code(double a, double b, double c, double d) {
double tmp;
if (c <= -3.2e+156) {
tmp = b / c;
} else if (c <= -3.4e-151) {
tmp = (c / fma(d, d, (c * c))) * b;
} else if (c <= 1.8e-26) {
tmp = -a / d;
} else if (c <= 3.2e+93) {
tmp = ((b * c) - (d * a)) / (c * c);
} else {
tmp = b / c;
}
return tmp;
}
function code(a, b, c, d) tmp = 0.0 if (c <= -3.2e+156) tmp = Float64(b / c); elseif (c <= -3.4e-151) tmp = Float64(Float64(c / fma(d, d, Float64(c * c))) * b); elseif (c <= 1.8e-26) tmp = Float64(Float64(-a) / d); elseif (c <= 3.2e+93) tmp = Float64(Float64(Float64(b * c) - Float64(d * a)) / Float64(c * c)); else tmp = Float64(b / c); end return tmp end
code[a_, b_, c_, d_] := If[LessEqual[c, -3.2e+156], N[(b / c), $MachinePrecision], If[LessEqual[c, -3.4e-151], N[(N[(c / N[(d * d + N[(c * c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * b), $MachinePrecision], If[LessEqual[c, 1.8e-26], N[((-a) / d), $MachinePrecision], If[LessEqual[c, 3.2e+93], N[(N[(N[(b * c), $MachinePrecision] - N[(d * a), $MachinePrecision]), $MachinePrecision] / N[(c * c), $MachinePrecision]), $MachinePrecision], N[(b / c), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;c \leq -3.2 \cdot 10^{+156}:\\
\;\;\;\;\frac{b}{c}\\
\mathbf{elif}\;c \leq -3.4 \cdot 10^{-151}:\\
\;\;\;\;\frac{c}{\mathsf{fma}\left(d, d, c \cdot c\right)} \cdot b\\
\mathbf{elif}\;c \leq 1.8 \cdot 10^{-26}:\\
\;\;\;\;\frac{-a}{d}\\
\mathbf{elif}\;c \leq 3.2 \cdot 10^{+93}:\\
\;\;\;\;\frac{b \cdot c - d \cdot a}{c \cdot c}\\
\mathbf{else}:\\
\;\;\;\;\frac{b}{c}\\
\end{array}
\end{array}
if c < -3.20000000000000002e156 or 3.2000000000000001e93 < c Initial program 35.4%
Taylor expanded in c around inf
lower-/.f6475.4
Applied rewrites75.4%
if -3.20000000000000002e156 < c < -3.4000000000000003e-151Initial program 73.4%
Taylor expanded in c around inf
lower-/.f64N/A
+-commutativeN/A
mul-1-negN/A
associate-/l*N/A
*-commutativeN/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
distribute-neg-fracN/A
mul-1-negN/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6452.2
Applied rewrites52.2%
Taylor expanded in b around inf
*-commutativeN/A
associate-*l/N/A
lower-*.f64N/A
lower-/.f64N/A
+-commutativeN/A
unpow2N/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6454.5
Applied rewrites54.5%
if -3.4000000000000003e-151 < c < 1.8000000000000001e-26Initial program 73.1%
Taylor expanded in c around 0
associate-*r/N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6469.2
Applied rewrites69.2%
if 1.8000000000000001e-26 < c < 3.2000000000000001e93Initial program 75.4%
Taylor expanded in c around inf
unpow2N/A
lower-*.f6452.4
Applied rewrites52.4%
Final simplification65.9%
(FPCore (a b c d) :precision binary64 (if (< (fabs d) (fabs c)) (/ (- b (* a (/ d c))) (+ c (* d (/ d c)))) (/ (+ (- a) (* b (/ c d))) (+ d (* c (/ c d))))))
double code(double a, double b, double c, double d) {
double tmp;
if (fabs(d) < fabs(c)) {
tmp = (b - (a * (d / c))) / (c + (d * (d / c)));
} else {
tmp = (-a + (b * (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 = (b - (a * (d / c))) / (c + (d * (d / c)))
else
tmp = (-a + (b * (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 = (b - (a * (d / c))) / (c + (d * (d / c)));
} else {
tmp = (-a + (b * (c / d))) / (d + (c * (c / d)));
}
return tmp;
}
def code(a, b, c, d): tmp = 0 if math.fabs(d) < math.fabs(c): tmp = (b - (a * (d / c))) / (c + (d * (d / c))) else: tmp = (-a + (b * (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(b - Float64(a * Float64(d / c))) / Float64(c + Float64(d * Float64(d / c)))); else tmp = Float64(Float64(Float64(-a) + Float64(b * 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 = (b - (a * (d / c))) / (c + (d * (d / c))); else tmp = (-a + (b * (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[(b - N[(a * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(c + N[(d * N[(d / c), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[((-a) + N[(b * 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{b - a \cdot \frac{d}{c}}{c + d \cdot \frac{d}{c}}\\
\mathbf{else}:\\
\;\;\;\;\frac{\left(-a\right) + b \cdot \frac{c}{d}}{d + c \cdot \frac{c}{d}}\\
\end{array}
\end{array}
herbie shell --seed 2024230
(FPCore (a b c d)
:name "Complex division, imag part"
:precision binary64
:alt
(! :herbie-platform default (if (< (fabs d) (fabs c)) (/ (- b (* a (/ d c))) (+ c (* d (/ d c)))) (/ (+ (- a) (* b (/ c d))) (+ d (* c (/ c d))))))
(/ (- (* b c) (* a d)) (+ (* c c) (* d d))))