Average Error: 26.5 → 13.0
Time: 17.3s
Precision: 64
Internal Precision: 128
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;d \le -6.506377436448894 \cdot 10^{+135}:\\ \;\;\;\;\frac{-b}{\sqrt{c^2 + d^2}^*}\\ \mathbf{elif}\;d \le 4.881520392713296 \cdot 10^{+183}:\\ \;\;\;\;\frac{(a \cdot c + \left(b \cdot d\right))_* \cdot \frac{1}{\sqrt{c^2 + d^2}^*}}{\sqrt{c^2 + d^2}^*}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\sqrt{c^2 + d^2}^*}\\ \end{array}\]

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original26.5
Target0.5
Herbie13.0
\[\begin{array}{l} \mathbf{if}\;\left|d\right| \lt \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}\]

Derivation

  1. Split input into 3 regimes
  2. if d < -6.506377436448894e+135

    1. Initial program 43.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt43.3

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied *-un-lft-identity43.3

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac43.3

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified43.3

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c^2 + d^2}^*}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified29.0

      \[\leadsto \frac{1}{\sqrt{c^2 + d^2}^*} \cdot \color{blue}{\frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{c^2 + d^2}^*}}\]
    8. Using strategy rm
    9. Applied associate-*l/28.9

      \[\leadsto \color{blue}{\frac{1 \cdot \frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{c^2 + d^2}^*}}{\sqrt{c^2 + d^2}^*}}\]
    10. Simplified28.9

      \[\leadsto \frac{\color{blue}{\frac{(d \cdot b + \left(c \cdot a\right))_*}{\sqrt{c^2 + d^2}^*}}}{\sqrt{c^2 + d^2}^*}\]
    11. Taylor expanded around -inf 14.7

      \[\leadsto \frac{\color{blue}{-1 \cdot b}}{\sqrt{c^2 + d^2}^*}\]
    12. Simplified14.7

      \[\leadsto \frac{\color{blue}{-b}}{\sqrt{c^2 + d^2}^*}\]

    if -6.506377436448894e+135 < d < 4.881520392713296e+183

    1. Initial program 20.5

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt20.5

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied *-un-lft-identity20.5

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac20.5

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified20.5

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c^2 + d^2}^*}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified13.0

      \[\leadsto \frac{1}{\sqrt{c^2 + d^2}^*} \cdot \color{blue}{\frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{c^2 + d^2}^*}}\]
    8. Using strategy rm
    9. Applied associate-*r/12.9

      \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{c^2 + d^2}^*} \cdot (a \cdot c + \left(b \cdot d\right))_*}{\sqrt{c^2 + d^2}^*}}\]

    if 4.881520392713296e+183 < d

    1. Initial program 43.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt43.4

      \[\leadsto \frac{a \cdot c + b \cdot d}{\color{blue}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}}\]
    4. Applied *-un-lft-identity43.4

      \[\leadsto \frac{\color{blue}{1 \cdot \left(a \cdot c + b \cdot d\right)}}{\sqrt{c \cdot c + d \cdot d} \cdot \sqrt{c \cdot c + d \cdot d}}\]
    5. Applied times-frac43.4

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c \cdot c + d \cdot d}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}}\]
    6. Simplified43.4

      \[\leadsto \color{blue}{\frac{1}{\sqrt{c^2 + d^2}^*}} \cdot \frac{a \cdot c + b \cdot d}{\sqrt{c \cdot c + d \cdot d}}\]
    7. Simplified30.3

      \[\leadsto \frac{1}{\sqrt{c^2 + d^2}^*} \cdot \color{blue}{\frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{c^2 + d^2}^*}}\]
    8. Using strategy rm
    9. Applied associate-*l/30.3

      \[\leadsto \color{blue}{\frac{1 \cdot \frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{c^2 + d^2}^*}}{\sqrt{c^2 + d^2}^*}}\]
    10. Simplified30.3

      \[\leadsto \frac{\color{blue}{\frac{(d \cdot b + \left(c \cdot a\right))_*}{\sqrt{c^2 + d^2}^*}}}{\sqrt{c^2 + d^2}^*}\]
    11. Taylor expanded around inf 10.6

      \[\leadsto \frac{\color{blue}{b}}{\sqrt{c^2 + d^2}^*}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification13.0

    \[\leadsto \begin{array}{l} \mathbf{if}\;d \le -6.506377436448894 \cdot 10^{+135}:\\ \;\;\;\;\frac{-b}{\sqrt{c^2 + d^2}^*}\\ \mathbf{elif}\;d \le 4.881520392713296 \cdot 10^{+183}:\\ \;\;\;\;\frac{(a \cdot c + \left(b \cdot d\right))_* \cdot \frac{1}{\sqrt{c^2 + d^2}^*}}{\sqrt{c^2 + d^2}^*}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\sqrt{c^2 + d^2}^*}\\ \end{array}\]

