Average Error: 37.3 → 14.7
Time: 20.6s
Precision: 64
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
\[0.5 \cdot \sqrt{2.0 \cdot \left(\left(\left(\sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt{\left|\sqrt[3]{\mathsf{hypot}\left(re, im\right)}\right|}\right) \cdot \sqrt{\sqrt{\sqrt[3]{\mathsf{hypot}\left(re, im\right)}}}\right) \cdot \sqrt{\sqrt{\mathsf{hypot}\left(re, im\right)}} + re\right)}\]
0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}
0.5 \cdot \sqrt{2.0 \cdot \left(\left(\left(\sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt{\left|\sqrt[3]{\mathsf{hypot}\left(re, im\right)}\right|}\right) \cdot \sqrt{\sqrt{\sqrt[3]{\mathsf{hypot}\left(re, im\right)}}}\right) \cdot \sqrt{\sqrt{\mathsf{hypot}\left(re, im\right)}} + re\right)}
double f(double re, double im) {
        double r1914264 = 0.5;
        double r1914265 = 2.0;
        double r1914266 = re;
        double r1914267 = r1914266 * r1914266;
        double r1914268 = im;
        double r1914269 = r1914268 * r1914268;
        double r1914270 = r1914267 + r1914269;
        double r1914271 = sqrt(r1914270);
        double r1914272 = r1914271 + r1914266;
        double r1914273 = r1914265 * r1914272;
        double r1914274 = sqrt(r1914273);
        double r1914275 = r1914264 * r1914274;
        return r1914275;
}

double f(double re, double im) {
        double r1914276 = 0.5;
        double r1914277 = 2.0;
        double r1914278 = re;
        double r1914279 = im;
        double r1914280 = hypot(r1914278, r1914279);
        double r1914281 = sqrt(r1914280);
        double r1914282 = cbrt(r1914280);
        double r1914283 = fabs(r1914282);
        double r1914284 = sqrt(r1914283);
        double r1914285 = r1914281 * r1914284;
        double r1914286 = sqrt(r1914282);
        double r1914287 = sqrt(r1914286);
        double r1914288 = r1914285 * r1914287;
        double r1914289 = sqrt(r1914281);
        double r1914290 = r1914288 * r1914289;
        double r1914291 = r1914290 + r1914278;
        double r1914292 = r1914277 * r1914291;
        double r1914293 = sqrt(r1914292);
        double r1914294 = r1914276 * r1914293;
        return r1914294;
}

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.3
Target32.6
Herbie14.7
\[\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. Initial program 37.3

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

    \[\leadsto \color{blue}{0.5 \cdot \sqrt{\left(\mathsf{hypot}\left(re, im\right) + re\right) \cdot 2.0}}\]
  3. Using strategy rm
  4. Applied add-sqr-sqrt14.3

    \[\leadsto 0.5 \cdot \sqrt{\left(\color{blue}{\sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt{\mathsf{hypot}\left(re, im\right)}} + re\right) \cdot 2.0}\]
  5. Using strategy rm
  6. Applied add-sqr-sqrt14.3

    \[\leadsto 0.5 \cdot \sqrt{\left(\sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt{\color{blue}{\sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt{\mathsf{hypot}\left(re, im\right)}}} + re\right) \cdot 2.0}\]
  7. Applied sqrt-prod14.6

    \[\leadsto 0.5 \cdot \sqrt{\left(\sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \color{blue}{\left(\sqrt{\sqrt{\mathsf{hypot}\left(re, im\right)}} \cdot \sqrt{\sqrt{\mathsf{hypot}\left(re, im\right)}}\right)} + re\right) \cdot 2.0}\]
  8. Applied associate-*r*14.6

    \[\leadsto 0.5 \cdot \sqrt{\left(\color{blue}{\left(\sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt{\sqrt{\mathsf{hypot}\left(re, im\right)}}\right) \cdot \sqrt{\sqrt{\mathsf{hypot}\left(re, im\right)}}} + re\right) \cdot 2.0}\]
  9. Using strategy rm
  10. Applied add-cube-cbrt14.7

    \[\leadsto 0.5 \cdot \sqrt{\left(\left(\sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt{\sqrt{\color{blue}{\left(\sqrt[3]{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt[3]{\mathsf{hypot}\left(re, im\right)}\right) \cdot \sqrt[3]{\mathsf{hypot}\left(re, im\right)}}}}\right) \cdot \sqrt{\sqrt{\mathsf{hypot}\left(re, im\right)}} + re\right) \cdot 2.0}\]
  11. Applied sqrt-prod14.6

    \[\leadsto 0.5 \cdot \sqrt{\left(\left(\sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt{\color{blue}{\sqrt{\sqrt[3]{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt[3]{\mathsf{hypot}\left(re, im\right)}} \cdot \sqrt{\sqrt[3]{\mathsf{hypot}\left(re, im\right)}}}}\right) \cdot \sqrt{\sqrt{\mathsf{hypot}\left(re, im\right)}} + re\right) \cdot 2.0}\]
  12. Applied sqrt-prod14.7

    \[\leadsto 0.5 \cdot \sqrt{\left(\left(\sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \color{blue}{\left(\sqrt{\sqrt{\sqrt[3]{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt[3]{\mathsf{hypot}\left(re, im\right)}}} \cdot \sqrt{\sqrt{\sqrt[3]{\mathsf{hypot}\left(re, im\right)}}}\right)}\right) \cdot \sqrt{\sqrt{\mathsf{hypot}\left(re, im\right)}} + re\right) \cdot 2.0}\]
  13. Applied associate-*r*14.7

    \[\leadsto 0.5 \cdot \sqrt{\left(\color{blue}{\left(\left(\sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt{\sqrt{\sqrt[3]{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt[3]{\mathsf{hypot}\left(re, im\right)}}}\right) \cdot \sqrt{\sqrt{\sqrt[3]{\mathsf{hypot}\left(re, im\right)}}}\right)} \cdot \sqrt{\sqrt{\mathsf{hypot}\left(re, im\right)}} + re\right) \cdot 2.0}\]
  14. Simplified14.7

    \[\leadsto 0.5 \cdot \sqrt{\left(\left(\color{blue}{\left(\sqrt{\left|\sqrt[3]{\mathsf{hypot}\left(re, im\right)}\right|} \cdot \sqrt{\mathsf{hypot}\left(re, im\right)}\right)} \cdot \sqrt{\sqrt{\sqrt[3]{\mathsf{hypot}\left(re, im\right)}}}\right) \cdot \sqrt{\sqrt{\mathsf{hypot}\left(re, im\right)}} + re\right) \cdot 2.0}\]
  15. Final simplification14.7

    \[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\left(\left(\sqrt{\mathsf{hypot}\left(re, im\right)} \cdot \sqrt{\left|\sqrt[3]{\mathsf{hypot}\left(re, im\right)}\right|}\right) \cdot \sqrt{\sqrt{\sqrt[3]{\mathsf{hypot}\left(re, im\right)}}}\right) \cdot \sqrt{\sqrt{\mathsf{hypot}\left(re, im\right)}} + re\right)}\]

Reproduce

herbie shell --seed 2019132 +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)))))