Average Error: 25.5 → 13.1
Time: 17.0s
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 -4.0745539003521706 \cdot 10^{+83}:\\ \;\;\;\;\frac{-b}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;d \le 1.4791269730391837 \cdot 10^{+143}:\\ \;\;\;\;\frac{(d \cdot b + \left(a \cdot c\right))_* \cdot \frac{1}{\sqrt{d^2 + c^2}^*}}{\sqrt{d^2 + c^2}^*}\\ \mathbf{else}:\\ \;\;\;\;\frac{b}{\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
Herbie13.1
\[\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 < -4.0745539003521706e+83

    1. Initial program 37.0

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

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

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

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

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

      \[\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.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/23.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. Taylor expanded around -inf 17.8

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

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

    if -4.0745539003521706e+83 < d < 1.4791269730391837e+143

    1. Initial program 18.5

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified18.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-sqrt18.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-identity18.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-frac18.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. Simplified18.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. Simplified11.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/11.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}^*}}\]

    if 1.4791269730391837e+143 < d

    1. Initial program 43.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified43.1

      \[\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-sqrt43.1

      \[\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-identity43.1

      \[\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-frac43.1

      \[\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. Simplified43.1

      \[\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. Simplified27.8

      \[\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/27.7

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

      \[\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 0 14.3

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

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

Reproduce

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

sample66.0ms

Algorithm
intervals

simplify14.0ms

Counts
1 → 1
Calls
1 calls:
Slowest
14.0ms
(/ (+ (* a c) (* b d)) (+ (* c c) (* d d)))

prune20.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 25.3b

localize37.0ms

Local error

Found 2 expressions with local error:

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

rewrite5.0ms

Algorithm
rewrite-expression-head
Rules
10×add-cube-cbrt
10×*-un-lft-identity
10×add-sqr-sqrt
times-frac
add-exp-log
add-cbrt-cube
associate-/l*
associate-/r*
add-log-exp
log1p-expm1-u
pow1
expm1-log1p-u
div-inv
fma-udef
div-exp
frac-2neg
clear-num
cbrt-undiv
Counts
2 → 39
Calls
2 calls:
Slowest
4.0ms
(/ (fma a c (* b d)) (fma d d (* c c)))
0.0ms
(fma d d (* c c))

series72.0ms

Counts
2 → 6
Calls
2 calls:
Slowest
54.0ms
(/ (fma a c (* b d)) (fma d d (* c c)))
18.0ms
(fma d d (* c c))

simplify614.0ms

Counts
31 → 45
Calls
31 calls:
Slowest
194.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))))
31.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)))))
24.0ms
(/ (sqrt (fma a c (* b d))) (* (cbrt (fma d d (* c c))) (cbrt (fma d d (* c c)))))
23.0ms
(/ (* (cbrt (fma a c (* b d))) (cbrt (fma a c (* b d)))) 1)
21.0ms
(/ (sqrt (fma a c (* b d))) (sqrt (fma d d (* c c))))

prune495.0ms

Pruning

10 alts after pruning (9 fresh and 1 done)

Merged error: 16.0b

localize21.0ms

Local error

Found 4 expressions with local error:

15.8b
(/ (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
(fma d b (* a c))

rewrite14.0ms

Algorithm
rewrite-expression-head
Rules
21×add-cube-cbrt
21×*-un-lft-identity
21×add-sqr-sqrt
18×times-frac
17×add-exp-log
13×associate-*r*
11×add-cbrt-cube
pow1
associate-/r*
add-log-exp
div-inv
log1p-expm1-u
prod-exp
associate-*l*
expm1-log1p-u
associate-/l*
div-exp
rec-exp
cbrt-unprod
frac-2neg
clear-num
cbrt-undiv
inv-pow
pow-flip
fma-udef
*-commutative
associate-*r/
associate-*l/
pow-prod-down
frac-times
Counts
4 → 94
Calls
4 calls:
Slowest
6.0ms
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
4.0ms
(/ 1 (hypot d c))
3.0ms
(/ (fma d b (* a c)) (hypot d c))
0.0ms
(fma d b (* a c))

series301.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
126.0ms
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
97.0ms
(/ (fma d b (* a c)) (hypot d c))
45.0ms
(fma d b (* a c))
33.0ms
(/ 1 (hypot d c))

simplify2.1s

Counts
67 → 106
Calls
67 calls:
Slowest
578.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))))
371.0ms
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
147.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)))
121.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))))
109.0ms
(* 1 (/ (fma d b (* a c)) (hypot d c)))