Reproduce

herbie shell --seed 2019007 +o rules:numerics
(FPCore (a b c d)
  :name "Complex division, real part"

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

Details

Time bar (total: 16.1s)Debug log

sample38.0ms

Algorithm
intervals

simplify7.0ms

Counts
1 → 1
Calls

1 calls. Slowest were:

7.0ms
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))

prune13.0ms

Pruning

2 alts after pruning (2 fresh and 0 done)

Merged error: 28.5b

localize28.0ms

Local error

Found 3 expressions with local error:

28.5b
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))
0.9b
(+ (* a c) (* b d))
0.0b
(+ (* c c) (* d d))

rewrite15.0ms

Algorithm
rewrite-expression-head
Counts
3 → 61
Calls

3 calls. Slowest were:

10.0ms
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))
2.0ms
(+ (* a c) (* b d))
2.0ms
(+ (* c c) (* d d))

series155.0ms

Counts
3 → 9
Calls

3 calls. Slowest were:

91.0ms
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))
40.0ms
(+ (* a c) (* b d))
24.0ms
(+ (* c c) (* d d))

simplify1.5s

Counts
40 → 70
Calls

40 calls. Slowest were:

576.0ms
(- (* (* a c) (* a c)) (* (* b d) (* b d)))
302.0ms
(+ (* (* c c) (* c c)) (- (* (* d d) (* d d)) (* (* c c) (* d d))))
172.0ms
(/ (* (* (+ (* a c) (* b d)) (+ (* a c) (* b d))) (+ (* a c) (* b d))) (* (* (+ (* c c) (* d d)) (+ (* c c) (* d d))) (+ (* c c) (* d d))))

prune869.0ms

Pruning

9 alts after pruning (8 fresh and 1 done)

Merged error: 19.7b

localize25.0ms

Local error

Found 4 expressions with local error:

19.4b
(/ (fma a c (* b d)) (hypot c d))
0.7b
(fma a c (* b d))
0.2b
(* (/ 1 (hypot c d)) (/ (fma a c (* b d)) (hypot c d)))
0.0b
(/ 1 (hypot c d))

rewrite11.0ms

Algorithm
rewrite-expression-head
Counts
4 → 94
Calls

4 calls. Slowest were:

6.0ms
(* (/ 1 (hypot c d)) (/ (fma a c (* b d)) (hypot c d)))
3.0ms
(/ (fma a c (* b d)) (hypot c d))
1.0ms
(/ 1 (hypot c d))

series275.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

116.0ms
(* (/ 1 (hypot c d)) (/ (fma a c (* b d)) (hypot c d)))
87.0ms
(/ (fma a c (* b d)) (hypot c d))
42.0ms
(fma a c (* b d))
30.0ms
(/ 1 (hypot c d))

simplify2.0s

Counts
67 → 106
Calls

67 calls. Slowest were:

554.0ms
(* (* (* (/ 1 (hypot c d)) (/ 1 (hypot c d))) (/ 1 (hypot c d))) (* (* (/ (fma a c (* b d)) (hypot c d)) (/ (fma a c (* b d)) (hypot c d))) (/ (fma a c (* b d)) (hypot c d))))
281.0ms
(* (/ 1 (hypot c d)) (/ (fma a c (* b d)) (hypot c d)))
154.0ms
(* (* (* (/ 1 (hypot c d)) (/ 1 (hypot c d))) (/ 1 (hypot c d))) (/ (* (* (fma a c (* b d)) (fma a c (* b d))) (fma a c (* b d))) (* (* (hypot c d) (hypot c d)) (hypot c d))))

