Average Error: 37.3 → 17.3
Time: 14.5s
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 -3.423353045456592 \cdot 10^{+72}:\\ \;\;\;\;\sqrt{\left(-2 \cdot re\right) \cdot 2.0} \cdot 0.5\\ \mathbf{elif}\;re \le -9.627507825106705 \cdot 10^{-299}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(\sqrt{\sqrt{im \cdot im + re \cdot re}} \cdot \sqrt{\sqrt{im \cdot im + re \cdot re}} - re\right)} \cdot 0.5\\ \mathbf{elif}\;re \le 3.771304173360558 \cdot 10^{+140}:\\ \;\;\;\;\left(\sqrt{2.0} \cdot \frac{\left|im\right|}{\sqrt{\sqrt{im \cdot im + re \cdot re} + re}}\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\frac{\left|im\right|}{\sqrt{re + re}} \cdot \sqrt{2.0}\right)\\ \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 4 regimes
  2. if re < -3.423353045456592e+72

    1. Initial program 46.0

      \[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} - re\right)}\]
    2. Taylor expanded around -inf 11.9

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

    if -3.423353045456592e+72 < re < -9.627507825106705e-299

    1. Initial program 20.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-sqr-sqrt20.8

      \[\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)}\]

    if -9.627507825106705e-299 < re < 3.771304173360558e+140

    1. Initial program 38.6

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

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

      \[\leadsto 0.5 \cdot \left(\sqrt{2.0} \cdot \sqrt{\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}}}\right)\]
    6. Applied sqrt-div38.7

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

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

    if 3.771304173360558e+140 < re

    1. Initial program 61.3

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

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

      \[\leadsto 0.5 \cdot \left(\sqrt{2.0} \cdot \sqrt{\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}}}\right)\]
    6. Applied sqrt-div61.3

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

      \[\leadsto 0.5 \cdot \left(\sqrt{2.0} \cdot \frac{\color{blue}{\left|im\right|}}{\sqrt{\sqrt{re \cdot re + im \cdot im} + re}}\right)\]
    8. Taylor expanded around inf 8.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \le -3.423353045456592 \cdot 10^{+72}:\\ \;\;\;\;\sqrt{\left(-2 \cdot re\right) \cdot 2.0} \cdot 0.5\\ \mathbf{elif}\;re \le -9.627507825106705 \cdot 10^{-299}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(\sqrt{\sqrt{im \cdot im + re \cdot re}} \cdot \sqrt{\sqrt{im \cdot im + re \cdot re}} - re\right)} \cdot 0.5\\ \mathbf{elif}\;re \le 3.771304173360558 \cdot 10^{+140}:\\ \;\;\;\;\left(\sqrt{2.0} \cdot \frac{\left|im\right|}{\sqrt{\sqrt{im \cdot im + re \cdot re} + re}}\right) \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\frac{\left|im\right|}{\sqrt{re + re}} \cdot \sqrt{2.0}\right)\\ \end{array}\]

Reproduce

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

sample177.0ms

Algorithm
intervals
Results
32.0ms199×body80valid
21.0ms22×body2560valid
10.0ms14×body1280valid
5.0msbody640valid
5.0msbody5120valid
3.0msbody320valid
0.0msbody160valid

simplify48.0ms

Counts
1 → 1
Calls
1 calls:
Slowest
48.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: 35.6b

localize23.0ms

Local error

Found 4 expressions with local error:

26.2b
(sqrt (+ (* re re) (* im im)))
0.3b
(- (sqrt (+ (* re re) (* im im))) re)
0.0b
(sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))
0.0b
(+ (* re re) (* im im))

rewrite20.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
11.0ms
(- (sqrt (+ (* re re) (* im im))) re)
3.0ms
(sqrt (+ (* re re) (* im im)))
2.0ms
(+ (* re re) (* im im))
2.0ms
(sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))

series450.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
302.0ms
(sqrt (+ (* re re) (* im im)))
102.0ms
(sqrt (* 2.0 (- (sqrt (+ (* re re) (* im im))) re)))
25.0ms
(- (sqrt (+ (* re re) (* im im))) re)
20.0ms
(+ (* re re) (* im im))

simplify740.0ms

