Average Error: 25.7 → 14.3
Time: 1.5m
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 -2.9013770437529842 \cdot 10^{+79}:\\ \;\;\;\;\frac{-a}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;c \le 5.390280842657685 \cdot 10^{+57}:\\ \;\;\;\;\frac{\frac{(c \cdot a + \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.7
Target0.4
Herbie14.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 < -2.9013770437529842e+79

    1. Initial program 37.9

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

      \[\leadsto \color{blue}{\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-sqrt37.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-identity37.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-frac37.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. Simplified37.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. Simplified25.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-*l/25.1

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

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

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

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

    if -2.9013770437529842e+79 < c < 5.390280842657685e+57

    1. Initial program 18.3

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified18.3

      \[\leadsto \color{blue}{\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.3

      \[\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.3

      \[\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.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. Simplified18.3

      \[\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.3

      \[\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-*l/11.2

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

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

    if 5.390280842657685e+57 < c

    1. Initial program 35.5

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified35.5

      \[\leadsto \color{blue}{\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-sqrt35.5

      \[\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-identity35.5

      \[\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-frac35.5

      \[\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. Simplified35.5

      \[\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. Simplified23.3

      \[\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-*l/23.3

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -2.9013770437529842 \cdot 10^{+79}:\\ \;\;\;\;\frac{-a}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;c \le 5.390280842657685 \cdot 10^{+57}:\\ \;\;\;\;\frac{\frac{(c \cdot a + \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 2019002 +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: 1.5m)Debug log

sample69.0ms

Algorithm
intervals

simplify9.0ms

Counts
1 → 1
Calls

1 calls. Slowest were:

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

prune10.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 27.4b

localize23.0ms

Local error

Found 2 expressions with local error:

27.4b
(/ (fma a c (* b d)) (fma d d (* c c)))
0.3b
(fma a c (* b d))

rewrite8.0ms

Algorithm
rewrite-expression-head
Counts
2 → 39
Calls

2 calls. Slowest were:

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

series148.0ms

Counts
2 → 6
Calls

2 calls. Slowest were:

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

simplify604.0ms

Counts
31 → 45
Calls

31 calls. Slowest were:

241.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))))
26.0ms
(/ (* (cbrt (fma a c (* b d))) (cbrt (fma a c (* b d)))) (sqrt (fma d d (* c c))))
25.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)))))

prune512.0ms

Pruning

7 alts after pruning (7 fresh and 0 done)

Merged error: 19.4b

localize19.0ms

Local error

Found 4 expressions with local error:

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

rewrite13.0ms

Algorithm
rewrite-expression-head
Counts
4 → 94
Calls

4 calls. Slowest were:

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

series274.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

134.0ms
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
71.0ms
(/ (fma d b (* a c)) (hypot d c))
39.0ms
(hypot d c)
30.0ms
(/ 1 (hypot d c))

simplify2.0s

Counts
67 → 106
Calls

67 calls. Slowest were:

585.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))))
306.0ms
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
134.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))) (* (* (hypot d c) (hypot d c)) (hypot d c))))

prune1.1s

Pruning

10 alts after pruning (9 fresh and 1 done)

Merged error: 8.1b

localize9.0ms

Local error

Found 4 expressions with local error:

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

rewrite12.0ms

Algorithm
rewrite-expression-head
Counts
4 → 121
Calls

4 calls. Slowest were:

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

series253.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

97.0ms
(/ (/ (fma c a (* b d)) (hypot d c)) (hypot d c))
81.0ms
(/ (fma c a (* b d)) (hypot d c))
47.0ms
(fma c a (* b d))
29.0ms
(hypot d c)

simplify3.0s

Counts
135 → 133
Calls

135 calls. Slowest were:

322.0ms
(/ (/ (* (* (fma c a (* b d)) (fma c a (* b d))) (fma c a (* b d))) (* (* (hypot d c) (hypot d c)) (hypot d c))) (* (* (hypot d c) (hypot d c)) (hypot d c)))
273.0ms
(/ (* (* (/ (fma c a (* b d)) (hypot d c)) (/ (fma c a (* b d)) (hypot d c))) (/ (fma c a (* b d)) (hypot d c))) (* (* (hypot d c) (hypot d c)) (hypot d c)))
203.0ms
(/ (/ (* (cbrt (fma c a (* b d))) (cbrt (fma c a (* b d)))) (* (cbrt (hypot d c)) (cbrt (hypot d c)))) 1)

prune1.6s

Pruning

9 alts after pruning (8 fresh and 1 done)

Merged error: 2.6b

localize21.0ms

Local error

Found 4 expressions with local error:

9.8b
(/ (hypot d c) (/ (fma c a (* b d)) (sqrt (hypot d c))))
9.5b
(/ (fma c a (* b d)) (sqrt (hypot d c)))
0.3b
(/ (/ 1 (sqrt (hypot d c))) (/ (hypot d c) (/ (fma c a (* b d)) (sqrt (hypot d c)))))
0.3b
(/ 1 (sqrt (hypot d c)))

rewrite65.0ms

Algorithm
rewrite-expression-head
Counts
4 → 553
Calls

4 calls. Slowest were:

17.0ms
(/ (/ 1 (sqrt (hypot d c))) (/ (hypot d c) (/ (fma c a (* b d)) (sqrt (hypot d c)))))
12.0ms
(/ (hypot d c) (/ (fma c a (* b d)) (sqrt (hypot d c))))
8.0ms
(/ (fma c a (* b d)) (sqrt (hypot d c)))

series405.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

159.0ms
(/ (/ 1 (sqrt (hypot d c))) (/ (hypot d c) (/ (fma c a (* b d)) (sqrt (hypot d c)))))
100.0ms
(/ (hypot d c) (/ (fma c a (* b d)) (sqrt (hypot d c))))
100.0ms
(/ (fma c a (* b d)) (sqrt (hypot d c)))
46.0ms
(/ 1 (sqrt (hypot d c)))

simplify1.2m

Counts
878 → 565
Calls

878 calls. Slowest were:

676.0ms
(/ 1 (/ (* (cbrt (hypot d c)) (cbrt (hypot d c))) (/ (* (cbrt (fma c a (* b d))) (cbrt (fma c a (* b d)))) (sqrt 1))))
668.0ms
(/ 1 (/ (* (cbrt (hypot d c)) (cbrt (hypot d c))) (/ (* (cbrt (fma c a (* b d))) (cbrt (fma c a (* b d)))) (sqrt 1))))
534.0ms
(/ (* (cbrt (/ 1 (sqrt (hypot d c)))) (cbrt (/ 1 (sqrt (hypot d c))))) (/ (* (cbrt (hypot d c)) (cbrt (hypot d c))) (/ (sqrt (fma c a (* b d))) 1)))

prune5.4s

Pruning

9 alts after pruning (7 fresh and 2 done)

Merged error: 2.6b

regimes233.0ms

Accuracy

14.9% (11.9b remaining)

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

bsearch99.0ms

end0.0ms

sample1.0s

Algorithm
intervals