Average Error: 37.3 → 5.8
Time: 31.5s
Precision: 64
Internal Precision: 3456
\[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{\left(\sqrt[3]{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*} \cdot \sqrt[3]{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}\right) \cdot \sqrt[3]{(\left(\sqrt{re^2 + im^2}^*\right) \cdot 2.0 + \left(re \cdot 2.0\right))_*}} \le 0.0:\\ \;\;\;\;\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.3
Target32.7
Herbie5.8
\[\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 (* (* (cbrt (fma (hypot re im) 2.0 (* re 2.0))) (cbrt (fma (hypot re im) 2.0 (* re 2.0)))) (cbrt (fma (hypot re im) 2.0 (* re 2.0)))))) < 0.0

    1. Initial program 59.8

      \[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 44.8

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

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

    if 0.0 < (* 0.5 (sqrt (* (* (cbrt (fma (hypot re im) 2.0 (* re 2.0))) (cbrt (fma (hypot re im) 2.0 (* re 2.0)))) (cbrt (fma (hypot re im) 2.0 (* re 2.0))))))

    1. Initial program 30.3

      \[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: 31.5s)Debug logProfile

herbie shell --seed '#(1070991898 1055468627 4280279443 640792587 928206309 3646738750)' +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)))))