Average Error: 25.7 → 12.7
Time: 22.4s
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.3845764181634926 \cdot 10^{+184}:\\ \;\;\;\;\frac{-a}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;c \le 3.0299220901443166 \cdot 10^{+165}:\\ \;\;\;\;\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
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 < -1.3845764181634926e+184

    1. Initial program 43.8

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

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

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

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

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

      \[\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.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/30.4

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

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

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

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

    if -1.3845764181634926e+184 < c < 3.0299220901443166e+165

    1. Initial program 20.3

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

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

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

    if 3.0299220901443166e+165 < c

    1. Initial program 43.2

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

      \[\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.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-identity43.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-frac43.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. Simplified43.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. Simplified29.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/29.4

      \[\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. Simplified29.4

      \[\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-identity29.4

      \[\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*29.4

      \[\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}^*}\]
    15. Taylor expanded around 0 13.1

      \[\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 -1.3845764181634926 \cdot 10^{+184}:\\ \;\;\;\;\frac{-a}{\sqrt{d^2 + c^2}^*}\\ \mathbf{elif}\;c \le 3.0299220901443166 \cdot 10^{+165}:\\ \;\;\;\;\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 2019016 +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: 20.6s)Debug log

sample50.0ms

Algorithm
intervals
Results
272×(pre true 80)
272×(body real 80)

simplify9.0ms

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

prune22.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 23.8b

localize36.0ms

Local error

Found 3 expressions with local error:

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

rewrite9.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
7.0ms
(/ (fma a c (* b d)) (fma d d (* c c)))
0.0ms
(fma d d (* c c))
0.0ms
(fma a c (* b d))

series163.0ms

Counts
3 → 9
Calls
3 calls:
Slowest
112.0ms
(/ (fma a c (* b d)) (fma d d (* c c)))
27.0ms
(fma a c (* b d))
23.0ms
(fma d d (* c c))

simplify626.0ms

Counts
34 → 58
Calls
34 calls:
Slowest
227.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))))
42.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)))))
31.0ms
(/ (* (cbrt (fma a c (* b d))) (cbrt (fma a c (* b d)))) 1)
24.0ms
(/ (cbrt (fma a c (* b d))) (sqrt (fma d d (* c c))))
22.0ms
(/ (sqrt (fma a c (* b d))) (sqrt (fma d d (* c c))))

prune633.0ms

Pruning

8 alts after pruning (7 fresh and 1 done)

Merged error: 13.9b

localize17.0ms

Local error

Found 4 expressions with local error:

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

rewrite12.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)))
3.0ms
(/ (fma d b (* a c)) (hypot d c))
1.0ms
(/ 1 (hypot d c))
0.0ms
(fma d b (* a c))

series277.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
132.0ms
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
78.0ms
(/ (fma d b (* a c)) (hypot d c))
40.0ms
(/ 1 (hypot d c))
26.0ms
(fma d b (* a c))

simplify2.2s

Counts
67 → 106
Calls
67 calls:
Slowest
699.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))))
299.0ms
(* (/ 1 (hypot d c)) (/ (fma d b (* a c)) (hypot d c)))
186.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))))
181.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)))
106.0ms
(+ (log (/ 1 (hypot d c))) (log (/ (fma d b (* a c)) (hypot d c))))

prune1.4s

Pruning

12 alts after pruning (12 fresh and 0 done)

Merged error: 6.1b

localize12.0ms

Local error

Found 3 expressions with local error:

13.7b
(/ (fma c a (* b d)) (hypot d c))
0.0b
(/ (/ (fma c a (* b d)) (hypot d c)) (hypot d c))
0.0b
(fma c a (* b d))

rewrite18.0ms

Algorithm
rewrite-expression-head
Rules
84×times-frac
53×add-cube-cbrt
53×*-un-lft-identity
53×add-sqr-sqrt
16×associate-/l*
10×add-exp-log
10×add-cbrt-cube
div-inv
associate-/r*
div-exp
cbrt-undiv
add-log-exp
log1p-expm1-u
pow1
expm1-log1p-u
frac-2neg
clear-num
associate-/l/
fma-udef
Counts
3 → 111
Calls
3 calls:
Slowest
11.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))

series181.0ms

Counts
3 → 9
Calls
3 calls:
Slowest
84.0ms
(/ (fma c a (* b d)) (hypot d c))
67.0ms
(/ (/ (fma c a (* b d)) (hypot d c)) (hypot d c))
30.0ms
(fma c a (* b d))

simplify3.3s

Counts
132 → 120
Calls
132 calls:
Slowest
374.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)))
270.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)))
208.0ms
(/ (/ (* (cbrt (fma c a (* b d))) (cbrt (fma c a (* b d)))) (* (cbrt (hypot d c)) (cbrt (hypot d c)))) 1)
182.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)))
149.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

12 alts after pruning (11 fresh and 1 done)

Merged error: 1.7b

localize8.0ms

Local error

Found 4 expressions with local error:

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

rewrite21.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))
7.0ms
(/ (hypot d c) (fma c a (* b d)))
3.0ms
(/ 1 (/ (hypot d c) (fma c a (* b d))))
0.0ms
(fma c a (* b d))

series356.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
127.0ms
(/ (/ 1 (/ (hypot d c) (fma c a (* b d)))) (hypot d c))
111.0ms
(/ 1 (/ (hypot d c) (fma c a (* b d))))
73.0ms
(/ (hypot d c) (fma c a (* b d)))
46.0ms
(fma c a (* b d))

simplify6.1s

Counts
92 → 121
Calls
92 calls:
Slowest
517.0ms
(/ (/ 1 (/ (hypot d c) (fma c a (* b d)))) (cbrt (hypot d c)))
494.0ms
(/ (sqrt (/ 1 (/ (hypot d c) (fma c a (* b d))))) (cbrt (hypot d c)))
416.0ms
(/ (/ 1 (/ (hypot d c) (fma c a (* b d)))) (sqrt (hypot d c)))
411.0ms
(/ (/ 1 (/ (hypot d c) (fma c a (* b d)))) (sqrt (hypot d c)))
383.0ms
(/ (/ 1 (/ (hypot d c) (fma c a (* b d)))) (cbrt (hypot d c)))

prune1.5s

Pruning

12 alts after pruning (11 fresh and 1 done)

Merged error: 1.7b

regimes434.0ms

Accuracy

27.9% (10.2b remaining)

Error of 12.7b against oracle of 2.5b and baseline of 16.6b

bsearch150.0ms

end0.0ms

sample1.5s

Algorithm
intervals
Results
8493×(pre true 80)
8493×(body real 80)