math.log/1 on complex, real part

Percentage Accurate: 51.4% → 100.0%
Time: 5.9s
Alternatives: 6
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \log \left(\sqrt{re \cdot re + im \cdot im}\right) \end{array} \]
(FPCore (re im) :precision binary64 (log (sqrt (+ (* re re) (* im im)))))
double code(double re, double im) {
	return log(sqrt(((re * re) + (im * im))));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log(sqrt(((re * re) + (im * im))))
end function
public static double code(double re, double im) {
	return Math.log(Math.sqrt(((re * re) + (im * im))));
}
def code(re, im):
	return math.log(math.sqrt(((re * re) + (im * im))))
function code(re, im)
	return log(sqrt(Float64(Float64(re * re) + Float64(im * im))))
end
function tmp = code(re, im)
	tmp = log(sqrt(((re * re) + (im * im))));
end
code[re_, im_] := N[Log[N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(\sqrt{re \cdot re + im \cdot im}\right)
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 6 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 51.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(\sqrt{re \cdot re + im \cdot im}\right) \end{array} \]
(FPCore (re im) :precision binary64 (log (sqrt (+ (* re re) (* im im)))))
double code(double re, double im) {
	return log(sqrt(((re * re) + (im * im))));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log(sqrt(((re * re) + (im * im))))
end function
public static double code(double re, double im) {
	return Math.log(Math.sqrt(((re * re) + (im * im))));
}
def code(re, im):
	return math.log(math.sqrt(((re * re) + (im * im))))
function code(re, im)
	return log(sqrt(Float64(Float64(re * re) + Float64(im * im))))
end
function tmp = code(re, im)
	tmp = log(sqrt(((re * re) + (im * im))));
end
code[re_, im_] := N[Log[N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(\sqrt{re \cdot re + im \cdot im}\right)
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(\mathsf{hypot}\left(re, im\right)\right) \end{array} \]
(FPCore (re im) :precision binary64 (log (hypot re im)))
double code(double re, double im) {
	return log(hypot(re, im));
}
public static double code(double re, double im) {
	return Math.log(Math.hypot(re, im));
}
def code(re, im):
	return math.log(math.hypot(re, im))
function code(re, im)
	return log(hypot(re, im))
end
function tmp = code(re, im)
	tmp = log(hypot(re, im));
end
code[re_, im_] := N[Log[N[Sqrt[re ^ 2 + im ^ 2], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(\mathsf{hypot}\left(re, im\right)\right)
\end{array}
Derivation
  1. Initial program 51.1%

    \[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
  2. Step-by-step derivation
    1. log-lowering-log.f64N/A

      \[\leadsto \mathsf{log.f64}\left(\left(\sqrt{re \cdot re + im \cdot im}\right)\right) \]
    2. hypot-defineN/A

      \[\leadsto \mathsf{log.f64}\left(\left(\mathsf{hypot}\left(re, im\right)\right)\right) \]
    3. hypot-lowering-hypot.f64100.0%

      \[\leadsto \mathsf{log.f64}\left(\mathsf{hypot.f64}\left(re, im\right)\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\log \left(\mathsf{hypot}\left(re, im\right)\right)} \]
  4. Add Preprocessing
  5. Add Preprocessing

Alternative 2: 27.4% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \log \left(im + \left(re \cdot 0.5\right) \cdot \frac{re}{im}\right) \end{array} \]
(FPCore (re im) :precision binary64 (log (+ im (* (* re 0.5) (/ re im)))))
double code(double re, double im) {
	return log((im + ((re * 0.5) * (re / im))));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log((im + ((re * 0.5d0) * (re / im))))
end function
public static double code(double re, double im) {
	return Math.log((im + ((re * 0.5) * (re / im))));
}
def code(re, im):
	return math.log((im + ((re * 0.5) * (re / im))))
function code(re, im)
	return log(Float64(im + Float64(Float64(re * 0.5) * Float64(re / im))))
end
function tmp = code(re, im)
	tmp = log((im + ((re * 0.5) * (re / im))));
end
code[re_, im_] := N[Log[N[(im + N[(N[(re * 0.5), $MachinePrecision] * N[(re / im), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\log \left(im + \left(re \cdot 0.5\right) \cdot \frac{re}{im}\right)
\end{array}
Derivation
  1. Initial program 51.1%

    \[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
  2. Step-by-step derivation
    1. log-lowering-log.f64N/A

      \[\leadsto \mathsf{log.f64}\left(\left(\sqrt{re \cdot re + im \cdot im}\right)\right) \]
    2. hypot-defineN/A

      \[\leadsto \mathsf{log.f64}\left(\left(\mathsf{hypot}\left(re, im\right)\right)\right) \]
    3. hypot-lowering-hypot.f64100.0%

      \[\leadsto \mathsf{log.f64}\left(\mathsf{hypot.f64}\left(re, im\right)\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\log \left(\mathsf{hypot}\left(re, im\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in re around 0

    \[\leadsto \mathsf{log.f64}\left(\color{blue}{\left(im + \frac{1}{2} \cdot \frac{{re}^{2}}{im}\right)}\right) \]
  6. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \mathsf{log.f64}\left(\left(im + \frac{{re}^{2}}{im} \cdot \frac{1}{2}\right)\right) \]
    2. associate-*l/N/A

      \[\leadsto \mathsf{log.f64}\left(\left(im + \frac{{re}^{2} \cdot \frac{1}{2}}{im}\right)\right) \]
    3. associate-*r/N/A

      \[\leadsto \mathsf{log.f64}\left(\left(im + {re}^{2} \cdot \frac{\frac{1}{2}}{im}\right)\right) \]
    4. metadata-evalN/A

      \[\leadsto \mathsf{log.f64}\left(\left(im + {re}^{2} \cdot \frac{\frac{1}{2} \cdot 1}{im}\right)\right) \]
    5. associate-*r/N/A

      \[\leadsto \mathsf{log.f64}\left(\left(im + {re}^{2} \cdot \left(\frac{1}{2} \cdot \frac{1}{im}\right)\right)\right) \]
    6. +-lowering-+.f64N/A

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \left({re}^{2} \cdot \left(\frac{1}{2} \cdot \frac{1}{im}\right)\right)\right)\right) \]
    7. *-commutativeN/A

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \left(\left(\frac{1}{2} \cdot \frac{1}{im}\right) \cdot {re}^{2}\right)\right)\right) \]
    8. associate-*r/N/A

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \left(\frac{\frac{1}{2} \cdot 1}{im} \cdot {re}^{2}\right)\right)\right) \]
    9. metadata-evalN/A

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \left(\frac{\frac{1}{2}}{im} \cdot {re}^{2}\right)\right)\right) \]
    10. associate-*l/N/A

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \left(\frac{\frac{1}{2} \cdot {re}^{2}}{im}\right)\right)\right) \]
    11. /-lowering-/.f64N/A

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{/.f64}\left(\left(\frac{1}{2} \cdot {re}^{2}\right), im\right)\right)\right) \]
    12. *-lowering-*.f64N/A

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \left({re}^{2}\right)\right), im\right)\right)\right) \]
    13. unpow2N/A

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \left(re \cdot re\right)\right), im\right)\right)\right) \]
    14. *-lowering-*.f6423.3%

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(re, re\right)\right), im\right)\right)\right) \]
  7. Simplified23.3%

    \[\leadsto \log \color{blue}{\left(im + \frac{0.5 \cdot \left(re \cdot re\right)}{im}\right)} \]
  8. Step-by-step derivation
    1. associate-*r*N/A

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \left(\frac{\left(\frac{1}{2} \cdot re\right) \cdot re}{im}\right)\right)\right) \]
    2. associate-/l*N/A

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \left(\left(\frac{1}{2} \cdot re\right) \cdot \frac{re}{im}\right)\right)\right) \]
    3. *-lowering-*.f64N/A

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{*.f64}\left(\left(\frac{1}{2} \cdot re\right), \left(\frac{re}{im}\right)\right)\right)\right) \]
    4. *-lowering-*.f64N/A

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{*.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, re\right), \left(\frac{re}{im}\right)\right)\right)\right) \]
    5. /-lowering-/.f6425.1%

      \[\leadsto \mathsf{log.f64}\left(\mathsf{+.f64}\left(im, \mathsf{*.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, re\right), \mathsf{/.f64}\left(re, im\right)\right)\right)\right) \]
  9. Applied egg-rr25.1%

    \[\leadsto \log \left(im + \color{blue}{\left(0.5 \cdot re\right) \cdot \frac{re}{im}}\right) \]
  10. Final simplification25.1%

    \[\leadsto \log \left(im + \left(re \cdot 0.5\right) \cdot \frac{re}{im}\right) \]
  11. Add Preprocessing