prune1.2s

Pruning

9 alts after pruning (8 fresh and 1 done)

Merged error: 7.6b

localize11.0ms

Local error

Found 3 expressions with local error:

19.4b
(/ (fma d b (* c a)) (hypot c d))
0.2b
(fma d b (* c a))
0.1b
(/ (/ (fma d b (* c a)) (hypot c d)) (hypot c d))

rewrite12.0ms

Algorithm
rewrite-expression-head
Counts
3 → 111
Calls

3 calls. Slowest were:

6.0ms
(/ (/ (fma d b (* c a)) (hypot c d)) (hypot c d))
4.0ms
(/ (fma d b (* c a)) (hypot c d))
0.0ms
(fma d b (* c a))

series217.0ms

Counts
3 → 9
Calls

3 calls. Slowest were:

101.0ms
(/ (/ (fma d b (* c a)) (hypot c d)) (hypot c d))
90.0ms
(/ (fma d b (* c a)) (hypot c d))
26.0ms
(fma d b (* c a))

simplify2.9s

Counts
132 → 120
Calls

132 calls. Slowest were:

271.0ms
(/ (/ (* (* (fma d b (* c a)) (fma d b (* c a))) (fma d b (* c a))) (* (* (hypot c d) (hypot c d)) (hypot c d))) (* (* (hypot c d) (hypot c d)) (hypot c d)))
241.0ms
(/ (* (* (/ (fma d b (* c a)) (hypot c d)) (/ (fma d b (* c a)) (hypot c d))) (/ (fma d b (* c a)) (hypot c d))) (* (* (hypot c d) (hypot c d)) (hypot c d)))
175.0ms
(/ (/ (* (cbrt (fma d b (* c a))) (cbrt (fma d b (* c a)))) 1) (* (cbrt (hypot c d)) (cbrt (hypot c d))))

prune1.5s

Pruning

10 alts after pruning (8 fresh and 2 done)

Merged error: 2.8b

localize12.0ms

Local error

Found 4 expressions with local error:

19.5b
(* (/ 1 (hypot c d)) (fma a c (* b d)))
0.7b
(fma a c (* b d))
0.1b
(/ (* (/ 1 (hypot c d)) (fma a c (* b d))) (hypot c d))
0.0b
(/ 1 (hypot c d))

rewrite28.0ms

Algorithm
rewrite-expression-head
Counts
4 → 77
Calls

4 calls. Slowest were:

15.0ms
(/ (* (/ 1 (hypot c d)) (fma a c (* b d))) (hypot c d))
9.0ms
(* (/ 1 (hypot c d)) (fma a c (* b d)))
2.0ms
(/ 1 (hypot c d))

series339.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

136.0ms
(/ (* (/ 1 (hypot c d)) (fma a c (* b d))) (hypot c d))
114.0ms
(* (/ 1 (hypot c d)) (fma a c (* b d)))
45.0ms
(/ 1 (hypot c d))
44.0ms
(fma a c (* b d))

simplify1.9s

Counts
44 → 89
Calls

44 calls. Slowest were:

728.0ms
(* (* (* (/ 1 (hypot c d)) (/ 1 (hypot c d))) (/ 1 (hypot c d))) (* (* (fma a c (* b d)) (fma a c (* b d))) (fma a c (* b d))))
255.0ms
(- (log (* (/ 1 (hypot c d)) (fma a c (* b d)))) (log (hypot c d)))
164.0ms
(* (/ 1 (hypot c d)) (fma a c (* b d)))

prune885.0ms

Pruning

10 alts after pruning (8 fresh and 2 done)

Merged error: 2.7b

regimes427.0ms

Accuracy

29.5% (10.4b remaining)

Error of 13.0b against oracle of 2.5b and baseline of 17.3b

bsearch83.0ms

end0.0ms

sample1.7s

Algorithm
intervals