Average Error: 37.2 → 5.6
Time: 20.8s
Precision: 64
Internal Precision: 3392
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
\[\begin{array}{l} \mathbf{if}\;0.5 \cdot \sqrt{e^{\left(\sqrt[3]{\log \left((\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*\right)} \cdot \sqrt[3]{\log \left((\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*\right)}\right) \cdot \sqrt[3]{\log \left((\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*\right)}}} \le 3.7397622310826 \cdot 10^{-310}:\\ \;\;\;\;\sqrt{\frac{1.0}{\frac{-1}{im}} \cdot \frac{\frac{-1}{re}}{\frac{-1}{im}}} \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}\\ \end{array}\]

Error

Bits error versus re

Bits error versus im

Target

Original37.2
Target32.3
Herbie5.6
\[\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 2 regimes
  2. if (* 0.5 (sqrt (exp (* (* (cbrt (log (fma (hypot re im) 2.0 (* re 2.0)))) (cbrt (log (fma (hypot re im) 2.0 (* re 2.0))))) (cbrt (log (fma (hypot re im) 2.0 (* re 2.0)))))))) < 3.7397622310826e-310

    1. Initial program 59.6

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

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}\]
    3. Using strategy rm
    4. Applied add-exp-log53.4

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{e^{\log \left((\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*\right)}}}\]
    5. Taylor expanded around -inf 42.4

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{e^{\left(\log \left(\frac{-1}{re}\right) + \log 1.0\right) - 2 \cdot \log \left(\frac{-1}{im}\right)}}}\]
    6. Applied simplify22.5

      \[\leadsto \color{blue}{\sqrt{\frac{1.0}{\frac{-1}{im}} \cdot \frac{\frac{-1}{re}}{\frac{-1}{im}}} \cdot 0.5}\]

    if 3.7397622310826e-310 < (* 0.5 (sqrt (exp (* (* (cbrt (log (fma (hypot re im) 2.0 (* re 2.0)))) (cbrt (log (fma (hypot re im) 2.0 (* re 2.0))))) (cbrt (log (fma (hypot re im) 2.0 (* re 2.0))))))))

    1. Initial program 30.2

      \[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
    2. Applied simplify0.3

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}}\]
  3. Recombined 2 regimes into one program.

Runtime

Time bar (total: 20.8s)Debug logProfile

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