math.log/1 on complex, real part

Percentage Accurate: 52.8% → 100.0%
Time: 1.3s
Alternatives: 1
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 1 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: 52.8% 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 52.3%

    \[\log \left(\sqrt{re \cdot re + im \cdot im}\right) \]
  2. Step-by-step derivation
    1. hypot-def100.0%

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

    \[\leadsto \color{blue}{\log \left(\mathsf{hypot}\left(re, im\right)\right)} \]
  4. Final simplification100.0%

    \[\leadsto \log \left(\mathsf{hypot}\left(re, im\right)\right) \]

Reproduce

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