Average Error: 37.5 → 13.0
Time: 15.4s
Precision: 64
Internal Precision: 128
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
\[\begin{array}{l} \mathbf{if}\;re \le -4.834562283134786 \cdot 10^{+206}:\\ \;\;\;\;0.5 \cdot {\left(e^{(\left(\log 1.0 + \log \left(\frac{-1}{re}\right)\right) \cdot \frac{1}{8} + \left(\log \left(\frac{-1}{im}\right) \cdot \frac{-1}{4}\right))_*}\right)}^{4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}\\ \end{array}\]

Error

Bits error versus re

Bits error versus im

Target

Original37.5
Target32.7
Herbie13.0
\[\begin{array}{l} \mathbf{if}\;re \lt 0:\\ \;\;\;\;0.5 \cdot \left(\sqrt{2} \cdot \sqrt{\frac{im \cdot im}{\sqrt{re \cdot re + im \cdot im} - re}}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\\ \end{array}\]

Derivation

  1. Split input into 2 regimes
  2. if re < -4.834562283134786e+206

    1. Initial program 62.3

      \[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
    2. Simplified43.7

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt43.7

      \[\leadsto 0.5 \cdot \color{blue}{\left(\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}} \cdot \sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}\right)}\]
    5. Using strategy rm
    6. Applied add-sqr-sqrt43.7

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}} \cdot \sqrt{\color{blue}{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}} \cdot \sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}}}\right)\]
    7. Applied sqrt-prod43.7

      \[\leadsto 0.5 \cdot \left(\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}} \cdot \color{blue}{\left(\sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}} \cdot \sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}}\right)}\right)\]
    8. Applied associate-*r*43.7

      \[\leadsto 0.5 \cdot \color{blue}{\left(\left(\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}} \cdot \sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}}\right) \cdot \sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}}\right)}\]
    9. Using strategy rm
    10. Applied pow143.7

      \[\leadsto 0.5 \cdot \left(\left(\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}} \cdot \sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}}\right) \cdot \color{blue}{{\left(\sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}}\right)}^{1}}\right)\]
    11. Applied add-sqr-sqrt43.7

      \[\leadsto 0.5 \cdot \left(\left(\color{blue}{\left(\sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}} \cdot \sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}}\right)} \cdot \sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}}\right) \cdot {\left(\sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}}\right)}^{1}\right)\]
    12. Applied pow343.7

      \[\leadsto 0.5 \cdot \left(\color{blue}{{\left(\sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}}\right)}^{3}} \cdot {\left(\sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}}\right)}^{1}\right)\]
    13. Applied pow-prod-up43.7

      \[\leadsto 0.5 \cdot \color{blue}{{\left(\sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}}\right)}^{\left(3 + 1\right)}}\]
    14. Simplified43.7

      \[\leadsto 0.5 \cdot {\left(\sqrt{\sqrt{\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}}\right)}^{\color{blue}{4}}\]
    15. Taylor expanded around -inf 37.0

      \[\leadsto 0.5 \cdot \color{blue}{{\left(e^{\frac{1}{8} \cdot \left(\left(\log \left(\frac{-1}{re}\right) + \log 1.0\right) - 2 \cdot \log \left(\frac{-1}{im}\right)\right)}\right)}^{4}}\]
    16. Simplified37.0

      \[\leadsto 0.5 \cdot \color{blue}{{\left(e^{(\left(\log 1.0 + \log \left(\frac{-1}{re}\right)\right) \cdot \frac{1}{8} + \left(\frac{-1}{4} \cdot \log \left(\frac{-1}{im}\right)\right))_*}\right)}^{4}}\]

    if -4.834562283134786e+206 < re

    1. Initial program 35.3

      \[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
    2. Simplified10.8

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}\]
  3. Recombined 2 regimes into one program.
  4. Final simplification13.0

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le -4.834562283134786 \cdot 10^{+206}:\\ \;\;\;\;0.5 \cdot {\left(e^{(\left(\log 1.0 + \log \left(\frac{-1}{re}\right)\right) \cdot \frac{1}{8} + \left(\log \left(\frac{-1}{im}\right) \cdot \frac{-1}{4}\right))_*}\right)}^{4}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}\\ \end{array}\]

Reproduce

herbie shell --seed 2019002 +o rules:numerics
(FPCore (re im)
  :name "math.sqrt on complex, real part"

  :herbie-target
  (if (< re 0) (* 0.5 (* (sqrt 2) (sqrt (/ (* im im) (- (sqrt (+ (* re re) (* im im))) re))))) (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))

  (* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))))

Details

Time bar (total: 13.4s)Debug log

sample96.0ms

Algorithm
intervals

simplify24.0ms

Counts
1 → 1
Calls

1 calls. Slowest were:

24.0ms
(* 0.5 (sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re))))

prune25.0ms

Pruning

