Average Error: 37.4 → 31.9
Time: 13.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}\;im \le -9.60061267431029 \cdot 10^{-07}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(\sqrt{im \cdot im + re \cdot re} - re\right)} \cdot 0.5\\ \mathbf{elif}\;im \le 4.408789558466924 \cdot 10^{-51}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(-2 \cdot re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(im - re\right) \cdot 2.0}\\ \end{array}\]

Error

Bits error versus re

Bits error versus im

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation

  1. Split input into 3 regimes
  2. if im < -9.60061267431029e-07

    1. Initial program 40.1

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

    if -9.60061267431029e-07 < im < 4.408789558466924e-51

    1. Initial program 35.7

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

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\color{blue}{\left(\sqrt[3]{\sqrt{re \cdot re + im \cdot im}} \cdot \sqrt[3]{\sqrt{re \cdot re + im \cdot im}}\right) \cdot \sqrt[3]{\sqrt{re \cdot re + im \cdot im}}} - re\right)}\]
    4. Taylor expanded around -inf 37.1

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

    if 4.408789558466924e-51 < im

    1. Initial program 37.7

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

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\color{blue}{\left(\sqrt[3]{\sqrt{re \cdot re + im \cdot im}} \cdot \sqrt[3]{\sqrt{re \cdot re + im \cdot im}}\right) \cdot \sqrt[3]{\sqrt{re \cdot re + im \cdot im}}} - re\right)}\]
    4. Taylor expanded around 0 16.1

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \color{blue}{\left(im - re\right)}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification31.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \le -9.60061267431029 \cdot 10^{-07}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(\sqrt{im \cdot im + re \cdot re} - re\right)} \cdot 0.5\\ \mathbf{elif}\;im \le 4.408789558466924 \cdot 10^{-51}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(-2 \cdot re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{\left(im - re\right) \cdot 2.0}\\ \end{array}\]

Reproduce

herbie shell --seed 2019016 
(FPCore (re im)
  :name "math.sqrt on complex, imaginary part, im greater than 0 branch"
  (* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))))

Details

Time bar (total: 11.9s)Debug log

sample191.0ms

Algorithm
intervals
Results
256×(pre true 80)
208×(body real 80)
22×(body real 2560)
11×(body real 1280)
(body real 320)
(body real 640)
(body real 5120)
(body real 160)

simplify40.0ms

Counts
1 → 1
Calls
1 calls:
Slowest
40.0ms
(* 0.5 (sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re))))

prune8.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 38.8b

localize51.0ms

Local error

Found 2 expressions with local error:

31.4b
(sqrt (+ (* re re) (* im im)))
0.5b
(- (sqrt (+ (* re re) (* im im))) re)

rewrite21.0ms

Algorithm
rewrite-expression-head
Rules
add-sqr-sqrt
sqrt-prod
add-cube-cbrt
*-un-lft-identity
add-log-exp
add-exp-log
sqrt-div
add-cbrt-cube
pow1
flip--
flip-+
pow1/2
flip3--
flip3-+
sub-neg
rem-sqrt-square
Counts
2 → 25
Calls
2 calls:
Slowest
12.0ms
(- (sqrt (+ (* re re) (* im im))) re)
7.0ms
(sqrt (+ (* re re) (* im im)))

series64.0ms

Counts
2 → 6
Calls
2 calls:
Slowest
34.0ms
(- (sqrt (+ (* re re) (* im im))) re)
30.0ms
(sqrt (+ (* re re) (* im im)))

simplify234.0ms

Counts
12 → 31
Calls
12 calls:
Slowest
167.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
36.0ms
(sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))
6.0ms
(sqrt (* (cbrt (+ (* re re) (* im im))) (cbrt (+ (* re re) (* im im)))))
5.0ms
(sqrt (sqrt (+ (* re re) (* im im))))
4.0ms
(- im re)

prune380.0ms

Pruning

9 alts after pruning (8 fresh and 1 done)

Merged error: 19.5b

localize44.0ms

