Complex division, imag part

?

Percentage Accurate: 61.5% → 97.3%
Time: 19.2s
Precision: binary64
Cost: 33152

?

\[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
\[\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{\frac{-a}{\mathsf{hypot}\left(c, d\right)}}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right) \]
(FPCore (a b c d)
 :precision binary64
 (/ (- (* b c) (* a d)) (+ (* c c) (* d d))))
(FPCore (a b c d)
 :precision binary64
 (fma
  (/ c (hypot c d))
  (/ b (hypot c d))
  (/ (/ (- a) (hypot c d)) (/ (hypot c d) d))))
double code(double a, double b, double c, double d) {
	return ((b * c) - (a * d)) / ((c * c) + (d * d));
}
double code(double a, double b, double c, double d) {
	return fma((c / hypot(c, d)), (b / hypot(c, d)), ((-a / hypot(c, d)) / (hypot(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 code(a, b, c, d)
	return fma(Float64(c / hypot(c, d)), Float64(b / hypot(c, d)), Float64(Float64(Float64(-a) / hypot(c, d)) / Float64(hypot(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]
code[a_, b_, c_, d_] := N[(N[(c / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] * N[(b / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] + N[(N[((-a) / N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision]), $MachinePrecision] / N[(N[Sqrt[c ^ 2 + d ^ 2], $MachinePrecision] / d), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d}
\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{\frac{-a}{\mathsf{hypot}\left(c, d\right)}}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right)

Local Percentage Accuracy?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 11 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Bogosity?

Bogosity

Target

Original61.5%
Target99.5%
Herbie97.3%
\[\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} \]

Derivation?

  1. Initial program 60.3%

    \[\frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]
  2. Applied egg-rr80.9%

    \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\frac{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}}{d}}\right)} \]
    Step-by-step derivation

    [Start]60.3

    \[ \frac{b \cdot c - a \cdot d}{c \cdot c + d \cdot d} \]

    div-sub [=>]57.1

    \[ \color{blue}{\frac{b \cdot c}{c \cdot c + d \cdot d} - \frac{a \cdot d}{c \cdot c + d \cdot d}} \]

    *-commutative [=>]57.1

    \[ \frac{\color{blue}{c \cdot b}}{c \cdot c + d \cdot d} - \frac{a \cdot d}{c \cdot c + d \cdot d} \]

    add-sqr-sqrt [=>]57.1

    \[ \frac{c \cdot b}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}} - \frac{a \cdot d}{c \cdot c + d \cdot d} \]

    times-frac [=>]62.1

    \[ \color{blue}{\frac{c}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{b}{\sqrt{c \cdot c + d \cdot d}}} - \frac{a \cdot d}{c \cdot c + d \cdot d} \]

    fma-neg [=>]62.1

    \[ \color{blue}{\mathsf{fma}\left(\frac{c}{\sqrt{c \cdot c + d \cdot d}}, \frac{b}{\sqrt{c \cdot c + d \cdot d}}, -\frac{a \cdot d}{c \cdot c + d \cdot d}\right)} \]

    hypot-def [=>]62.1

    \[ \mathsf{fma}\left(\frac{c}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}, \frac{b}{\sqrt{c \cdot c + d \cdot d}}, -\frac{a \cdot d}{c \cdot c + d \cdot d}\right) \]

    hypot-def [=>]75.0

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\color{blue}{\mathsf{hypot}\left(c, d\right)}}, -\frac{a \cdot d}{c \cdot c + d \cdot d}\right) \]

    associate-/l* [=>]80.9

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\color{blue}{\frac{a}{\frac{c \cdot c + d \cdot d}{d}}}\right) \]

    add-sqr-sqrt [=>]80.9

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\frac{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}{d}}\right) \]

    pow2 [=>]80.9

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\frac{\color{blue}{{\left(\sqrt{c \cdot c + d \cdot d}\right)}^{2}}}{d}}\right) \]

    hypot-def [=>]80.9

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\frac{{\color{blue}{\left(\mathsf{hypot}\left(c, d\right)\right)}}^{2}}{d}}\right) \]
  3. Applied egg-rr97.2%

    \[\leadsto \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\color{blue}{\mathsf{hypot}\left(c, d\right) \cdot \left(\mathsf{hypot}\left(c, d\right) \cdot \frac{1}{d}\right)}}\right) \]
    Step-by-step derivation

    [Start]80.9

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\frac{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}}{d}}\right) \]

    div-inv [=>]80.8

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\color{blue}{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2} \cdot \frac{1}{d}}}\right) \]

    unpow2 [=>]80.8

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\color{blue}{\left(\mathsf{hypot}\left(c, d\right) \cdot \mathsf{hypot}\left(c, d\right)\right)} \cdot \frac{1}{d}}\right) \]

    associate-*l* [=>]97.2

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\color{blue}{\mathsf{hypot}\left(c, d\right) \cdot \left(\mathsf{hypot}\left(c, d\right) \cdot \frac{1}{d}\right)}}\right) \]
  4. Applied egg-rr97.6%

    \[\leadsto \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\color{blue}{\frac{a}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}}\right) \]
    Step-by-step derivation

    [Start]97.2

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\mathsf{hypot}\left(c, d\right) \cdot \left(\mathsf{hypot}\left(c, d\right) \cdot \frac{1}{d}\right)}\right) \]

    associate-/r* [=>]97.6

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\color{blue}{\frac{\frac{a}{\mathsf{hypot}\left(c, d\right)}}{\mathsf{hypot}\left(c, d\right) \cdot \frac{1}{d}}}\right) \]

    div-inv [=>]97.6

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\color{blue}{\frac{a}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{1}{\mathsf{hypot}\left(c, d\right) \cdot \frac{1}{d}}}\right) \]

    un-div-inv [=>]97.6

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{1}{\color{blue}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}}\right) \]
  5. Simplified97.6%

    \[\leadsto \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\color{blue}{\frac{\frac{a}{\mathsf{hypot}\left(c, d\right)}}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}}\right) \]
    Step-by-step derivation

    [Start]97.6

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{a}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{1}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right) \]

    associate-*r/ [=>]97.6

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\color{blue}{\frac{\frac{a}{\mathsf{hypot}\left(c, d\right)} \cdot 1}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}}\right) \]

    *-rgt-identity [=>]97.6

    \[ \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, -\frac{\color{blue}{\frac{a}{\mathsf{hypot}\left(c, d\right)}}}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right) \]
  6. Final simplification97.6%

    \[\leadsto \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{\frac{-a}{\mathsf{hypot}\left(c, d\right)}}{\frac{\mathsf{hypot}\left(c, d\right)}{d}}\right) \]

