Average Error: 25.5 → 12.3
Time: 18.3s
Precision: 64
Internal Precision: 128
\[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
\[\begin{array}{l} \mathbf{if}\;c \le -1.0598670353086325 \cdot 10^{+133}:\\ \;\;\;\;\frac{-a}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;c \le 2.0830718422818717 \cdot 10^{+146}:\\ \;\;\;\;\frac{\frac{(a \cdot c + \left(d \cdot b\right))_*}{\sqrt{d^2 + c^2}^*}}{\sqrt{d^2 + c^2}^*}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\sqrt{d^2 + c^2}^*}\\ \end{array}\]

Error

Bits error versus a

Bits error versus b

Bits error versus c

Bits error versus d

Target

Original25.5
Target0.4
Herbie12.3
\[\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 c < -1.0598670353086325e+133

    1. Initial program 42.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Initial simplification42.9

      \[\leadsto \frac{(a \cdot c + \left(b \cdot d\right))_*}{(d \cdot d + \left(c \cdot c\right))_*}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt42.9

      \[\leadsto \frac{(a \cdot c + \left(b \cdot d\right))_*}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied *-un-lft-identity42.9

      \[\leadsto \frac{\color{blue}{1 \cdot (a \cdot c + \left(b \cdot d\right))_*}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
    6. Applied times-frac42.9

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

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

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

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

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

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

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

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

    if -1.0598670353086325e+133 < c < 2.0830718422818717e+146

    1. Initial program 18.2

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Initial simplification18.2

      \[\leadsto \frac{(a \cdot c + \left(b \cdot d\right))_*}{(d \cdot d + \left(c \cdot c\right))_*}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt18.2

      \[\leadsto \frac{(a \cdot c + \left(b \cdot d\right))_*}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied *-un-lft-identity18.2

      \[\leadsto \frac{\color{blue}{1 \cdot (a \cdot c + \left(b \cdot d\right))_*}}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}\]
    6. Applied times-frac18.2

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

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

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

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

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

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

    if 2.0830718422818717e+146 < c

    1. Initial program 44.4

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Initial simplification44.4

      \[\leadsto \frac{(a \cdot c + \left(b \cdot d\right))_*}{(d \cdot d + \left(c \cdot c\right))_*}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt44.4

      \[\leadsto \frac{(a \cdot c + \left(b \cdot d\right))_*}{\color{blue}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*} \cdot \sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    5. Applied *-un-lft-identity44.4

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

      \[\leadsto \color{blue}{\frac{1}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}} \cdot \frac{(a \cdot c + \left(b \cdot d\right))_*}{\sqrt{(d \cdot d + \left(c \cdot c\right))_*}}}\]
    7. Simplified44.4

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -1.0598670353086325 \cdot 10^{+133}:\\ \;\;\;\;\frac{-a}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;c \le 2.0830718422818717 \cdot 10^{+146}:\\ \;\;\;\;\frac{\frac{(a \cdot c + \left(d \cdot b\right))_*}{\sqrt{d^2 + c^2}^*}}{\sqrt{d^2 + c^2}^*}\\ \mathbf{else}:\\ \;\;\;\;\frac{a}{\sqrt{d^2 + c^2}^*}\\ \end{array}\]

Reproduce

herbie shell --seed 2018362 +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: 14.4s)Debug log

start72.0ms

Algorithm
intervals

setup17.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 23.8b

localize16.0ms

Local error

Found 2 expressions with local error:

23.8b
(/ (fma a c (* b d)) (fma d d (* c c)))
0.0b
(fma a c (* b d))

rewrite4.0ms

Algorithm
rewrite-expression-head
Counts
2 → 39
Calls

2 calls. Slowest were:

3.0ms
(/ (fma a c (* b d)) (fma d d (* c c)))
0.0ms
(fma a c (* b d))

series132.0ms

Counts
2 → 6
Calls

2 calls. Slowest were:

87.0ms
(/ (fma a c (* b d)) (fma d d (* c c)))
45.0ms
(fma a c (* b d))

simplify611.0ms

Counts
31 → 45
Calls

31 calls. Slowest were:

275.0ms
(/ (* (* (fma a c (* b d)) (fma a c (* b d))) (fma a c (* b d))) (* (* (fma d d (* c c)) (fma d d (* c c))) (fma d d (* c c))))
37.0ms
(/ (* (cbrt (fma a c (* b d))) (cbrt (fma a c (* b d)))) (* (cbrt (fma d d (* c c))) (cbrt (fma d d (* c c)))))
26.0ms
(/ (* (cbrt (fma a c (* b d))) (cbrt (fma a c (* b d)))) 1)

prune574.0ms

Pruning

8 alts after pruning (8 fresh and 0 done)

Merged error: 16.3b

localize31.0ms

Local error

Found 4 expressions with local error:

16.0b
(/ (fma d b (* a c)) (hypot d c))
0.3b
(fma d b (* a c))
0.3b
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
0.0b
(/ 1 (hypot d c))

rewrite15.0ms

Algorithm
rewrite-expression-head
Counts
4 → 94
Calls

4 calls. Slowest were:

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

series289.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

133.0ms
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
79.0ms
(/ (fma d b (* a c)) (hypot d c))
46.0ms
(fma d b (* a c))
32.0ms
(/ 1 (hypot d c))

simplify2.2s

Counts
67 → 106
Calls

67 calls. Slowest were:

598.0ms
(* (* (* (/ 1 (hypot d c)) (/ 1 (hypot d c))) (/ 1 (hypot d c))) (* (* (/ (fma d b (* a c)) (hypot d c)) (/ (fma d b (* a c)) (hypot d c))) (/ (fma d b (* a c)) (hypot d c))))
315.0ms
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
219.0ms
(/ (* (* (fma d b (* a c)) (fma d b (* a c))) (fma d b (* a c))) (* (* (hypot d c) (hypot d c)) (hypot d c)))

prune1.4s

Pruning

10 alts after pruning (10 fresh and 0 done)

Merged error: 6.8b

localize6.0ms

Local error

Found 4 expressions with local error:

16.1b
(* (/ 1 (hypot d c)) (fma d b (* a c)))
0.3b
(fma d b (* a c))
0.1b
(/ (* (/ 1 (hypot d c)) (fma d b (* a c))) (hypot d c))
0.0b
(/ 1 (hypot d c))

rewrite13.0ms

Algorithm
rewrite-expression-head
Counts
4 → 77
Calls

4 calls. Slowest were:

7.0ms
(/ (* (/ 1 (hypot d c)) (fma d b (* a c))) (hypot d c))
4.0ms
(* (/ 1 (hypot d c)) (fma d b (* a c)))
1.0ms
(/ 1 (hypot d c))

series303.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

120.0ms
(/ (* (/ 1 (hypot d c)) (fma d b (* a c))) (hypot d c))
104.0ms
(* (/ 1 (hypot d c)) (fma d b (* a c)))
45.0ms
(/ 1 (hypot d c))
35.0ms
(fma d b (* a c))

simplify2.0s

Counts
44 → 89
Calls

44 calls. Slowest were:

868.0ms
(* (* (* (/ 1 (hypot d c)) (/ 1 (hypot d c))) (/ 1 (hypot d c))) (* (* (fma d b (* a c)) (fma d b (* a c))) (fma d b (* a c))))
251.0ms
(- (log (* (/ 1 (hypot d c)) (fma d b (* a c)))) (log (hypot d c)))
186.0ms
(* (/ 1 (hypot d c)) (fma d b (* a c)))

prune1.0s

Pruning

11 alts after pruning (11 fresh and 0 done)

Merged error: 6.7b

localize11.0ms

Local error

Found 4 expressions with local error:

16.0b
(/ (fma a c (* b d)) (hypot d c))
0.1b
(/ (/ (fma a c (* b d)) (hypot d c)) (hypot d c))
0.0b
(hypot d c)
0.0b
(hypot d c)

rewrite22.0ms

Algorithm
rewrite-expression-head
Counts
4 → 121
Calls

4 calls. Slowest were:

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

series335.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

130.0ms
(/ (fma a c (* b d)) (hypot d c))
129.0ms
(/ (/ (fma a c (* b d)) (hypot d c)) (hypot d c))
38.0ms
(hypot d c)
38.0ms
(hypot d c)

simplify3.1s

Counts
135 → 133
Calls

135 calls. Slowest were:

315.0ms
(/ (/ (* (* (fma a c (* b d)) (fma a c (* b d))) (fma a c (* b d))) (* (* (hypot d c) (hypot d c)) (hypot d c))) (* (* (hypot d c) (hypot d c)) (hypot d c)))
238.0ms
(/ (* (* (/ (fma a c (* b d)) (hypot d c)) (/ (fma a c (* b d)) (hypot d c))) (/ (fma a c (* b d)) (hypot d c))) (* (* (hypot d c) (hypot d c)) (hypot d c)))
220.0ms
(/ (/ (* (cbrt (fma a c (* b d))) (cbrt (fma a c (* b d)))) (* (cbrt (hypot d c)) (cbrt (hypot d c)))) 1)

prune1.7s

Pruning

11 alts after pruning (10 fresh and 1 done)

Merged error: 2.9b

regimes396.0ms

Accuracy

29.8% (9.8b remaining)

Error of 12.3b against oracle of 2.4b and baseline of 16.4b

bsearch157.0ms