Counts
22 → 59
Calls
22 calls:
Slowest
315.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))))
119.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
38.0ms
(sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))
8.0ms
(* (exp (* re re)) (exp (* im im)))

prune617.0ms

Pruning

5 alts after pruning (5 fresh and 0 done)

Merged error: 14.5b

localize14.0ms

Local error

Found 4 expressions with local error:

26.2b
(sqrt (+ (* re re) (* im im)))
0.4b
(* (sqrt 2.0) (sqrt (- (sqrt (+ (* re re) (* im im))) re)))
0.3b
(- (sqrt (+ (* re re) (* im im))) re)
0.0b
(sqrt (- (sqrt (+ (* re re) (* im im))) re))

rewrite55.0ms

Algorithm
rewrite-expression-head
Rules
11×add-sqr-sqrt
sqrt-prod
add-cube-cbrt
*-un-lft-identity
add-exp-log
associate-*r*
sqrt-div
add-cbrt-cube
pow1
add-log-exp
pow1/2
flip--
associate-*l*
flip3--
associate-*r/
pow-prod-down
rem-sqrt-square
flip-+
cbrt-unprod
*-commutative
prod-exp
sqrt-unprod
flip3-+
sub-neg
Counts
4 → 63
Calls
4 calls:
Slowest
30.0ms
(* (sqrt 2.0) (sqrt (- (sqrt (+ (* re re) (* im im))) re)))
12.0ms
(- (sqrt (+ (* re re) (* im im))) re)
8.0ms
(sqrt (- (sqrt (+ (* re re) (* im im))) re))
3.0ms
(sqrt (+ (* re re) (* im im)))

series198.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
91.0ms
(* (sqrt 2.0) (sqrt (- (sqrt (+ (* re re) (* im im))) re)))
64.0ms
(sqrt (- (sqrt (+ (* re re) (* im im))) re))
24.0ms
(- (sqrt (+ (* re re) (* im im))) re)
19.0ms
(sqrt (+ (* re re) (* im im)))

simplify1.1s

Counts
39 → 75
Calls
39 calls:
Slowest
496.0ms
(sqrt (- (* (sqrt (+ (* re re) (* im im))) (sqrt (+ (* re re) (* im im)))) (* re re)))
159.0ms
(* (* (* (sqrt 2.0) (sqrt 2.0)) (sqrt 2.0)) (* (* (sqrt (- (sqrt (+ (* re re) (* im im))) re)) (sqrt (- (sqrt (+ (* re re) (* im im))) re))) (sqrt (- (sqrt (+ (* re re) (* im im))) re))))
113.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
76.0ms
(sqrt (- (pow (sqrt (+ (* re re) (* im im))) 3) (pow re 3)))
44.0ms
(sqrt (+ (* (sqrt (+ (* re re) (* im im))) (sqrt (+ (* re re) (* im im)))) (+ (* re re) (* (sqrt (+ (* re re) (* im im))) re))))

prune775.0ms

Pruning

7 alts after pruning (6 fresh and 1 done)

Merged error: 10.6b

localize26.0ms

Local error

Found 4 expressions with local error:

26.2b
(sqrt (+ (* re re) (* im im)))
1.5b
(+ (sqrt (+ (* re re) (* im im))) re)
0.4b
(* (sqrt 2.0) (/ (fabs im) (sqrt (+ (sqrt (+ (* re re) (* im im))) re))))
0.3b
(/ (fabs im) (sqrt (+ (sqrt (+ (* re re) (* im im))) re)))

rewrite38.0ms

Algorithm
rewrite-expression-head
Rules
36×times-frac
35×add-sqr-sqrt
34×add-cube-cbrt
34×*-un-lft-identity
24×sqrt-prod
24×associate-*r*
11×add-exp-log
11×add-cbrt-cube
sqrt-div
associate-/r*
pow1
add-log-exp
flip-+
associate-/r/
flip3-+
associate-/l*
associate-*l*
div-inv
cbrt-unprod
prod-exp
div-exp
cbrt-undiv
*-commutative
associate-*r/
pow-prod-down
pow1/2
frac-2neg
clear-num
rem-sqrt-square
+-commutative
Counts
4 → 106
Calls
4 calls:
Slowest
17.0ms
(* (sqrt 2.0) (/ (fabs im) (sqrt (+ (sqrt (+ (* re re) (* im im))) re))))
11.0ms
(/ (fabs im) (sqrt (+ (sqrt (+ (* re re) (* im im))) re)))
7.0ms
(+ (sqrt (+ (* re re) (* im im))) re)
2.0ms
(sqrt (+ (* re re) (* im im)))

