Average Error: 37.9 → 23.3
Time: 11.3s
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.878549878455066 \cdot 10^{+150}:\\ \;\;\;\;\sqrt{\left(re - im\right) \cdot 2.0} \cdot 0.5\\ \mathbf{elif}\;im \le -6.735337428424573 \cdot 10^{-160}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(\sqrt{\sqrt{im \cdot im + re \cdot re} + re} \cdot \sqrt{\sqrt{im \cdot im + re \cdot re} + re}\right)} \cdot 0.5\\ \mathbf{elif}\;im \le -8.285055990118468 \cdot 10^{-193}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(re + re\right)}\\ \mathbf{elif}\;im \le -1.007255023949383 \cdot 10^{-251}:\\ \;\;\;\;\sqrt{\left(re - im\right) \cdot 2.0} \cdot 0.5\\ \mathbf{elif}\;im \le 3.0747566875798526 \cdot 10^{-136}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(re + re\right)}\\ \mathbf{elif}\;im \le 1.5698886127552807 \cdot 10^{+128}:\\ \;\;\;\;\frac{\sqrt{im \cdot \left(2.0 \cdot im\right)}}{\sqrt{\sqrt{im \cdot im + re \cdot re} - re}} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(re + im\right)} \cdot 0.5\\ \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

Target

Original37.9
Target32.7
Herbie23.3
\[\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 5 regimes
  2. if im < -9.878549878455066e+150 or -8.285055990118468e-193 < im < -1.007255023949383e-251

    1. Initial program 56.8

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

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

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

    if -9.878549878455066e+150 < im < -6.735337428424573e-160

    1. Initial program 24.9

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

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

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

    if -6.735337428424573e-160 < im < -8.285055990118468e-193 or -1.007255023949383e-251 < im < 3.0747566875798526e-136

    1. Initial program 42.1

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

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

    if 3.0747566875798526e-136 < im < 1.5698886127552807e+128

    1. Initial program 22.5

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

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

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

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

    if 1.5698886127552807e+128 < im

    1. Initial program 54.9

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

      \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\color{blue}{e^{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}} + re\right)}\]
    4. Taylor expanded around 0 9.3

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \le -9.878549878455066 \cdot 10^{+150}:\\ \;\;\;\;\sqrt{\left(re - im\right) \cdot 2.0} \cdot 0.5\\ \mathbf{elif}\;im \le -6.735337428424573 \cdot 10^{-160}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(\sqrt{\sqrt{im \cdot im + re \cdot re} + re} \cdot \sqrt{\sqrt{im \cdot im + re \cdot re} + re}\right)} \cdot 0.5\\ \mathbf{elif}\;im \le -8.285055990118468 \cdot 10^{-193}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(re + re\right)}\\ \mathbf{elif}\;im \le -1.007255023949383 \cdot 10^{-251}:\\ \;\;\;\;\sqrt{\left(re - im\right) \cdot 2.0} \cdot 0.5\\ \mathbf{elif}\;im \le 3.0747566875798526 \cdot 10^{-136}:\\ \;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(re + re\right)}\\ \mathbf{elif}\;im \le 1.5698886127552807 \cdot 10^{+128}:\\ \;\;\;\;\frac{\sqrt{im \cdot \left(2.0 \cdot im\right)}}{\sqrt{\sqrt{im \cdot im + re \cdot re} - re}} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;\sqrt{2.0 \cdot \left(re + im\right)} \cdot 0.5\\ \end{array}\]

Reproduce

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

sample59.0ms

Algorithm
intervals
Results
22.0ms180×body80valid
11.0ms22×body2560valid
9.0ms25×body1280valid
4.0msbody5120valid
3.0msbody640valid
2.0msbody320valid
1.0msbody160valid

simplify10.0ms

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

prune6.0ms

Pruning

1 alts after pruning (1 fresh and 0 done)

Merged error: 40.0b

localize21.0ms

Local error

Found 4 expressions with local error:

31.0b
(sqrt (+ (* re re) (* im im)))
1.8b
(+ (sqrt (+ (* re re) (* im im))) re)
0.1b
(sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))
0.0b
(+ (* re re) (* im im))

rewrite21.0ms

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

series177.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
109.0ms
(sqrt (* 2.0 (+ (sqrt (+ (* re re) (* im im))) re)))
28.0ms
(sqrt (+ (* re re) (* im im)))
26.0ms
(+ (sqrt (+ (* re re) (* im im))) re)
14.0ms
(+ (* re re) (* im im))

simplify671.0ms

Counts
22 → 58
Calls
22 calls:
Slowest
301.0ms
(sqrt (* 2.0 (- (* (sqrt (+ (* re re) (* im im))) (sqrt (+ (* re re) (* im im)))) (* re re))))
158.0ms
(sqrt (* 2.0 (+ (pow (sqrt (+ (* re re) (* im im))) 3) (pow re 3))))
117.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
53.0ms
(sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))
11.0ms
(* (exp (* re re)) (exp (* im im)))

prune528.0ms

Pruning

6 alts after pruning (6 fresh and 0 done)

Merged error: 18.5b

localize28.0ms

Local error

Found 4 expressions with local error:

31.0b
(sqrt (+ (* im im) (* re re)))
1.8b
(+ (sqrt (+ (* im im) (* re re))) re)
0.1b
(sqrt (* 2.0 (+ (sqrt (+ (* im im) (* re re))) re)))
0.0b
(+ (* im im) (* re re))

rewrite29.0ms

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

series202.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
112.0ms
(sqrt (* 2.0 (+ (sqrt (+ (* im im) (* re re))) re)))
39.0ms
(+ (sqrt (+ (* im im) (* re re))) re)
30.0ms
(sqrt (+ (* im im) (* re re)))
21.0ms
(+ (* im im) (* re re))

simplify620.0ms

Counts
22 → 58
Calls
22 calls:
Slowest
269.0ms
(sqrt (* 2.0 (- (* (sqrt (+ (* im im) (* re re))) (sqrt (+ (* im im) (* re re)))) (* re re))))
132.0ms
(sqrt (* 2.0 (+ (pow (sqrt (+ (* im im) (* re re))) 3) (pow re 3))))
124.0ms
(sqrt (- (* (* im im) (* im im)) (* (* re re) (* re re))))
54.0ms
(sqrt (+ (pow (* im im) 3) (pow (* re re) 3)))
5.0ms
(* (exp (* im im)) (exp (* re re)))

prune644.0ms

Pruning

6 alts after pruning (6 fresh and 0 done)

Merged error: 11.8b

localize25.0ms

Local error

Found 4 expressions with local error:

31.0b
(sqrt (+ (* im im) (* re re)))
31.0b
(sqrt (+ (* im im) (* re re)))
1.8b
(+ (sqrt (+ (* im im) (* re re))) re)
1.8b
(+ (sqrt (+ (* im im) (* re re))) re)

rewrite36.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
14.0ms
(+ (sqrt (+ (* im im) (* re re))) re)
14.0ms
(+ (sqrt (+ (* im im) (* re re))) re)
3.0ms
(sqrt (+ (* im im) (* re re)))
3.0ms
(sqrt (+ (* im im) (* re re)))

series120.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
36.0ms
(+ (sqrt (+ (* im im) (* re re))) re)
30.0ms
(sqrt (+ (* im im) (* re re)))
29.0ms
(sqrt (+ (* im im) (* re re)))
24.0ms
(+ (sqrt (+ (* im im) (* re re))) re)

simplify428.0ms

Counts
24 → 60
Calls
24 calls:
Slowest
157.0ms
(sqrt (- (* (* im im) (* im im)) (* (* re re) (* re re))))
153.0ms
(sqrt (- (* (* im im) (* im im)) (* (* re re) (* re re))))
47.0ms
(sqrt (+ (pow (* im im) 3) (pow (* re re) 3)))
32.0ms
(sqrt (+ (pow (* im im) 3) (pow (* re re) 3)))
4.0ms
(sqrt (+ (* im im) (* re re)))

prune760.0ms

Pruning

6 alts after pruning (5 fresh and 1 done)

Merged error: 11.8b

localize30.0ms

Local error

Found 4 expressions with local error:

31.0b
(sqrt (+ (* re re) (* im im)))
17.6b
(+ (exp (log (sqrt (+ (* re re) (* im im))))) re)
5.6b
(exp (log (sqrt (+ (* re re) (* im im)))))
0.1b
(sqrt (* 2.0 (+ (exp (log (sqrt (+ (* re re) (* im im))))) re)))

rewrite28.0ms

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

series170.0ms

Counts
4 → 12
Calls
4 calls:
Slowest
100.0ms
(sqrt (* 2.0 (+ (exp (log (sqrt (+ (* re re) (* im im))))) re)))
34.0ms
(+ (exp (log (sqrt (+ (* re re) (* im im))))) re)
18.0ms
(sqrt (+ (* re re) (* im im)))
18.0ms
(exp (log (sqrt (+ (* re re) (* im im)))))

simplify771.0ms

Counts
35 → 68
Calls
35 calls:
Slowest
197.0ms
(sqrt (* 2.0 (- (* (exp (log (sqrt (+ (* re re) (* im im))))) (exp (log (sqrt (+ (* re re) (* im im)))))) (* re re))))
135.0ms
(sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))
131.0ms
(exp (log (sqrt (- (* (* re re) (* re re)) (* (* im im) (* im im))))))
113.0ms
(sqrt (* 2.0 (+ (pow (exp (log (sqrt (+ (* re re) (* im im))))) 3) (pow re 3))))
65.0ms
(exp (log (sqrt (+ (pow (* re re) 3) (pow (* im im) 3)))))

prune772.0ms

Pruning

8 alts after pruning (7 fresh and 1 done)

Merged error: 11.7b

regimes370.0ms

Accuracy

52% (13.6b remaining)

Error of 23.3b against oracle of 9.8b and baseline of 38.0b

bsearch968.0ms

end0.0ms

sample2.4s

Algorithm
intervals
Results
807.0ms5985×body80valid
552.0ms657×body2560valid
335.0ms486×body1280valid
304.0ms310×body5120valid
176.0ms315×body640valid
109.0ms153×body320valid
20.0ms94×body160valid