Alternative 3: 27.7% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \log im \end{array} \]
(FPCore (re im) :precision binary64 (log im))
double code(double re, double im) {
	return log(im);
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = log(im)
end function
public static double code(double re, double im) {
	return Math.log(im);
}
def code(re, im):
	return math.log(im)
function code(re, im)
	return log(im)
end
function tmp = code(re, im)
	tmp = log(im);
end
code[re_, im_] := N[Log[im], $MachinePrecision]
\begin{array}{l}

\\
\log im
\end{array}
Derivation
  1. Initial program 51.1%

    \[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
  2. Step-by-step derivation
    1. log-lowering-log.f64N/A

      \[\leadsto \mathsf{log.f64}\left(\left(\sqrt{re \cdot re + im \cdot im}\right)\right) \]
    2. hypot-defineN/A

      \[\leadsto \mathsf{log.f64}\left(\left(\mathsf{hypot}\left(re, im\right)\right)\right) \]
    3. hypot-lowering-hypot.f64100.0%

      \[\leadsto \mathsf{log.f64}\left(\mathsf{hypot.f64}\left(re, im\right)\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\log \left(\mathsf{hypot}\left(re, im\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in re around 0

    \[\leadsto \color{blue}{\log im} \]
  6. Step-by-step derivation
    1. log-lowering-log.f6425.4%

      \[\leadsto \mathsf{log.f64}\left(im\right) \]
  7. Simplified25.4%

    \[\leadsto \color{blue}{\log im} \]
  8. Add Preprocessing

Alternative 4: 2.9% accurate, 29.6× speedup?

\[\begin{array}{l} \\ re \cdot \frac{0.5}{\frac{im}{re}} \end{array} \]
(FPCore (re im) :precision binary64 (* re (/ 0.5 (/ im re))))
double code(double re, double im) {
	return re * (0.5 / (im / re));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = re * (0.5d0 / (im / re))
end function
public static double code(double re, double im) {
	return re * (0.5 / (im / re));
}
def code(re, im):
	return re * (0.5 / (im / re))
function code(re, im)
	return Float64(re * Float64(0.5 / Float64(im / re)))
end
function tmp = code(re, im)
	tmp = re * (0.5 / (im / re));
end
code[re_, im_] := N[(re * N[(0.5 / N[(im / re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
re \cdot \frac{0.5}{\frac{im}{re}}
\end{array}
Derivation
  1. Initial program 51.1%

    \[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
  2. Step-by-step derivation
    1. log-lowering-log.f64N/A

      \[\leadsto \mathsf{log.f64}\left(\left(\sqrt{re \cdot re + im \cdot im}\right)\right) \]
    2. hypot-defineN/A

      \[\leadsto \mathsf{log.f64}\left(\left(\mathsf{hypot}\left(re, im\right)\right)\right) \]
    3. hypot-lowering-hypot.f64100.0%

      \[\leadsto \mathsf{log.f64}\left(\mathsf{hypot.f64}\left(re, im\right)\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\log \left(\mathsf{hypot}\left(re, im\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in re around 0

    \[\leadsto \color{blue}{\log im + \frac{1}{2} \cdot \frac{{re}^{2}}{{im}^{2}}} \]
  6. Step-by-step derivation
    1. +-lowering-+.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\log im, \color{blue}{\left(\frac{1}{2} \cdot \frac{{re}^{2}}{{im}^{2}}\right)}\right) \]
    2. log-lowering-log.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\color{blue}{\frac{1}{2}} \cdot \frac{{re}^{2}}{{im}^{2}}\right)\right) \]
    3. unpow2N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{1}{2} \cdot \frac{{re}^{2}}{im \cdot \color{blue}{im}}\right)\right) \]
    4. associate-/r*N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{1}{2} \cdot \frac{\frac{{re}^{2}}{im}}{\color{blue}{im}}\right)\right) \]
    5. associate-*r/N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{\frac{1}{2} \cdot \frac{{re}^{2}}{im}}{\color{blue}{im}}\right)\right) \]
    6. *-commutativeN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{\frac{{re}^{2}}{im} \cdot \frac{1}{2}}{im}\right)\right) \]
    7. associate-*l/N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{\frac{{re}^{2} \cdot \frac{1}{2}}{im}}{im}\right)\right) \]
    8. associate-*r/N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{{re}^{2} \cdot \frac{\frac{1}{2}}{im}}{im}\right)\right) \]
    9. metadata-evalN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{{re}^{2} \cdot \frac{\frac{1}{2} \cdot 1}{im}}{im}\right)\right) \]
    10. associate-*r/N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{{re}^{2} \cdot \left(\frac{1}{2} \cdot \frac{1}{im}\right)}{im}\right)\right) \]
    11. /-lowering-/.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\left({re}^{2} \cdot \left(\frac{1}{2} \cdot \frac{1}{im}\right)\right), \color{blue}{im}\right)\right) \]
    12. *-commutativeN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\left(\left(\frac{1}{2} \cdot \frac{1}{im}\right) \cdot {re}^{2}\right), im\right)\right) \]
    13. associate-*r/N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\left(\frac{\frac{1}{2} \cdot 1}{im} \cdot {re}^{2}\right), im\right)\right) \]
    14. metadata-evalN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\left(\frac{\frac{1}{2}}{im} \cdot {re}^{2}\right), im\right)\right) \]
    15. associate-*l/N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\left(\frac{\frac{1}{2} \cdot {re}^{2}}{im}\right), im\right)\right) \]
    16. /-lowering-/.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\mathsf{/.f64}\left(\left(\frac{1}{2} \cdot {re}^{2}\right), im\right), im\right)\right) \]
    17. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \left({re}^{2}\right)\right), im\right), im\right)\right) \]
    18. unpow2N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \left(re \cdot re\right)\right), im\right), im\right)\right) \]
    19. *-lowering-*.f6422.4%

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(re, re\right)\right), im\right), im\right)\right) \]
  7. Simplified22.4%

    \[\leadsto \color{blue}{\log im + \frac{\frac{0.5 \cdot \left(re \cdot re\right)}{im}}{im}} \]
  8. Taylor expanded in im around 0

    \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{{re}^{2}}{{im}^{2}}} \]
  9. Step-by-step derivation
    1. associate-*r/N/A

      \[\leadsto \frac{\frac{1}{2} \cdot {re}^{2}}{\color{blue}{{im}^{2}}} \]
    2. /-lowering-/.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\left(\frac{1}{2} \cdot {re}^{2}\right), \color{blue}{\left({im}^{2}\right)}\right) \]
    3. *-lowering-*.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \left({re}^{2}\right)\right), \left({\color{blue}{im}}^{2}\right)\right) \]
    4. unpow2N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \left(re \cdot re\right)\right), \left({im}^{2}\right)\right) \]
    5. *-lowering-*.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(re, re\right)\right), \left({im}^{2}\right)\right) \]
    6. unpow2N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(re, re\right)\right), \left(im \cdot \color{blue}{im}\right)\right) \]
    7. *-lowering-*.f642.6%

      \[\leadsto \mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(re, re\right)\right), \mathsf{*.f64}\left(im, \color{blue}{im}\right)\right) \]
  10. Simplified2.6%

    \[\leadsto \color{blue}{\frac{0.5 \cdot \left(re \cdot re\right)}{im \cdot im}} \]
  11. Step-by-step derivation
    1. pow2N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{{im}^{\color{blue}{2}}} \]
    2. pow-to-expN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\log im \cdot 2}} \]
    3. *-commutativeN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{2 \cdot \log im}} \]
    4. count-2N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\log im + \log im}} \]
    5. flip-+N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{\log im \cdot \log im - \log im \cdot \log im}{\log im - \log im}}} \]
    6. +-inversesN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{0}{\log im - \log im}}} \]
    7. metadata-evalN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{\frac{1}{2} \cdot 0}{\log im - \log im}}} \]
    8. +-inversesN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{\frac{1}{2} \cdot 0}{0}}} \]
    9. associate-*r/N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{1}{2} \cdot \frac{0}{0}}} \]
    10. +-inversesN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{1}{2} \cdot \frac{\log im \cdot \log im - \log im \cdot \log im}{0}}} \]
    11. +-inversesN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{1}{2} \cdot \frac{\log im \cdot \log im - \log im \cdot \log im}{\log im - \log im}}} \]
    12. flip-+N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{1}{2} \cdot \left(\log im + \log im\right)}} \]
    13. distribute-lft-inN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{1}{2} \cdot \log im + \frac{1}{2} \cdot \log im}} \]
    14. distribute-rgt-outN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\log im \cdot \left(\frac{1}{2} + \frac{1}{2}\right)}} \]
    15. metadata-evalN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\log im \cdot 1}} \]
    16. pow-to-expN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{{im}^{\color{blue}{1}}} \]
    17. unpow1N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{im} \]
    18. associate-*r*N/A

      \[\leadsto \frac{\left(\frac{1}{2} \cdot re\right) \cdot re}{im} \]
    19. associate-*r/N/A

      \[\leadsto \left(\frac{1}{2} \cdot re\right) \cdot \color{blue}{\frac{re}{im}} \]
    20. *-commutativeN/A

      \[\leadsto \frac{re}{im} \cdot \color{blue}{\left(\frac{1}{2} \cdot re\right)} \]
    21. div-invN/A

      \[\leadsto \left(re \cdot \frac{1}{im}\right) \cdot \left(\color{blue}{\frac{1}{2}} \cdot re\right) \]
    22. associate-*l*N/A

      \[\leadsto re \cdot \color{blue}{\left(\frac{1}{im} \cdot \left(\frac{1}{2} \cdot re\right)\right)} \]
  12. Applied egg-rr3.1%

    \[\leadsto \color{blue}{\frac{0.5}{\frac{im}{re}} \cdot re} \]
  13. Final simplification3.1%

    \[\leadsto re \cdot \frac{0.5}{\frac{im}{re}} \]
  14. Add Preprocessing

