Average Error: 37.5 → 24.7
Time: 38.6s
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}\;\frac{\frac{-1}{re}}{\frac{-1}{im}} \cdot \frac{\frac{1}{2} \cdot 2.0}{\frac{-1}{im}} \le -6.436265392040012 \cdot 10^{+268}:\\
\;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(im + re\right)}\\
\mathbf{if}\;\frac{\frac{-1}{re}}{\frac{-1}{im}} \cdot \frac{\frac{1}{2} \cdot 2.0}{\frac{-1}{im}} \le -1.1633688800232677 \cdot 10^{-77}:\\
\;\;\;\;0.5 \cdot \sqrt{2.0 \cdot {\left(e^{\sqrt[3]{\log \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \sqrt[3]{\log \left(\sqrt{re \cdot re + im \cdot im} + re\right)}}\right)}^{\left(\sqrt[3]{\log \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}}\\
\mathbf{if}\;\frac{\frac{-1}{re}}{\frac{-1}{im}} \cdot \frac{\frac{1}{2} \cdot 2.0}{\frac{-1}{im}} \le 3.8759052411778 \cdot 10^{-313}:\\
\;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(re + re\right)}\\
\mathbf{if}\;\frac{\frac{-1}{re}}{\frac{-1}{im}} \cdot \frac{\frac{1}{2} \cdot 2.0}{\frac{-1}{im}} \le 2.557898934912673 \cdot 10^{-124}:\\
\;\;\;\;\sqrt{\frac{\frac{-1}{re}}{\frac{-1}{im}} \cdot \frac{\frac{1}{2} \cdot 2.0}{\frac{-1}{im}}} \cdot 0.5\\
\mathbf{if}\;\frac{\frac{-1}{re}}{\frac{-1}{im}} \cdot \frac{\frac{1}{2} \cdot 2.0}{\frac{-1}{im}} \le 9.996032980801339 \cdot 10^{+154}:\\
\;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \frac{im \cdot im}{\sqrt{re \cdot re + im \cdot im} - re}}\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \sqrt{2.0 \cdot \left(im + re\right)}\\
\end{array}\]
Target
| Original | 37.5 |
|---|
| Target | 32.6 |
|---|
| Herbie | 24.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
- Split input into 5 regimes
if (* (/ (/ -1 re) (/ -1 im)) (/ (* 1/2 2.0) (/ -1 im))) < -6.436265392040012e+268 or 9.996032980801339e+154 < (* (/ (/ -1 re) (/ -1 im)) (/ (* 1/2 2.0) (/ -1 im)))
Initial program 41.0
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
Taylor expanded around 0 31.2
\[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\color{blue}{im} + re\right)}\]
if -6.436265392040012e+268 < (* (/ (/ -1 re) (/ -1 im)) (/ (* 1/2 2.0) (/ -1 im))) < -1.1633688800232677e-77
Initial program 16.9
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
- Using strategy
rm Applied add-exp-log19.6
\[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \color{blue}{e^{\log \left(\sqrt{re \cdot re + im \cdot im} + re\right)}}}\]
- Using strategy
rm Applied add-cube-cbrt21.4
\[\leadsto 0.5 \cdot \sqrt{2.0 \cdot e^{\color{blue}{\left(\sqrt[3]{\log \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \sqrt[3]{\log \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right) \cdot \sqrt[3]{\log \left(\sqrt{re \cdot re + im \cdot im} + re\right)}}}}\]
Applied exp-prod21.4
\[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \color{blue}{{\left(e^{\sqrt[3]{\log \left(\sqrt{re \cdot re + im \cdot im} + re\right)} \cdot \sqrt[3]{\log \left(\sqrt{re \cdot re + im \cdot im} + re\right)}}\right)}^{\left(\sqrt[3]{\log \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\right)}}}\]
if -1.1633688800232677e-77 < (* (/ (/ -1 re) (/ -1 im)) (/ (* 1/2 2.0) (/ -1 im))) < 3.8759052411778e-313
Initial program 39.4
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
Taylor expanded around inf 25.3
\[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \left(\color{blue}{re} + re\right)}\]
if 3.8759052411778e-313 < (* (/ (/ -1 re) (/ -1 im)) (/ (* 1/2 2.0) (/ -1 im))) < 2.557898934912673e-124
Initial program 61.3
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
- Using strategy
rm Applied add-exp-log61.3
\[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \color{blue}{e^{\log \left(\sqrt{re \cdot re + im \cdot im} + re\right)}}}\]
Taylor expanded around -inf 39.1
\[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \color{blue}{e^{\left(\log \frac{1}{2} + \log \left(\frac{-1}{re}\right)\right) - 2 \cdot \log \left(\frac{-1}{im}\right)}}}\]
Applied simplify9.2
\[\leadsto \color{blue}{\sqrt{\frac{\frac{-1}{re}}{\frac{-1}{im}} \cdot \frac{\frac{1}{2} \cdot 2.0}{\frac{-1}{im}}} \cdot 0.5}\]
if 2.557898934912673e-124 < (* (/ (/ -1 re) (/ -1 im)) (/ (* 1/2 2.0) (/ -1 im))) < 9.996032980801339e+154
Initial program 30.5
\[0.5 \cdot \sqrt{2.0 \cdot \left(\sqrt{re \cdot re + im \cdot im} + re\right)}\]
- Using strategy
rm Applied flip-+30.5
\[\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}}}\]
Applied simplify18.2
\[\leadsto 0.5 \cdot \sqrt{2.0 \cdot \frac{\color{blue}{im \cdot im}}{\sqrt{re \cdot re + im \cdot im} - re}}\]
- Recombined 5 regimes into one program.
Runtime
herbie shell --seed '#(1070864556 424010669 783715395 1203517814 4070606583 4107618214)'
(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)))))