Average Error: 37.8 → 13.4
Time: 14.2s
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 -79426894.0267396:\\ \;\;\;\;\frac{\sqrt{\left(im \cdot 2.0\right) \cdot im}}{\sqrt{\sqrt{im \cdot im + re \cdot re} - re}} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(2.0 \cdot re\right))_*} \cdot 0.5\\ \end{array}\]

Error

Bits error versus re

Bits error versus im

Target

Original37.8
Target33.1
Herbie13.4
\[\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 < -79426894.0267396

    1. Initial program 55.9

      \[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
    2. Using strategy rm
    3. Applied flip-+55.9

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \color{blue}{\frac{\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im} - re \cdot re}{\sqrt{re \cdot re + im \cdot im} - re}}}\]
    4. Applied associate-*r/55.9

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{\frac{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im} - re \cdot re\right)}{\sqrt{re \cdot re + im \cdot im} - re}}}\]
    5. Applied sqrt-div55.9

      \[\leadsto 0.5 \cdot \color{blue}{\frac{\sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im} - re \cdot re\right)}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}}\]
    6. Simplified38.9

      \[\leadsto 0.5 \cdot \frac{\color{blue}{\sqrt{\left(im \cdot 2.0\right) \cdot im}}}{\sqrt{\sqrt{re \cdot re + im \cdot im} - re}}\]

    if -79426894.0267396 < re

    1. Initial program 31.9

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le -79426894.0267396:\\ \;\;\;\;\frac{\sqrt{\left(im \cdot 2.0\right) \cdot im}}{\sqrt{\sqrt{im \cdot im + re \cdot re} - re}} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(2.0 \cdot re\right))_*} \cdot 0.5\\ \end{array}\]

Reproduce

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

sample48.0ms

Algorithm
intervals

simplify25.0ms

Counts
1 → 1
Calls

1 calls. Slowest were:

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

prune13.0ms

Pruning

2 alts after pruning (2 fresh and 0 done)

Merged error: 14.6b

localize15.0ms

Local error

Found 2 expressions with local error:

0.5b
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
0.0b
(fma (hypot re im) 2.0 (* re 2.0))

rewrite1.0ms

Algorithm
rewrite-expression-head
Counts
2 → 24
Calls

2 calls. Slowest were:

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

series156.0ms

Counts
2 → 6
Calls

2 calls. Slowest were:

104.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
52.0ms
(fma (hypot re im) 2.0 (* re 2.0))

simplify69.0ms

Counts
10 → 30
Calls

10 calls. Slowest were:

18.0ms
(sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))
17.0ms
(sqrt (* (cbrt (fma (hypot re im) 2.0 (* re 2.0))) (cbrt (fma (hypot re im) 2.0 (* re 2.0)))))
15.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))

prune345.0ms

Pruning

5 alts after pruning (4 fresh and 1 done)

Merged error: 14.6b

localize16.0ms

Local error

Found 4 expressions with local error:

0.5b
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
0.5b
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
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.1b
(sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))

rewrite13.0ms

Algorithm
rewrite-expression-head
Counts
4 → 84
Calls

4 calls. Slowest were:

9.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 (sqrt (fma (hypot re im) 2.0 (* re 2.0))))

series583.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

161.0ms
(* (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))
150.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
140.0ms
(sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0))))
132.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))

simplify957.0ms

Counts
56 → 96
Calls

56 calls. Slowest were:

265.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))))))
106.0ms
(exp (* 1/4 (- (+ (log (/ -1 re)) (log 1.0)) (* 2 (log (/ -1 im))))))
79.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))))))

prune1.2s

Pruning

5 alts after pruning (3 fresh and 2 done)

Merged error: 14.6b

localize35.0ms

Local error

Found 4 expressions with local error:

0.9b
(cbrt (fma (hypot re im) 2.0 (* re 2.0)))
0.9b
(cbrt (fma (hypot re im) 2.0 (* 2.0 re)))
0.5b
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
0.3b
(* (sqrt (sqrt (cbrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))

rewrite13.0ms

Algorithm
rewrite-expression-head
Counts
4 → 76
Calls

4 calls. Slowest were:

7.0ms
(* (sqrt (sqrt (cbrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))
5.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
1.0ms
(cbrt (fma (hypot re im) 2.0 (* re 2.0)))

series584.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

157.0ms
(cbrt (fma (hypot re im) 2.0 (* re 2.0)))
150.0ms
(sqrt (fma (hypot re im) 2.0 (* re 2.0)))
148.0ms
(cbrt (fma (hypot re im) 2.0 (* 2.0 re)))
129.0ms
(* (sqrt (sqrt (cbrt (fma (hypot re im) 2.0 (* re 2.0))))) (sqrt (sqrt (fma (hypot re im) 2.0 (* re 2.0)))))

simplify1.5s

Counts
47 → 88
Calls

47 calls. Slowest were:

174.0ms
(+ (exp (* 1/3 (+ (log im) (log 2.0)))) (+ (* 0.3333333333333333 (* re (exp (* 1/3 (- (log 2.0) (* 2 (log im))))))) (* 0.05555555555555555 (* (pow re 2) (exp (* 1/3 (- (log 2.0) (* 5 (log im)))))))))
172.0ms
(+ (exp (* 1/3 (+ (log im) (log 2.0)))) (+ (* 0.3333333333333333 (* re (exp (* 1/3 (- (log 2.0) (* 2 (log im))))))) (* 0.05555555555555555 (* (pow re 2) (exp (* 1/3 (- (log 2.0) (* 5 (log im)))))))))
168.0ms
(+ (exp (* 1/3 (+ (log im) (log 2.0)))) (+ (* 0.3333333333333333 (* re (exp (* 1/3 (- (log 2.0) (* 2 (log im))))))) (* 0.05555555555555555 (* (pow re 2) (exp (* 1/3 (- (log 2.0) (* 5 (log im)))))))))

prune1.9s

Pruning

5 alts after pruning (2 fresh and 3 done)

Merged error: 14.6b

localize23.0ms

Local error

Found 4 expressions with local error:

31.1b
(sqrt (+ (* re re) (* im im)))
0.5b
(sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))
0.0b
(+ (sqrt (+ (* re re) (* im im))) re)
0.0b
(+ (* re re) (* im im))

rewrite15.0ms

Algorithm
rewrite-expression-head
Counts
4 → 62
Calls

4 calls. Slowest were:

9.0ms
(+ (sqrt (+ (* re re) (* im im))) re)
2.0ms
(+ (* re re) (* im im))
2.0ms
(sqrt (+ (* re re) (* im im)))

series184.0ms

Counts
4 → 12
Calls

4 calls. Slowest were:

115.0ms
(sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))
29.0ms
(+ (sqrt (+ (* re re) (* im im))) re)
20.0ms
(+ (* re re) (* im im))
20.0ms
(sqrt (+ (* re re) (* im im)))

simplify932.0ms

Counts
27 → 74
Calls

27 calls. Slowest were:

411.0ms
(sqrt (* 2.0 (- (* (sqrt (+ (* re re) (* im im))) (sqrt (+ (* re re) (* im im)))) (* re re))))
198.0ms
(sqrt (* 2.0 (+ (pow (sqrt (+ (* re re) (* im im))) 3) (pow re 3))))
178.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))

prune765.0ms

Pruning

8 alts after pruning (6 fresh and 2 done)

Merged error: 10.9b

regimes247.0ms

Accuracy

-4.9% (3.8b remaining)

Error of 13.4b against oracle of 9.5b and baseline of 13.2b

bsearch114.0ms

end0.0ms

sample3.2s

Algorithm
intervals