Local error

Found 4 expressions with local error:

32.5b
(cbrt (+ (* re re) (* im im)))
32.5b
(cbrt (+ (* re re) (* im im)))
4.6b
(- (* (fabs (cbrt (+ (* re re) (* im im)))) (sqrt (cbrt (+ (* re re) (* im im))))) re)
0.3b
(* (fabs (cbrt (+ (* re re) (* im im)))) (sqrt (cbrt (+ (* re re) (* im im)))))

rewrite34.0ms

Algorithm
rewrite-expression-head
Rules
10×add-cube-cbrt
10×*-un-lft-identity
10×add-sqr-sqrt
associate-*r*
cbrt-prod
sqrt-prod
add-exp-log
add-cbrt-cube
cbrt-div
pow1
add-log-exp
flip-+
associate-*l*
flip3-+
associate-*r/
sqrt-div
pow1/3
flip--
cbrt-unprod
*-commutative
prod-exp
pow-prod-down
flip3--
sub-neg
Counts
4 → 62
Calls
4 calls:
Slowest
15.0ms
(- (* (fabs (cbrt (+ (* re re) (* im im)))) (sqrt (cbrt (+ (* re re) (* im im))))) re)
13.0ms
(* (fabs (cbrt (+ (* re re) (* im im)))) (sqrt (cbrt (+ (* re re) (* im im)))))
2.0ms
(cbrt (+ (* re re) (* im im)))
2.0ms
(cbrt (+ (* re re) (* im im)))

series474.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
142.0ms
(cbrt (+ (* re re) (* im im)))
132.0ms
(cbrt (+ (* re re) (* im im)))
107.0ms
(* (fabs (cbrt (+ (* re re) (* im im)))) (sqrt (cbrt (+ (* re re) (* im im)))))
93.0ms
(- (* (fabs (cbrt (+ (* re re) (* im im)))) (sqrt (cbrt (+ (* re re) (* im im))))) re)

simplify1.1s

Counts
38 → 74
Calls
38 calls:
Slowest
187.0ms
(- (+ (* (fabs (pow (+ (pow im 2) (pow re 2)) 1/3)) (pow im 1/3)) (* 1/6 (* (* (pow re 2) (fabs (pow (+ (pow im 2) (pow re 2)) 1/3))) (pow (/ 1 (pow im 5)) 1/3)))) re)
132.0ms
(cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
126.0ms
(sqrt (cbrt (+ (* (* re re) (* re re)) (- (* (* im im) (* im im)) (* (* re re) (* im im))))))
126.0ms
(+ (* (fabs (pow (+ (pow im 2) (pow re 2)) 1/3)) (pow im 1/3)) (* 1/6 (* (* (pow re 2) (fabs (pow (+ (pow im 2) (pow re 2)) 1/3))) (pow (/ 1 (pow im 5)) 1/3))))
126.0ms
(cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))

prune1.1s

Pruning

9 alts after pruning (8 fresh and 1 done)

Merged error: 19.5b

localize49.0ms

Local error

Found 4 expressions with local error:

32.5b
(cbrt (+ (* re re) (* im im)))
31.4b
(sqrt (+ (* re re) (* im im)))
31.4b
(sqrt (+ (* re re) (* im im)))
5.8b
(- (* (* (fabs (cbrt (+ (* re re) (* im im)))) (sqrt (cbrt (sqrt (+ (* re re) (* im im)))))) (sqrt (cbrt (sqrt (+ (* re re) (* im im)))))) re)

rewrite55.0ms

Algorithm
rewrite-expression-head
Rules
add-sqr-sqrt
add-cube-cbrt
*-un-lft-identity
sqrt-prod
add-log-exp
add-exp-log
sqrt-div
add-cbrt-cube
pow1
flip-+
flip3-+
cbrt-prod
pow1/2
cbrt-div
rem-sqrt-square
flip--
pow1/3
flip3--
sub-neg
Counts
4 → 52
Calls
4 calls:
Slowest
44.0ms
(- (* (* (fabs (cbrt (+ (* re re) (* im im)))) (sqrt (cbrt (sqrt (+ (* re re) (* im im)))))) (sqrt (cbrt (sqrt (+ (* re re) (* im im)))))) re)
4.0ms
(sqrt (+ (* re re) (* im im)))
4.0ms
(sqrt (+ (* re re) (* im im)))
3.0ms
(cbrt (+ (* re re) (* im im)))