series254.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
105.0ms
(* (sqrt 2.0) (/ (fabs im) (sqrt (+ (sqrt (+ (* re re) (* im im))) re))))
85.0ms
(/ (fabs im) (sqrt (+ (sqrt (+ (* re re) (* im im))) re)))
39.0ms
(+ (sqrt (+ (* re re) (* im im))) re)
25.0ms
(sqrt (+ (* re re) (* im im)))

simplify1.7s

Counts
98 → 118
Calls
98 calls:
Slowest
449.0ms
(* (* (* (sqrt 2.0) (sqrt 2.0)) (sqrt 2.0)) (/ (* (* (fabs im) (fabs im)) (fabs im)) (* (* (sqrt (+ (sqrt (+ (* re re) (* im im))) re)) (sqrt (+ (sqrt (+ (* re re) (* im im))) re))) (sqrt (+ (sqrt (+ (* re re) (* im im))) re)))))
132.0ms
(* (* (* (sqrt 2.0) (sqrt 2.0)) (sqrt 2.0)) (* (* (/ (fabs im) (sqrt (+ (sqrt (+ (* re re) (* im im))) re))) (/ (fabs im) (sqrt (+ (sqrt (+ (* re re) (* im im))) re)))) (/ (fabs im) (sqrt (+ (sqrt (+ (* re re) (* im im))) re)))))
127.0ms
(sqrt (+ (* (sqrt (+ (* re re) (* im im))) (sqrt (+ (* re re) (* im im)))) (- (* re re) (* (sqrt (+ (* re re) (* im im))) re))))
124.0ms
(/ (* (* (fabs im) (fabs im)) (fabs im)) (* (* (sqrt (+ (sqrt (+ (* re re) (* im im))) re)) (sqrt (+ (sqrt (+ (* re re) (* im im))) re))) (sqrt (+ (sqrt (+ (* re re) (* im im))) re))))
112.0ms
(sqrt (+ (* (sqrt (+ (* re re) (* im im))) (sqrt (+ (* re re) (* im im)))) (- (* re re) (* (sqrt (+ (* re re) (* im im))) re))))

prune1.7s

Pruning

11 alts after pruning (9 fresh and 2 done)

Merged error: 7.1b

localize10.0ms

Local error

Found 4 expressions with local error:

26.2b
(sqrt (+ (* re re) (* im im)))
26.2b
(sqrt (+ (* re re) (* im im)))
1.5b
(+ (sqrt (+ (* re re) (* im im))) re)
1.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
flip-+
add-exp-log
sqrt-div
add-cbrt-cube
flip3-+
pow1
pow1/2
rem-sqrt-square
+-commutative
Counts
4 → 48
Calls
4 calls:
Slowest
9.0ms
(+ (sqrt (+ (* re re) (* im im))) re)
7.0ms
(+ (sqrt (+ (* re re) (* im im))) re)
2.0ms
(sqrt (+ (* re re) (* im im)))
2.0ms
(sqrt (+ (* re re) (* im im)))

series100.0ms

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

simplify440.0ms

Counts
24 → 60
Calls
24 calls:
Slowest
174.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
108.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
62.0ms
(sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))
45.0ms
(sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))
6.0ms
(sqrt (* (cbrt (+ (* re re) (* im im))) (cbrt (+ (* re re) (* im im)))))

prune1.1s

Pruning

11 alts after pruning (8 fresh and 3 done)

Merged error: 7.1b

regimes527.0ms

Accuracy

67.3% (9.5b remaining)

Error of 17.3b against oracle of 7.8b and baseline of 36.9b

bsearch294.0ms

end0.0ms

sample2.7s

Algorithm
intervals
Results
944.0ms674×body2560valid
760.0ms6055×body80valid
300.0ms487×body1280valid
297.0ms274×body5120valid
151.0ms282×body640valid
57.0ms175×body320valid
11.0ms53×body160valid