Alternatives

Alternative 1
Accuracy92.2%
Cost33552
\[\begin{array}{l} t_0 := \frac{c}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := \frac{b}{\mathsf{hypot}\left(c, d\right)}\\ t_2 := \mathsf{fma}\left(t_0, t_1, \frac{-a}{\frac{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}}{d}}\right)\\ t_3 := \mathsf{fma}\left(t_0, t_1, \frac{-a}{d}\right)\\ \mathbf{if}\;d \leq -1.02 \cdot 10^{+166}:\\ \;\;\;\;t_3\\ \mathbf{elif}\;d \leq -1.6 \cdot 10^{-111}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;d \leq 3 \cdot 10^{-146}:\\ \;\;\;\;\frac{b + \frac{a}{\frac{1}{d}} \cdot \frac{-1}{c}}{c}\\ \mathbf{elif}\;d \leq 10^{+137}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_3\\ \end{array} \]
Alternative 2
Accuracy91.0%
Cost27344
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{b}{\frac{\mathsf{hypot}\left(c, d\right)}{c}} - d \cdot \frac{a}{{\left(\mathsf{hypot}\left(c, d\right)\right)}^{2}}\\ t_1 := \mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{-a}{d}\right)\\ \mathbf{if}\;d \leq -1.2 \cdot 10^{+121}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -6 \cdot 10^{-146}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 2.5 \cdot 10^{-144}:\\ \;\;\;\;\frac{b + \frac{a}{\frac{1}{d}} \cdot \frac{-1}{c}}{c}\\ \mathbf{elif}\;d \leq 4.5 \cdot 10^{+48}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Accuracy89.5%
Cost22025
\[\begin{array}{l} t_0 := c \cdot b - d \cdot a\\ t_1 := \frac{t_0}{c \cdot c + d \cdot d}\\ \mathbf{if}\;t_1 \leq -\infty \lor \neg \left(t_1 \leq 2 \cdot 10^{+221}\right):\\ \;\;\;\;\mathsf{fma}\left(\frac{c}{\mathsf{hypot}\left(c, d\right)}, \frac{b}{\mathsf{hypot}\left(c, d\right)}, \frac{-a}{d}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{t_0}{\mathsf{hypot}\left(c, d\right)}\\ \end{array} \]
Alternative 4
Accuracy84.6%
Cost14288
\[\begin{array}{l} t_0 := \frac{1}{\mathsf{hypot}\left(c, d\right)} \cdot \frac{c \cdot b - d \cdot a}{\mathsf{hypot}\left(c, d\right)}\\ t_1 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -4.5 \cdot 10^{+86}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -4.5 \cdot 10^{-85}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 8.8 \cdot 10^{-94}:\\ \;\;\;\;\frac{b - \frac{d \cdot a}{c}}{c}\\ \mathbf{elif}\;d \leq 1.6 \cdot 10^{+49}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Accuracy82.1%
Cost7496
\[\begin{array}{l} t_0 := c \cdot b - d \cdot a\\ t_1 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -4.2 \cdot 10^{+64}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -1.35 \cdot 10^{-89}:\\ \;\;\;\;\frac{t_0}{\mathsf{fma}\left(c, c, d \cdot d\right)}\\ \mathbf{elif}\;d \leq 4.8 \cdot 10^{-90}:\\ \;\;\;\;\frac{b - \frac{d \cdot a}{c}}{c}\\ \mathbf{elif}\;d \leq 5.2 \cdot 10^{+46}:\\ \;\;\;\;\frac{t_0}{c \cdot c + d \cdot d}\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 6
Accuracy82.1%
Cost1488
\[\begin{array}{l} t_0 := \frac{c \cdot b - d \cdot a}{c \cdot c + d \cdot d}\\ t_1 := \frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{if}\;d \leq -1.55 \cdot 10^{+65}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;d \leq -5.6 \cdot 10^{-98}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;d \leq 1.6 \cdot 10^{-92}:\\ \;\;\;\;\frac{b - \frac{d \cdot a}{c}}{c}\\ \mathbf{elif}\;d \leq 1.5 \cdot 10^{+49}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 7
Accuracy78.1%
Cost969
\[\begin{array}{l} \mathbf{if}\;d \leq -7.8 \cdot 10^{-38} \lor \neg \left(d \leq 0.32\right):\\ \;\;\;\;\frac{c}{d} \cdot \frac{b}{d} - \frac{a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - \frac{d \cdot a}{c}}{c}\\ \end{array} \]
Alternative 8
Accuracy72.7%
Cost841
\[\begin{array}{l} \mathbf{if}\;d \leq -1.62 \cdot 10^{+90} \lor \neg \left(d \leq 1.42 \cdot 10^{+25}\right):\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b - a \cdot \frac{d}{c}}{c}\\ \end{array} \]
Alternative 9
Accuracy63.1%
Cost521
\[\begin{array}{l} \mathbf{if}\;d \leq -3.6 \cdot 10^{-63} \lor \neg \left(d \leq 1.2\right):\\ \;\;\;\;\frac{-a}{d}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{c}\\ \end{array} \]
Alternative 10
Accuracy43.4%
Cost192
\[\frac{b}{c} \]

Reproduce?

herbie shell --seed 2023161 
(FPCore (a b c d)
  :name "Complex division, imag part"
  :precision binary64

  :herbie-target
  (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))))