series257.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
107.0ms
(cbrt (+ (* re re) (* im im)))
94.0ms
(- (* (* (fabs (cbrt (+ (* re re) (* im im)))) (sqrt (cbrt (sqrt (+ (* re re) (* im im)))))) (sqrt (cbrt (sqrt (+ (* re re) (* im im)))))) re)
28.0ms
(sqrt (+ (* re re) (* im im)))
27.0ms
(sqrt (+ (* re re) (* im im)))

simplify963.0ms

Counts
29 → 64
Calls
29 calls:
Slowest
220.0ms
(- (+ (* (fabs (pow (+ (pow im 2) (pow re 2)) 1/3)) (pow im 1/3)) (* 1/6 (* (* (pow re 2) (fabs (pow (+ (pow im 2) (pow re 2)) 1/3))) (pow (/ 1 (pow im 5)) 1/3)))) re)
160.0ms
(cbrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
149.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
126.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
62.0ms
(sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))

prune1.2s

Pruning

10 alts after pruning (9 fresh and 1 done)

Merged error: 19.5b

localize24.0ms

Local error

Found 4 expressions with local error:

31.4b
(sqrt (+ (* re re) (* im im)))
31.4b
(sqrt (+ (* re re) (* im im)))
31.4b
(sqrt (+ (* re re) (* im im)))
8.0b
(- (* (* (cbrt (sqrt (+ (* re re) (* im im)))) (cbrt (sqrt (+ (* re re) (* im im))))) (cbrt (sqrt (+ (* re re) (* im im))))) re)

rewrite47.0ms

Algorithm
rewrite-expression-head
Rules
10×add-sqr-sqrt
sqrt-prod
add-cube-cbrt
*-un-lft-identity
sqrt-div
add-log-exp
add-exp-log
add-cbrt-cube
pow1
flip-+
pow1/2
flip3-+
rem-sqrt-square
flip--
flip3--
sub-neg
Counts
4 → 53
Calls
4 calls:
Slowest
41.0ms
(- (* (* (cbrt (sqrt (+ (* re re) (* im im)))) (cbrt (sqrt (+ (* re re) (* im im))))) (cbrt (sqrt (+ (* re re) (* im im))))) re)
2.0ms
(sqrt (+ (* re re) (* im im)))
2.0ms
(sqrt (+ (* re re) (* im im)))
2.0ms
(sqrt (+ (* re re) (* im im)))

series104.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
35.0ms
(- (* (* (cbrt (sqrt (+ (* re re) (* im im)))) (cbrt (sqrt (+ (* re re) (* im im))))) (cbrt (sqrt (+ (* re re) (* im im))))) re)
24.0ms
(sqrt (+ (* re re) (* im im)))
22.0ms
(sqrt (+ (* re re) (* im im)))
22.0ms
(sqrt (+ (* re re) (* im im)))

simplify585.0ms

Counts
30 → 65
Calls
30 calls:
Slowest
145.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
139.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
133.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
47.0ms
(sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))
39.0ms
(sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))

prune913.0ms

Pruning

11 alts after pruning (10 fresh and 1 done)

Merged error: 19.5b

regimes1.0s

Accuracy

33.2% (11.2b remaining)

Error of 31.9b against oracle of 20.7b and baseline of 37.4b

bsearch145.0ms

end0.0ms

sample2.8s

Algorithm
intervals
Results
8000×(pre true 80)
6040×(body real 80)
680×(body real 2560)
480×(body real 1280)
281×(body real 640)
281×(body real 5120)
158×(body real 320)
80×(body real 160)