Average Error: 37.3 → 26.0
Time: 13.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 -1.98901646800801 \cdot 10^{+52}:\\ \;\;\;\;\sqrt{\left(-2 \cdot re\right) \cdot 2.0} \cdot 0.5\\ \mathbf{elif}\;re \le -2.453660426131194 \cdot 10^{-293}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(\left(\sqrt{\sqrt[3]{\sqrt{im \cdot im + re \cdot re}} \cdot \sqrt[3]{\sqrt{im \cdot im + re \cdot re}}} \cdot \sqrt{\sqrt{im \cdot im + re \cdot re}}\right) \cdot \sqrt{\sqrt[3]{\sqrt{im \cdot im + re \cdot re}}} - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{\left(2.0 \cdot im\right) \cdot im}}{\sqrt{\sqrt{im \cdot im + re \cdot re} + re}}\\ \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 re < -1.98901646800801e+52

    1. Initial program 42.1

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

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

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

    if -1.98901646800801e+52 < re < -2.453660426131194e-293

    1. Initial program 19.2

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

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\color{blue}{\sqrt{\sqrt{re \cdot re + im \cdot im}} \cdot \sqrt{\sqrt{re \cdot re + im \cdot im}}} - re\right)}\]
    4. Using strategy rm
    5. Applied add-cube-cbrt19.4

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{\sqrt{re \cdot re + im \cdot im}} \cdot \sqrt{\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)}\]
    6. Applied sqrt-prod19.4

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{\sqrt{re \cdot re + im \cdot im}} \cdot \color{blue}{\left(\sqrt{\sqrt[3]{\sqrt{re \cdot re + im \cdot im}} \cdot \sqrt[3]{\sqrt{re \cdot re + im \cdot im}}} \cdot \sqrt{\sqrt[3]{\sqrt{re \cdot re + im \cdot im}}}\right)} - re\right)}\]
    7. Applied associate-*r*19.4

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

    if -2.453660426131194e-293 < re

    1. Initial program 45.1

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

      \[\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/45.1

      \[\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-div45.2

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le -1.98901646800801 \cdot 10^{+52}:\\ \;\;\;\;\sqrt{\left(-2 \cdot re\right) \cdot 2.0} \cdot 0.5\\ \mathbf{elif}\;re \le -2.453660426131194 \cdot 10^{-293}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(\left(\sqrt{\sqrt[3]{\sqrt{im \cdot im + re \cdot re}} \cdot \sqrt[3]{\sqrt{im \cdot im + re \cdot re}}} \cdot \sqrt{\sqrt{im \cdot im + re \cdot re}}\right) \cdot \sqrt{\sqrt[3]{\sqrt{im \cdot im + re \cdot re}}} - re\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \frac{\sqrt{\left(2.0 \cdot im\right) \cdot im}}{\sqrt{\sqrt{im \cdot im + re \cdot re} + re}}\\ \end{array}\]

Reproduce

herbie shell --seed 2019021 
(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: 12.5s)Debug log

sample114.0ms

Algorithm
intervals
Results
14.0ms193×body80valid
10.0ms22×body2560valid
7.0ms14×body1280valid
4.0msbody5120valid
3.0ms11×body640valid
1.0msbody320valid
1.0msbody160valid

simplify55.0ms

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

prune7.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 35.5b

localize23.0ms

Local error

Found 4 expressions with local error:

27.4b
(sqrt (+ (* re re) (* im im)))
1.4b
(- (sqrt (+ (* re re) (* im im))) re)
0.0b
(+ (* re re) (* im im))
0.0b
(sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))

rewrite85.0ms

Algorithm
rewrite-expression-head
Rules
add-log-exp
add-sqr-sqrt
add-cube-cbrt
*-un-lft-identity
sqrt-prod
add-exp-log
sqrt-div
add-cbrt-cube
pow1
flip--
flip-+
associate-*r/
pow1/2
flip3--
flip3-+
sub-neg
sum-log
rem-sqrt-square
+-commutative
Counts
4 → 47
Calls
4 calls:
Slowest
29.0ms
(sqrt (+ (* re re) (* im im)))
25.0ms
(sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))
24.0ms
(- (sqrt (+ (* re re) (* im im))) re)
4.0ms
(+ (* re re) (* im im))

series754.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
606.0ms
(sqrt (+ (* re re) (* im im)))
101.0ms
(sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))
30.0ms
(- (sqrt (+ (* re re) (* im im))) re)
16.0ms
(+ (* re re) (* im im))

simplify731.0ms

Counts
22 → 59
Calls
22 calls:
Slowest
328.0ms
(sqrt (* 2.0 (- (* (sqrt (+ (* re re) (* im im))) (sqrt (+ (* re re) (* im im)))) (* re re))))
211.0ms
(sqrt (* 2.0 (- (pow (sqrt (+ (* re re) (* im im))) 3) (pow re 3))))
105.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
36.0ms
(sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))
6.0ms
(+ (pow im 2) (pow re 2))

prune688.0ms

Pruning

6 alts after pruning (6 fresh and 0 done)

Merged error: 16.7b

localize32.0ms

Local error

Found 4 expressions with local error:

