Average Error: 26.1 → 12.5
Time: 16.1s
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.8506614466369815 \cdot 10^{+150}:\\ \;\;\;\;\frac{-a}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;c \le 6.560820932670249 \cdot 10^{+185}:\\ \;\;\;\;\frac{\frac{1}{\frac{\sqrt{d^2 + c^2}^*}{(c \cdot a + \left(d \cdot b\right))_*}}}{\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.4
Herbie12.5
\[\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.8506614466369815e+150

    1. Initial program 44.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified44.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-sqrt44.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-identity44.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-frac44.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. Simplified44.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. 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.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. Simplified28.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 13.3

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

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

    if -1.8506614466369815e+150 < c < 6.560820932670249e+185

    1. Initial program 20.1

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified20.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-sqrt20.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-identity20.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-frac20.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. Simplified20.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. Simplified12.4

      \[\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/12.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. Simplified12.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. Using strategy rm
    13. Applied *-un-lft-identity12.2

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

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

    if 6.560820932670249e+185 < c

    1. Initial program 44.6

      \[\frac{a \cdot c + b \cdot d}{c \cdot c + d \cdot d}\]
    2. Simplified44.6

      \[\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-sqrt44.6

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

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

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

      \[\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. Simplified30.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-*l/30.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. Simplified30.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 inf 12.2

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

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

Reproduce

herbie shell --seed 2019022 +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.3s)Debug log

sample64.0ms

Algorithm
intervals
Results
49.0ms270×body80valid

simplify10.0ms

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

prune10.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 26.8b

localize16.0ms

Local error

Found 3 expressions with local error:

26.8b
(/ (fma a c (* b d)) (fma d d (* c c)))
0.3b
(fma a c (* b d))
0.0b
(fma d d (* c c))

rewrite4.0ms

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

series144.0ms

Counts
3 → 9
Calls
3 calls:
Slowest
80.0ms
(/ (fma a c (* b d)) (fma d d (* c c)))
39.0ms
(fma a c (* b d))
26.0ms
(fma d d (* c c))

simplify471.0ms

Counts
34 → 58
Calls
34 calls:
Slowest
203.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))))
33.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)))))
15.0ms
(/ (sqrt (fma a c (* b d))) (cbrt (fma d d (* c c))))
15.0ms
(/ (sqrt (fma a c (* b d))) (sqrt (fma d d (* c c))))
15.0ms
(/ (sqrt (fma a c (* b d))) (sqrt (fma d d (* c c))))

prune616.0ms

Pruning

8 alts after pruning (7 fresh and 1 done)

Merged error: 16.4b

localize30.0ms

Local error

Found 4 expressions with local error:

16.2b
(/ (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
(hypot d c)

rewrite10.0ms

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

series275.0ms

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

simplify1.7s

Counts
63 → 98
Calls
63 calls:
Slowest
472.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))))
224.0ms
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
137.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)))
125.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))))
103.0ms
(+ (log (/ 1 (hypot d c))) (- (log (fma d b (* a c))) (log (hypot d c))))

prune1.1s

Pruning

12 alts after pruning (10 fresh and 2 done)

Merged error: 7.6b

localize13.0ms

Local error

Found 4 expressions with local error:

16.2b
(/ (fma c a (* b d)) (hypot d c))
0.3b
(fma c a (* b d))
0.1b
(/ (/ (fma c a (* b d)) (hypot d c)) (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
associate-/l/
fma-udef
hypot-udef
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
(fma c a (* b d))
0.0ms
(hypot d c)

series236.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
91.0ms
(/ (/ (fma c a (* b d)) (hypot d c)) (hypot d c))
80.0ms
(/ (fma c a (* b d)) (hypot d c))
38.0ms
(fma c a (* b d))
27.0ms
(hypot d c)

simplify2.5s

Counts
135 → 133
Calls
135 calls:
Slowest
287.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)))
190.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)))
150.0ms
(/ (/ (* (cbrt (fma c a (* b d))) (cbrt (fma c a (* b d)))) (* (cbrt (hypot d c)) (cbrt (hypot d c)))) 1)
148.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)))
126.0ms
(/ (/ (* (cbrt (fma c a (* b d))) (cbrt (fma c a (* b d)))) 1) (* (cbrt (hypot d c)) (cbrt (hypot d c))))

prune1.3s

Pruning

10 alts after pruning (8 fresh and 2 done)

Merged error: 2.5b

localize13.0ms

Local error

Found 4 expressions with local error:

16.3b
(/ (hypot d c) (fma c a (* b d)))
0.3b
(fma c a (* b d))
0.2b
(/ 1 (/ (hypot d c) (fma c a (* b d))))
0.1b
(/ (/ 1 (/ (hypot d c) (fma c a (* b d)))) (hypot d c))

rewrite18.0ms

Algorithm
rewrite-expression-head
Rules
33×times-frac
29×add-cube-cbrt
29×*-un-lft-identity
29×add-sqr-sqrt
19×associate-/r*
16×add-exp-log
associate-/l*
div-inv
add-cbrt-cube
div-exp
associate-/r/
pow1
add-log-exp
log1p-expm1-u
rec-exp
expm1-log1p-u
frac-2neg
clear-num
cbrt-undiv
associate-/l/
inv-pow
pow-flip
fma-udef
Counts
4 → 109
Calls
4 calls:
Slowest
8.0ms
(/ (/ 1 (/ (hypot d c) (fma c a (* b d)))) (hypot d c))
5.0ms
(/ (hypot d c) (fma c a (* b d)))
2.0ms
(/ 1 (/ (hypot d c) (fma c a (* b d))))
0.0ms
(fma c a (* b d))

series306.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
108.0ms
(/ (/ 1 (/ (hypot d c) (fma c a (* b d)))) (hypot d c))
91.0ms
(/ 1 (/ (hypot d c) (fma c a (* b d))))
78.0ms
(/ (hypot d c) (fma c a (* b d)))
29.0ms
(fma c a (* b d))

simplify4.3s

Counts
92 → 121
Calls
92 calls:
Slowest
365.0ms
(/ (/ 1 (/ (hypot d c) (fma c a (* b d)))) (sqrt (hypot d c)))
347.0ms
(/ (/ 1 (/ (hypot d c) (fma c a (* b d)))) (cbrt (hypot d c)))
341.0ms
(/ (/ 1 (/ (hypot d c) (fma c a (* b d)))) (sqrt (hypot d c)))
327.0ms
(/ (/ 1 (/ (hypot d c) (fma c a (* b d)))) (cbrt (hypot d c)))
257.0ms
(/ (/ 1 (/ (hypot d c) (fma c a (* b d)))) (hypot d c))

prune887.0ms

Pruning

10 alts after pruning (7 fresh and 3 done)

Merged error: 2.5b

regimes258.0ms

Accuracy

28.9% (10.0b remaining)

Error of 12.5b against oracle of 2.5b and baseline of 16.5b

bsearch85.0ms

end0.0ms

sample949.0ms

Algorithm
intervals
Results
795.0ms8447×body80valid