prune1.2s

Pruning

10 alts after pruning (10 fresh and 0 done)

Merged error: 6.3b

localize9.0ms

Local error

Found 4 expressions with local error:

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

rewrite11.0ms

Algorithm
rewrite-expression-head
Rules
84×times-frac
54×add-cube-cbrt
54×*-un-lft-identity
54×add-sqr-sqrt
16×associate-/l*
11×add-exp-log
11×add-cbrt-cube
div-inv
associate-/r*
add-log-exp
log1p-expm1-u
div-exp
cbrt-undiv
pow1
expm1-log1p-u
frac-2neg
clear-num
hypot-udef
associate-/l/
Counts
4 → 121
Calls
4 calls:
Slowest
6.0ms
(/ (/ (fma c a (* b d)) (hypot d c)) (hypot d c))
3.0ms
(/ (fma c a (* b d)) (hypot d c))
0.0ms
(hypot d c)
0.0ms
(hypot d c)

series268.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
108.0ms
(/ (/ (fma c a (* b d)) (hypot d c)) (hypot d c))
81.0ms
(/ (fma c a (* b d)) (hypot d c))
40.0ms
(hypot d c)
39.0ms
(hypot d c)

simplify3.1s

Counts
135 → 133
Calls
135 calls:
Slowest
352.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)))
224.0ms
(/ (/ (* (cbrt (fma c a (* b d))) (cbrt (fma c a (* b d)))) (* (cbrt (hypot d c)) (cbrt (hypot d c)))) 1)
212.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)))
161.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)))
152.0ms
(/ (/ (* (cbrt (fma c a (* b d))) (cbrt (fma c a (* b d)))) 1) (* (cbrt (hypot d c)) (cbrt (hypot d c))))

prune1.6s

Pruning

9 alts after pruning (9 fresh and 0 done)

Merged error: 2.4b

localize6.0ms

Local error

Found 4 expressions with local error:

15.9b
(* (/ 1 (hypot d c)) (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))
0.0b
(fma d b (* a c))

rewrite16.0ms

Algorithm
rewrite-expression-head
Rules
17×add-exp-log
11×add-cbrt-cube
add-cube-cbrt
*-un-lft-identity
add-sqr-sqrt
pow1
associate-/r*
add-log-exp
log1p-expm1-u
prod-exp
associate-*l*
expm1-log1p-u
div-inv
fma-udef
associate-*r*
div-exp
times-frac
rec-exp
cbrt-unprod
associate-*l/
frac-2neg
clear-num
cbrt-undiv
associate-/l/
inv-pow
associate-/l*
pow-flip
*-commutative
pow-prod-down
distribute-lft-in
distribute-rgt-in
Counts
4 → 77
Calls
4 calls:
Slowest
9.0ms
(/ (* (/ 1 (hypot d c)) (fma d b (* a c))) (hypot d c))
5.0ms
(* (/ 1 (hypot d c)) (fma d b (* a c)))
1.0ms
(/ 1 (hypot d c))
0.0ms
(fma d b (* a c))

series252.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
101.0ms
(/ (* (/ 1 (hypot d c)) (fma d b (* a c))) (hypot d c))
84.0ms
(* (/ 1 (hypot d c)) (fma d b (* a c)))
41.0ms
(fma d b (* a c))
26.0ms
(/ 1 (hypot d c))

simplify1.9s

Counts
44 → 89
Calls
44 calls:
Slowest
791.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))))
197.0ms
(- (log (* (/ 1 (hypot d c)) (fma d b (* a c)))) (log (hypot d c)))
167.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)))
142.0ms
(* (/ 1 (hypot d c)) (fma d b (* a c)))
119.0ms
(/ (* (* (* (/ 1 (hypot d c)) (fma d b (* a c))) (* (/ 1 (hypot d c)) (fma d b (* a c)))) (* (/ 1 (hypot d c)) (fma d b (* a c)))) (* (* (hypot d c) (hypot d c)) (hypot d c)))

prune1.1s

Pruning

9 alts after pruning (8 fresh and 1 done)

Merged error: 2.4b

regimes378.0ms

Accuracy

21.6% (10.9b remaining)

Error of 13.1b against oracle of 2.2b and baseline of 16.1b

bsearch121.0ms

end0.0ms

sample1.4s

Algorithm
intervals