Average Error: 31.5 → 11.8
Time: 1.3min
Precision: binary64
Cost: 15044
\[\log \left(\sqrt{re \cdot re + im \cdot im}\right)\]
\[\begin{array}{l} \mathbf{if}\;im \cdot im \leq 4.496931482573535 \cdot 10^{-289}:\\ \;\;\;\;\log \left(\left|re\right|\right)\\ \mathbf{elif}\;im \cdot im \leq 2.8151563237733957 \cdot 10^{-47}:\\ \;\;\;\;\log \left(\sqrt{im \cdot im + re \cdot re}\right)\\ \mathbf{elif}\;im \cdot im \leq 3.0416083611477998:\\ \;\;\;\;\log \left(\left|re\right|\right)\\ \mathbf{elif}\;im \cdot im \leq 1.1242704904966132 \cdot 10^{+233}:\\ \;\;\;\;\log \left(\sqrt{im \cdot im + re \cdot re}\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(\left|im\right|\right)\\ \end{array}\]
\log \left(\sqrt{re \cdot re + im \cdot im}\right)
\begin{array}{l}
\mathbf{if}\;im \cdot im \leq 4.496931482573535 \cdot 10^{-289}:\\
\;\;\;\;\log \left(\left|re\right|\right)\\

\mathbf{elif}\;im \cdot im \leq 2.8151563237733957 \cdot 10^{-47}:\\
\;\;\;\;\log \left(\sqrt{im \cdot im + re \cdot re}\right)\\

\mathbf{elif}\;im \cdot im \leq 3.0416083611477998:\\
\;\;\;\;\log \left(\left|re\right|\right)\\

\mathbf{elif}\;im \cdot im \leq 1.1242704904966132 \cdot 10^{+233}:\\
\;\;\;\;\log \left(\sqrt{im \cdot im + re \cdot re}\right)\\

\mathbf{else}:\\
\;\;\;\;\log \left(\left|im\right|\right)\\

\end{array}
(FPCore (re im) :precision binary64 (log (sqrt (+ (* re re) (* im im)))))
(FPCore (re im)
 :precision binary64
 (if (<= (* im im) 4.496931482573535e-289)
   (log (fabs re))
   (if (<= (* im im) 2.8151563237733957e-47)
     (log (sqrt (+ (* im im) (* re re))))
     (if (<= (* im im) 3.0416083611477998)
       (log (fabs re))
       (if (<= (* im im) 1.1242704904966132e+233)
         (log (sqrt (+ (* im im) (* re re))))
         (log (fabs im)))))))
double code(double re, double im) {
	return log(sqrt((re * re) + (im * im)));
}
double code(double re, double im) {
	double tmp;
	if ((im * im) <= 4.496931482573535e-289) {
		tmp = log(fabs(re));
	} else if ((im * im) <= 2.8151563237733957e-47) {
		tmp = log(sqrt((im * im) + (re * re)));
	} else if ((im * im) <= 3.0416083611477998) {
		tmp = log(fabs(re));
	} else if ((im * im) <= 1.1242704904966132e+233) {
		tmp = log(sqrt((im * im) + (re * re)));
	} else {
		tmp = log(fabs(im));
	}
	return tmp;
}

Error

Bits error versus re

Bits error versus im

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Alternatives

Alternative 1
Error14.1
Cost14790
\[\begin{array}{l} \mathbf{if}\;im \leq -106.83797026055463:\\ \;\;\;\;\log \left(-im\right)\\ \mathbf{elif}\;im \leq 1.1067513554342732 \cdot 10^{-109}:\\ \;\;\;\;\log \left(\left|re\right|\right)\\ \mathbf{elif}\;im \leq 2.243616955365425 \cdot 10^{-68}:\\ \;\;\;\;\log im\\ \mathbf{elif}\;im \leq 5.3731698519203635 \cdot 10^{-48}:\\ \;\;\;\;\log \left(\left|re\right|\right)\\ \mathbf{elif}\;im \leq 1.3118079959809238 \cdot 10^{+46}:\\ \;\;\;\;\log \left(im + 0.5 \cdot \frac{re \cdot re}{im}\right)\\ \mathbf{elif}\;im \leq 4.862508318544993 \cdot 10^{+62}:\\ \;\;\;\;\log \left(\left|re\right|\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(\left|im\right|\right)\\ \end{array}\]
Alternative 2
Error14.1
Cost14790
\[\begin{array}{l} \mathbf{if}\;im \leq -235.28556719233924:\\ \;\;\;\;\log \left(-im\right)\\ \mathbf{elif}\;im \leq 1.1067513554342732 \cdot 10^{-109}:\\ \;\;\;\;\log \left(\left|re\right|\right)\\ \mathbf{elif}\;im \leq 2.243616955365425 \cdot 10^{-68}:\\ \;\;\;\;\log im\\ \mathbf{elif}\;im \leq 6.945777971472865 \cdot 10^{-47}:\\ \;\;\;\;\log \left(\left|re\right|\right)\\ \mathbf{elif}\;im \leq 3.9755217757921706 \cdot 10^{+45}:\\ \;\;\;\;\log \left(im + 0.5 \cdot \frac{re \cdot re}{im}\right)\\ \mathbf{elif}\;im \leq 4.002851153219117 \cdot 10^{+61}:\\ \;\;\;\;\log \left(\left|re\right|\right)\\ \mathbf{else}:\\ \;\;\;\;\log im\\ \end{array}\]
Alternative 3
Error25.2
Cost8454
\[\begin{array}{l} \mathbf{if}\;im \leq -104.81621854878125:\\ \;\;\;\;\log \left(-im\right)\\ \mathbf{elif}\;im \leq -4.1416446537757786 \cdot 10^{-60}:\\ \;\;\;\;\log \left(-re\right)\\ \mathbf{elif}\;im \leq -8.801481458014728 \cdot 10^{-160}:\\ \;\;\;\;\log re\\ \mathbf{elif}\;im \leq -1.061832494382022 \cdot 10^{-270}:\\ \;\;\;\;\log \left(-re\right)\\ \mathbf{elif}\;im \leq 9.679007844856983 \cdot 10^{-293}:\\ \;\;\;\;\log re\\ \mathbf{elif}\;im \leq 1.823051975270757 \cdot 10^{-127}:\\ \;\;\;\;\log \left(-re\right)\\ \mathbf{else}:\\ \;\;\;\;\log im\\ \end{array}\]
Alternative 4
Error24.7
Cost7106
\[\begin{array}{l} \mathbf{if}\;im \leq -9.529487865086369 \cdot 10^{-80}:\\ \;\;\;\;\log \left(-im\right)\\ \mathbf{elif}\;im \leq 7.369539249726618 \cdot 10^{-146}:\\ \;\;\;\;\log re\\ \mathbf{else}:\\ \;\;\;\;\log im\\ \end{array}\]
Alternative 5
Error35.2
Cost6785
\[\begin{array}{l} \mathbf{if}\;re \leq 2.0788063215962518 \cdot 10^{-98}:\\ \;\;\;\;\log im\\ \mathbf{else}:\\ \;\;\;\;\log re\\ \end{array}\]
Alternative 6
Error43.0
Cost6785
\[\begin{array}{l} \mathbf{if}\;re \leq 3.6990940831335627 \cdot 10^{-301}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log re\\ \end{array}\]
Alternative 7
Error57.0
Cost64
\[1\]

Error

Time

Derivation

  1. Split input into 3 regimes
  2. if (*.f64 im im) < 4.49693148257353471e-289 or 2.81515632377339565e-47 < (*.f64 im im) < 3.0416083611477998

    1. Initial program 27.4

      \[\log \left(\sqrt{re \cdot re + im \cdot im}\right)\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt_binary64_44127.4

      \[\leadsto \log \left(\sqrt{\color{blue}{\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im}}}\right)\]
    4. Applied rem-sqrt-square_binary64_43227.4

      \[\leadsto \log \color{blue}{\left(\left|\sqrt{re \cdot re + im \cdot im}\right|\right)}\]
    5. Taylor expanded around inf 9.8

      \[\leadsto \log \left(\left|\color{blue}{re}\right|\right)\]
    6. Simplified9.8

      \[\leadsto \color{blue}{\log \left(\left|re\right|\right)}\]

    if 4.49693148257353471e-289 < (*.f64 im im) < 2.81515632377339565e-47 or 3.0416083611477998 < (*.f64 im im) < 1.1242704904966132e233

    1. Initial program 16.4

      \[\log \left(\sqrt{re \cdot re + im \cdot im}\right)\]
    2. Using strategy rm
    3. Applied +-commutative_binary64_34916.4

      \[\leadsto \log \left(\sqrt{\color{blue}{im \cdot im + re \cdot re}}\right)\]
    4. Simplified16.4

      \[\leadsto \color{blue}{\log \left(\sqrt{re \cdot re + im \cdot im}\right)}\]

    if 1.1242704904966132e233 < (*.f64 im im)

    1. Initial program 54.8

      \[\log \left(\sqrt{re \cdot re + im \cdot im}\right)\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt_binary64_44154.8

      \[\leadsto \log \left(\sqrt{\color{blue}{\sqrt{re \cdot re + im \cdot im} \cdot \sqrt{re \cdot re + im \cdot im}}}\right)\]
    4. Applied rem-sqrt-square_binary64_43254.8

      \[\leadsto \log \color{blue}{\left(\left|\sqrt{re \cdot re + im \cdot im}\right|\right)}\]
    5. Taylor expanded around 0 7.9

      \[\leadsto \log \left(\left|\color{blue}{im}\right|\right)\]
    6. Simplified7.9

      \[\leadsto \color{blue}{\log \left(\left|im\right|\right)}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification11.8

    \[\leadsto \begin{array}{l} \mathbf{if}\;im \cdot im \leq 4.496931482573535 \cdot 10^{-289}:\\ \;\;\;\;\log \left(\left|re\right|\right)\\ \mathbf{elif}\;im \cdot im \leq 2.8151563237733957 \cdot 10^{-47}:\\ \;\;\;\;\log \left(\sqrt{im \cdot im + re \cdot re}\right)\\ \mathbf{elif}\;im \cdot im \leq 3.0416083611477998:\\ \;\;\;\;\log \left(\left|re\right|\right)\\ \mathbf{elif}\;im \cdot im \leq 1.1242704904966132 \cdot 10^{+233}:\\ \;\;\;\;\log \left(\sqrt{im \cdot im + re \cdot re}\right)\\ \mathbf{else}:\\ \;\;\;\;\log \left(\left|im\right|\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2021040 
(FPCore (re im)
  :name "math.log/1 on complex, real part"
  :precision binary64
  (log (sqrt (+ (* re re) (* im im)))))