27.4b
(sqrt (+ (* re re) (* im im)))
27.4b
(sqrt (+ (* re re) (* im im)))
8.4b
(- (* (sqrt (sqrt (+ (* re re) (* im im)))) (sqrt (sqrt (+ (* re re) (* im im))))) re)
0.5b
(* (sqrt (sqrt (+ (* re re) (* im im)))) (sqrt (sqrt (+ (* re re) (* im im)))))

rewrite48.0ms

Algorithm
rewrite-expression-head
Rules
28×sqrt-div
24×sqrt-prod
14×add-sqr-sqrt
12×add-cube-cbrt
12×*-un-lft-identity
associate-*l*
associate-*r*
pow1
flip-+
flip3-+
add-exp-log
pow1/2
add-cbrt-cube
add-log-exp
frac-times
pow-prod-up
associate-*r/
associate-*l/
pow-prod-down
rem-sqrt-square
flip--
cbrt-unprod
*-commutative
prod-exp
sqrt-unprod
flip3--
pow-plus
rem-square-sqrt
sub-neg
pow2
Counts
4 → 83
Calls
4 calls:
Slowest
20.0ms
(- (* (sqrt (sqrt (+ (* re re) (* im im)))) (sqrt (sqrt (+ (* re re) (* im im))))) re)
18.0ms
(* (sqrt (sqrt (+ (* re re) (* im im)))) (sqrt (sqrt (+ (* re re) (* im im)))))
4.0ms
(sqrt (+ (* re re) (* im im)))
3.0ms
(sqrt (+ (* re re) (* im im)))

series109.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
31.0ms
(sqrt (+ (* re re) (* im im)))
28.0ms
(- (* (sqrt (sqrt (+ (* re re) (* im im)))) (sqrt (sqrt (+ (* re re) (* im im))))) re)
27.0ms
(sqrt (+ (* re re) (* im im)))
23.0ms
(* (sqrt (sqrt (+ (* re re) (* im im)))) (sqrt (sqrt (+ (* re re) (* im im)))))

simplify1.9s

Counts
64 → 95
Calls
64 calls:
Slowest
186.0ms
(* (sqrt (sqrt (- (* re re) (* im im)))) (sqrt (sqrt (+ (* (* re re) (* re re)) (- (* (* im im) (* im im)) (* (* re re) (* im im)))))))
162.0ms
(* (sqrt (sqrt (+ (* (* re re) (* re re)) (- (* (* im im) (* im im)) (* (* re re) (* im im)))))) (sqrt (sqrt (+ (* (* re re) (* re re)) (- (* (* im im) (* im im)) (* (* re re) (* im im)))))))
158.0ms
(* (sqrt (sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))) (sqrt (sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))))
149.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
137.0ms
(* (sqrt (sqrt (+ (* (* re re) (* re re)) (- (* (* im im) (* im im)) (* (* re re) (* im im)))))) (sqrt (sqrt (- (* re re) (* im im)))))

prune1.1s

Pruning

7 alts after pruning (7 fresh and 0 done)

Merged error: 16.7b

localize46.0ms

Local error

Found 4 expressions with local error:

27.4b
(sqrt (+ (* re re) (* im im)))
27.4b
(sqrt (+ (* re re) (* im im)))
27.4b
(sqrt (+ (* re re) (* im im)))
27.4b
(sqrt (+ (* re re) (* im im)))

rewrite7.0ms

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

series79.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
24.0ms
(sqrt (+ (* re re) (* im im)))
19.0ms
(sqrt (+ (* re re) (* im im)))
18.0ms
(sqrt (+ (* re re) (* im im)))
18.0ms
(sqrt (+ (* re re) (* im im)))

simplify673.0ms

Counts
36 → 68
Calls
36 calls:
Slowest
168.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
104.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
101.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
92.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
46.0ms
(sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))

prune1.1s

Pruning

7 alts after pruning (6 fresh and 1 done)

Merged error: 16.7b

localize17.0ms

Local error

Found 4 expressions with local error:

27.4b
(sqrt (+ (* re re) (* im im)))
27.4b
(sqrt (+ (* re re) (* im im)))
27.4b
(sqrt (+ (* re re) (* im im)))
27.4b
(sqrt (+ (* re re) (* im im)))

rewrite9.0ms

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

series81.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
23.0ms
(sqrt (+ (* re re) (* im im)))
21.0ms
(sqrt (+ (* re re) (* im im)))
20.0ms
(sqrt (+ (* re re) (* im im)))
18.0ms
(sqrt (+ (* re re) (* im im)))

simplify736.0ms

Counts
36 → 68
Calls
36 calls:
Slowest
162.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
136.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
106.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
100.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
56.0ms
(sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))

prune1.1s

Pruning

7 alts after pruning (5 fresh and 2 done)

Merged error: 16.7b

regimes240.0ms

Accuracy

57.2% (8.9b remaining)

Error of 26.0b against oracle of 17.1b and baseline of 37.8b

bsearch110.0ms

end0.0ms

sample2.8s

Algorithm
intervals
Results
1.2s5978×body80valid
590.0ms712×body2560valid
390.0ms492×body1280valid
245.0ms296×body5120valid
172.0ms275×body640valid
96.0ms167×body320valid
17.0ms80×body160valid