Average Error: 26.1 → 12.7
Time: 21.5s
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 -4.001353954028944 \cdot 10^{+116}:\\ \;\;\;\;\frac{-a}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;c \le 1.6521411071335998 \cdot 10^{+156}:\\ \;\;\;\;\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

Original26.1
Target0.5
Herbie12.7
\[\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 < -4.001353954028944e+116

    1. Initial program 40.7

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

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

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

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

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

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

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

      \[\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. Simplified26.9

      \[\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 15.6

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

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

    if -4.001353954028944e+116 < c < 1.6521411071335998e+156

    1. Initial program 19.3

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

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

      \[\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. Simplified19.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.9

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

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

      \[\leadsto \frac{\color{blue}{\frac{(c \cdot a + \left(b \cdot d\right))_*}{\sqrt{d^2 + c^2}^*}}}{\sqrt{d^2 + c^2}^*}\]
    12. Using strategy rm
    13. Applied *-un-lft-identity11.8

      \[\leadsto \frac{\frac{(c \cdot a + \left(b \cdot d\right))_*}{\color{blue}{1 \cdot \sqrt{d^2 + c^2}^*}}}{\sqrt{d^2 + c^2}^*}\]
    14. Applied associate-/r*11.8

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

    if 1.6521411071335998e+156 < c

    1. Initial program 43.9

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Initial simplification43.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-sqrt43.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-identity43.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-frac43.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. Simplified43.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-*l/28.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. Simplified28.2

      \[\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 13.8

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;c \le -4.001353954028944 \cdot 10^{+116}:\\ \;\;\;\;\frac{-a}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;c \le 1.6521411071335998 \cdot 10^{+156}:\\ \;\;\;\;\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 2018365 +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: 17.5s)Debug log

start78.0ms

Algorithm
intervals

setup54.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 24.9b

localize33.0ms

Local error

Found 1 expressions with local error:

24.9b
(/ (fma a c (* b d)) (fma d d (* c c)))

rewrite7.0ms

Algorithm
rewrite-expression-head
Counts
1 → 29
Calls

1 calls. Slowest were:

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

series76.0ms

Counts
1 → 3
Calls

1 calls. Slowest were:

76.0ms
(/ (fma a c (* b d)) (fma d d (* c c)))

simplify624.0ms

Counts
28 → 32
Calls

28 calls. Slowest were:

284.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))))
28.0ms
(- (log (fma a c (* b d))) (log (fma d d (* c c))))
25.0ms
(/ (* (cbrt (fma a c (* b d))) (cbrt (fma a c (* b d)))) (sqrt (fma d d (* c c))))

prune504.0ms

Pruning

8 alts after pruning (8 fresh and 0 done)

Merged error: 16.8b

localize39.0ms

Local error

Found 4 expressions with local error:

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

rewrite22.0ms

Algorithm
rewrite-expression-head
Counts
4 → 94
Calls

4 calls. Slowest were:

12.0ms
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
7.0ms
(/ (fma d b (* a c)) (hypot d c))
2.0ms
(/ 1 (hypot d c))

series250.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

119.0ms
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
69.0ms
(/ (fma d b (* a c)) (hypot d c))
33.0ms
(/ 1 (hypot d c))
28.0ms
(fma d b (* a c))

simplify2.2s

Counts
67 → 106
Calls

67 calls. Slowest were:

697.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))))
262.0ms
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
178.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.5s

Pruning

11 alts after pruning (11 fresh and 0 done)

Merged error: 6.7b

localize21.0ms

Local error

Found 2 expressions with local error:

16.5b
(/ (fma c a (* b d)) (hypot d c))
0.1b
(/ (/ (fma c a (* b d)) (hypot d c)) (hypot d c))

rewrite22.0ms

Algorithm
rewrite-expression-head
Counts
2 → 101
Calls

2 calls. Slowest were:

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

series154.0ms

Counts
2 → 6
Calls

2 calls. Slowest were:

80.0ms
(/ (/ (fma c a (* b d)) (hypot d c)) (hypot d c))
74.0ms
(/ (fma c a (* b d)) (hypot d c))

simplify3.1s

Counts
129 → 107
Calls

129 calls. Slowest were:

382.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)))
232.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)))
226.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

10 alts after pruning (10 fresh and 0 done)

Merged error: 3.0b

localize12.0ms

Local error

Found 2 expressions with local error:

16.5b
(/ (/ (fma c a (* b d)) 1) (hypot d c))
0.1b
(/ (/ (/ (fma c a (* b d)) 1) (hypot d c)) (hypot d c))

rewrite15.0ms

Algorithm
rewrite-expression-head
Counts
2 → 118
Calls

2 calls. Slowest were:

8.0ms
(/ (/ (/ (fma c a (* b d)) 1) (hypot d c)) (hypot d c))
5.0ms
(/ (/ (fma c a (* b d)) 1) (hypot d c))

series165.0ms

Counts
2 → 6
Calls

2 calls. Slowest were:

99.0ms
(/ (/ (fma c a (* b d)) 1) (hypot d c))
66.0ms
(/ (/ (/ (fma c a (* b d)) 1) (hypot d c)) (hypot d c))

simplify4.8s

Counts
158 → 124
Calls

158 calls. Slowest were:

898.0ms
(/ (* (* (/ (/ (fma c a (* b d)) 1) (hypot d c)) (/ (/ (fma c a (* b d)) 1) (hypot d c))) (/ (/ (fma c a (* b d)) 1) (hypot d c))) (* (* (hypot d c) (hypot d c)) (hypot d c)))
394.0ms
(/ (* (* (/ (fma c a (* b d)) 1) (/ (fma c a (* b d)) 1)) (/ (fma c a (* b d)) 1)) (* (* (hypot d c) (hypot d c)) (hypot d c)))
258.0ms
(/ (/ (* (cbrt (/ (fma c a (* b d)) 1)) (cbrt (/ (fma c a (* b d)) 1))) (* (cbrt (hypot d c)) (cbrt (hypot d c)))) 1)

prune1.7s

Pruning

10 alts after pruning (9 fresh and 1 done)

Merged error: 3.0b

regimes434.0ms

Accuracy

26.6% (10.4b remaining)

Error of 12.7b against oracle of 2.3b and baseline of 16.5b

bsearch95.0ms