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}\]
Target
| Original | 37.2 |
|---|
| Target | 32.3 |
|---|
| Herbie | 5.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
- Split input into 2 regimes
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
Initial program 59.6
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
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))_*}}\]
- Using strategy
rm 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)}}}\]
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)}}}\]
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))))))))
Initial program 30.2
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
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))_*}}\]
- Recombined 2 regimes into one program.
Runtime
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)))))