2 alts after pruning (2 fresh and 0 done)

Merged error: 14.0b

localize28.0ms

Local error

Found 3 expressions with local error:

0.2b
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
0.1b
(fma (hypot re im) 2.0 (* re 2.0))
0.0b
(hypot re im)

rewrite3.0ms

Algorithm
rewrite-expression-head
Counts
3 → 34
Calls

3 calls. Slowest were:

2.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
0.0ms
(fma (hypot re im) 2.0 (* re 2.0))
0.0ms
(hypot re im)

series236.0ms

Counts
3 → 9
Calls

3 calls. Slowest were:

171.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
33.0ms
(fma (hypot re im) 2.0 (* re 2.0))
32.0ms
(hypot re im)

simplify53.0ms

Counts
13 → 43
Calls

13 calls. Slowest were:

15.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
14.0ms
(sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))
10.0ms
(sqrt (* (cbrt (fma (hypot re im) 2.0 (* re 2.0))) (cbrt (fma (hypot re im) 2.0 (* re 2.0)))))

prune438.0ms

Pruning

10 alts after pruning (9 fresh and 1 done)

Merged error: 13.8b

localize9.0ms

Local error

Found 4 expressions with local error:

0.4b
(* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))
0.2b
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
0.2b
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
0.1b
(fma (hypot re im) 2.0 (* re 2.0))

rewrite9.0ms

Algorithm
rewrite-expression-head
Counts
4 → 76
Calls

4 calls. Slowest were:

6.0ms
(* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))
1.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
1.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))

series460.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

136.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
134.0ms
(* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))
133.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
57.0ms
(fma (hypot re im) 2.0 (* re 2.0))

simplify560.0ms

Counts
48 → 88
Calls

48 calls. Slowest were:

90.0ms
(* (* (* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (* (* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))
21.0ms
(+ (log (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (log (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))
20.0ms
(sqrt (sqrt (* (cbrt (fma (hypot re im) 2.0 (* re 2.0))) (cbrt (fma (hypot re im) 2.0 (* re 2.0))))))

prune1.2s

Pruning

10 alts after pruning (9 fresh and 1 done)

Merged error: 13.8b

localize13.0ms

Local error

Found 4 expressions with local error:

0.3b
(* (* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))
0.3b
(* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))
0.2b
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
0.2b
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))

rewrite48.0ms

Algorithm
rewrite-expression-head
Counts
4 → 106
Calls

4 calls. Slowest were:

34.0ms
(* (* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))
12.0ms
(* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))
1.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))

series709.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

284.0ms
(* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))
167.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
136.0ms
(* (* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))
121.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))

simplify2.1s

Counts
82 → 118
Calls

82 calls. Slowest were:

283.0ms
(exp (* 1/8 (- (+ (* 3 (log (/ -1 re))) (log 1.0)) (* 6 (log (/ -1 im))))))
210.0ms
(exp (* 1/8 (- (log 64.0) (* 3 (log (/ 1 re))))))
166.0ms
(* (* (* (* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))) (* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))) (* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))))) (* (* (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))) (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))))

prune1.6s

Pruning

11 alts after pruning (10 fresh and 1 done)

Merged error: 13.8b

localize7.0ms

Local error

Found 4 expressions with local error:

0.7b
(pow (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) 4)
0.2b
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
0.1b
(fma (hypot re im) 2.0 (* re 2.0))
0.1b
(sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))

rewrite5.0ms

Algorithm
rewrite-expression-head
Counts
4 → 67
Calls

4 calls. Slowest were:

2.0ms
(pow (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) 4)
1.0ms
(sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))
1.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))

series572.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

179.0ms
(pow (sqrt (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))) 4)
176.0ms
(sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))
168.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
49.0ms
(fma (hypot re im) 2.0 (* re 2.0))

simplify993.0ms

Counts
39 → 79
Calls

39 calls. Slowest were:

257.0ms
(+ (* 0.03125 (* (pow re 2) (exp (* 1/4 (- (log 2.0) (* 7 (log im))))))) (+ (* 0.25 (* re (exp (* 1/4 (- (log 2.0) (* 3 (log im))))))) (exp (* 1/4 (+ (log im) (log 2.0))))))
120.0ms
(exp (* 1/4 (- (+ (log (/ -1 re)) (log 1.0)) (* 2 (log (/ -1 im))))))
113.0ms
(pow (exp (* 1/8 (- (+ (log (/ -1 re)) (log 1.0)) (* 2 (log (/ -1 im)))))) 4)

prune1.1s

Pruning

13 alts after pruning (11 fresh and 2 done)

Merged error: 8.5b

regimes252.0ms

Accuracy

9.3% (5.5b remaining)

Error of 13.0b against oracle of 7.5b and baseline of 13.6b

bsearch74.0ms

end0.0ms

sample2.8s

Algorithm
intervals