Alternative 5: 2.9% accurate, 29.6× speedup?

\[\begin{array}{l} \\ \frac{0.5}{im} \cdot \left(re \cdot re\right) \end{array} \]
(FPCore (re im) :precision binary64 (* (/ 0.5 im) (* re re)))
double code(double re, double im) {
	return (0.5 / im) * (re * re);
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (0.5d0 / im) * (re * re)
end function
public static double code(double re, double im) {
	return (0.5 / im) * (re * re);
}
def code(re, im):
	return (0.5 / im) * (re * re)
function code(re, im)
	return Float64(Float64(0.5 / im) * Float64(re * re))
end
function tmp = code(re, im)
	tmp = (0.5 / im) * (re * re);
end
code[re_, im_] := N[(N[(0.5 / im), $MachinePrecision] * N[(re * re), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{0.5}{im} \cdot \left(re \cdot re\right)
\end{array}
Derivation
  1. Initial program 51.1%

    \[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
  2. Step-by-step derivation
    1. log-lowering-log.f64N/A

      \[\leadsto \mathsf{log.f64}\left(\left(\sqrt{re \cdot re + im \cdot im}\right)\right) \]
    2. hypot-defineN/A

      \[\leadsto \mathsf{log.f64}\left(\left(\mathsf{hypot}\left(re, im\right)\right)\right) \]
    3. hypot-lowering-hypot.f64100.0%

      \[\leadsto \mathsf{log.f64}\left(\mathsf{hypot.f64}\left(re, im\right)\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\log \left(\mathsf{hypot}\left(re, im\right)\right)} \]
  4. Add Preprocessing
  5. Taylor expanded in re around 0

    \[\leadsto \color{blue}{\log im + \frac{1}{2} \cdot \frac{{re}^{2}}{{im}^{2}}} \]
  6. Step-by-step derivation
    1. +-lowering-+.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\log im, \color{blue}{\left(\frac{1}{2} \cdot \frac{{re}^{2}}{{im}^{2}}\right)}\right) \]
    2. log-lowering-log.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\color{blue}{\frac{1}{2}} \cdot \frac{{re}^{2}}{{im}^{2}}\right)\right) \]
    3. unpow2N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{1}{2} \cdot \frac{{re}^{2}}{im \cdot \color{blue}{im}}\right)\right) \]
    4. associate-/r*N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{1}{2} \cdot \frac{\frac{{re}^{2}}{im}}{\color{blue}{im}}\right)\right) \]
    5. associate-*r/N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{\frac{1}{2} \cdot \frac{{re}^{2}}{im}}{\color{blue}{im}}\right)\right) \]
    6. *-commutativeN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{\frac{{re}^{2}}{im} \cdot \frac{1}{2}}{im}\right)\right) \]
    7. associate-*l/N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{\frac{{re}^{2} \cdot \frac{1}{2}}{im}}{im}\right)\right) \]
    8. associate-*r/N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{{re}^{2} \cdot \frac{\frac{1}{2}}{im}}{im}\right)\right) \]
    9. metadata-evalN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{{re}^{2} \cdot \frac{\frac{1}{2} \cdot 1}{im}}{im}\right)\right) \]
    10. associate-*r/N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \left(\frac{{re}^{2} \cdot \left(\frac{1}{2} \cdot \frac{1}{im}\right)}{im}\right)\right) \]
    11. /-lowering-/.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\left({re}^{2} \cdot \left(\frac{1}{2} \cdot \frac{1}{im}\right)\right), \color{blue}{im}\right)\right) \]
    12. *-commutativeN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\left(\left(\frac{1}{2} \cdot \frac{1}{im}\right) \cdot {re}^{2}\right), im\right)\right) \]
    13. associate-*r/N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\left(\frac{\frac{1}{2} \cdot 1}{im} \cdot {re}^{2}\right), im\right)\right) \]
    14. metadata-evalN/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\left(\frac{\frac{1}{2}}{im} \cdot {re}^{2}\right), im\right)\right) \]
    15. associate-*l/N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\left(\frac{\frac{1}{2} \cdot {re}^{2}}{im}\right), im\right)\right) \]
    16. /-lowering-/.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\mathsf{/.f64}\left(\left(\frac{1}{2} \cdot {re}^{2}\right), im\right), im\right)\right) \]
    17. *-lowering-*.f64N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \left({re}^{2}\right)\right), im\right), im\right)\right) \]
    18. unpow2N/A

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \left(re \cdot re\right)\right), im\right), im\right)\right) \]
    19. *-lowering-*.f6422.4%

      \[\leadsto \mathsf{+.f64}\left(\mathsf{log.f64}\left(im\right), \mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(re, re\right)\right), im\right), im\right)\right) \]
  7. Simplified22.4%

    \[\leadsto \color{blue}{\log im + \frac{\frac{0.5 \cdot \left(re \cdot re\right)}{im}}{im}} \]
  8. Taylor expanded in im around 0

    \[\leadsto \color{blue}{\frac{1}{2} \cdot \frac{{re}^{2}}{{im}^{2}}} \]
  9. Step-by-step derivation
    1. associate-*r/N/A

      \[\leadsto \frac{\frac{1}{2} \cdot {re}^{2}}{\color{blue}{{im}^{2}}} \]
    2. /-lowering-/.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\left(\frac{1}{2} \cdot {re}^{2}\right), \color{blue}{\left({im}^{2}\right)}\right) \]
    3. *-lowering-*.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \left({re}^{2}\right)\right), \left({\color{blue}{im}}^{2}\right)\right) \]
    4. unpow2N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \left(re \cdot re\right)\right), \left({im}^{2}\right)\right) \]
    5. *-lowering-*.f64N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(re, re\right)\right), \left({im}^{2}\right)\right) \]
    6. unpow2N/A

      \[\leadsto \mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(re, re\right)\right), \left(im \cdot \color{blue}{im}\right)\right) \]
    7. *-lowering-*.f642.6%

      \[\leadsto \mathsf{/.f64}\left(\mathsf{*.f64}\left(\frac{1}{2}, \mathsf{*.f64}\left(re, re\right)\right), \mathsf{*.f64}\left(im, \color{blue}{im}\right)\right) \]
  10. Simplified2.6%

    \[\leadsto \color{blue}{\frac{0.5 \cdot \left(re \cdot re\right)}{im \cdot im}} \]
  11. Step-by-step derivation
    1. pow2N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{{im}^{\color{blue}{2}}} \]
    2. pow-to-expN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\log im \cdot 2}} \]
    3. *-commutativeN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{2 \cdot \log im}} \]
    4. count-2N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\log im + \log im}} \]
    5. flip-+N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{\log im \cdot \log im - \log im \cdot \log im}{\log im - \log im}}} \]
    6. +-inversesN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{0}{\log im - \log im}}} \]
    7. metadata-evalN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{\frac{1}{2} \cdot 0}{\log im - \log im}}} \]
    8. +-inversesN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{\frac{1}{2} \cdot 0}{0}}} \]
    9. associate-*r/N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{1}{2} \cdot \frac{0}{0}}} \]
    10. +-inversesN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{1}{2} \cdot \frac{\log im \cdot \log im - \log im \cdot \log im}{0}}} \]
    11. +-inversesN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{1}{2} \cdot \frac{\log im \cdot \log im - \log im \cdot \log im}{\log im - \log im}}} \]
    12. flip-+N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{1}{2} \cdot \left(\log im + \log im\right)}} \]
    13. distribute-lft-inN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\frac{1}{2} \cdot \log im + \frac{1}{2} \cdot \log im}} \]
    14. distribute-rgt-outN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\log im \cdot \left(\frac{1}{2} + \frac{1}{2}\right)}} \]
    15. metadata-evalN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{e^{\log im \cdot 1}} \]
    16. pow-to-expN/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{{im}^{\color{blue}{1}}} \]
    17. unpow1N/A

      \[\leadsto \frac{\frac{1}{2} \cdot \left(re \cdot re\right)}{im} \]
    18. associate-*l/N/A

      \[\leadsto \frac{\frac{1}{2}}{im} \cdot \color{blue}{\left(re \cdot re\right)} \]
    19. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\left(\frac{\frac{1}{2}}{im}\right), \color{blue}{\left(re \cdot re\right)}\right) \]
    20. /-lowering-/.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{1}{2}, im\right), \left(\color{blue}{re} \cdot re\right)\right) \]
    21. *-lowering-*.f643.0%

      \[\leadsto \mathsf{*.f64}\left(\mathsf{/.f64}\left(\frac{1}{2}, im\right), \mathsf{*.f64}\left(re, \color{blue}{re}\right)\right) \]
  12. Applied egg-rr3.0%

    \[\leadsto \color{blue}{\frac{0.5}{im} \cdot \left(re \cdot re\right)} \]
  13. Add Preprocessing

Alternative 6: 0.0% accurate, 69.0× speedup?

\[\begin{array}{l} \\ \frac{0}{0} \end{array} \]
(FPCore (re im) :precision binary64 (/ 0.0 0.0))
double code(double re, double im) {
	return 0.0 / 0.0;
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = 0.0d0 / 0.0d0
end function
public static double code(double re, double im) {
	return 0.0 / 0.0;
}
def code(re, im):
	return 0.0 / 0.0
function code(re, im)
	return Float64(0.0 / 0.0)
end
function tmp = code(re, im)
	tmp = 0.0 / 0.0;
end
code[re_, im_] := N[(0.0 / 0.0), $MachinePrecision]
\begin{array}{l}

\\
\frac{0}{0}
\end{array}
Derivation
  1. Initial program 51.1%

    \[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
  2. Step-by-step derivation
    1. log-lowering-log.f64N/A

      \[\leadsto \mathsf{log.f64}\left(\left(\sqrt{re \cdot re + im \cdot im}\right)\right) \]
    2. hypot-defineN/A

      \[\leadsto \mathsf{log.f64}\left(\left(\mathsf{hypot}\left(re, im\right)\right)\right) \]
    3. hypot-lowering-hypot.f64100.0%

      \[\leadsto \mathsf{log.f64}\left(\mathsf{hypot.f64}\left(re, im\right)\right) \]
  3. Simplified100.0%

    \[\leadsto \color{blue}{\log \left(\mathsf{hypot}\left(re, im\right)\right)} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. pow1/2N/A

      \[\leadsto \log \left({\left(re \cdot re + im \cdot im\right)}^{\frac{1}{2}}\right) \]
    2. pow-to-expN/A

      \[\leadsto \log \left(e^{\log \left(re \cdot re + im \cdot im\right) \cdot \frac{1}{2}}\right) \]
    3. rem-log-expN/A

      \[\leadsto \log \left(re \cdot re + im \cdot im\right) \cdot \color{blue}{\frac{1}{2}} \]
    4. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\log \left(re \cdot re + im \cdot im\right), \color{blue}{\frac{1}{2}}\right) \]
    5. log-lowering-log.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{log.f64}\left(\left(re \cdot re + im \cdot im\right)\right), \frac{1}{2}\right) \]
    6. +-lowering-+.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{log.f64}\left(\mathsf{+.f64}\left(\left(re \cdot re\right), \left(im \cdot im\right)\right)\right), \frac{1}{2}\right) \]
    7. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{log.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(re, re\right), \left(im \cdot im\right)\right)\right), \frac{1}{2}\right) \]
    8. *-lowering-*.f6451.1%

      \[\leadsto \mathsf{*.f64}\left(\mathsf{log.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(re, re\right), \mathsf{*.f64}\left(im, im\right)\right)\right), \frac{1}{2}\right) \]
  6. Applied egg-rr51.1%

    \[\leadsto \color{blue}{\log \left(re \cdot re + im \cdot im\right) \cdot 0.5} \]
  7. Taylor expanded in re around 0

    \[\leadsto \mathsf{*.f64}\left(\color{blue}{\log \left({im}^{2}\right)}, \frac{1}{2}\right) \]
  8. Step-by-step derivation
    1. log-lowering-log.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{log.f64}\left(\left({im}^{2}\right)\right), \frac{1}{2}\right) \]
    2. unpow2N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{log.f64}\left(\left(im \cdot im\right)\right), \frac{1}{2}\right) \]
    3. *-lowering-*.f6425.1%

      \[\leadsto \mathsf{*.f64}\left(\mathsf{log.f64}\left(\mathsf{*.f64}\left(im, im\right)\right), \frac{1}{2}\right) \]
  9. Simplified25.1%

    \[\leadsto \color{blue}{\log \left(im \cdot im\right)} \cdot 0.5 \]
  10. Step-by-step derivation
    1. log-prodN/A

      \[\leadsto \left(\log im + \log im\right) \cdot \frac{1}{2} \]
    2. flip-+N/A

      \[\leadsto \frac{\log im \cdot \log im - \log im \cdot \log im}{\log im - \log im} \cdot \frac{1}{2} \]
    3. +-inversesN/A

      \[\leadsto \frac{0}{\log im - \log im} \cdot \frac{1}{2} \]
    4. +-inversesN/A

      \[\leadsto \frac{0}{0} \cdot \frac{1}{2} \]
    5. associate-*l/N/A

      \[\leadsto \frac{0 \cdot \frac{1}{2}}{\color{blue}{0}} \]
    6. metadata-evalN/A

      \[\leadsto \frac{0}{0} \]
    7. /-lowering-/.f640.0%

      \[\leadsto \mathsf{/.f64}\left(0, \color{blue}{0}\right) \]
  11. Applied egg-rr0.0%

    \[\leadsto \color{blue}{\frac{0}{0}} \]
  12. Add Preprocessing

Reproduce

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