math.log/2 on complex, imaginary part

Percentage Accurate: 49.3% → 99.5%
Time: 8.7s
Alternatives: 4
Speedup: 3.0×

Specification

?
\[\begin{array}{l} \\ \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \end{array} \]
(FPCore (re im base)
 :precision binary64
 (/
  (- (* (atan2 im re) (log base)) (* (log (sqrt (+ (* re re) (* im im)))) 0.0))
  (+ (* (log base) (log base)) (* 0.0 0.0))))
double code(double re, double im, double base) {
	return ((atan2(im, re) * log(base)) - (log(sqrt(((re * re) + (im * im)))) * 0.0)) / ((log(base) * log(base)) + (0.0 * 0.0));
}
real(8) function code(re, im, base)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8), intent (in) :: base
    code = ((atan2(im, re) * log(base)) - (log(sqrt(((re * re) + (im * im)))) * 0.0d0)) / ((log(base) * log(base)) + (0.0d0 * 0.0d0))
end function
public static double code(double re, double im, double base) {
	return ((Math.atan2(im, re) * Math.log(base)) - (Math.log(Math.sqrt(((re * re) + (im * im)))) * 0.0)) / ((Math.log(base) * Math.log(base)) + (0.0 * 0.0));
}
def code(re, im, base):
	return ((math.atan2(im, re) * math.log(base)) - (math.log(math.sqrt(((re * re) + (im * im)))) * 0.0)) / ((math.log(base) * math.log(base)) + (0.0 * 0.0))
function code(re, im, base)
	return Float64(Float64(Float64(atan(im, re) * log(base)) - Float64(log(sqrt(Float64(Float64(re * re) + Float64(im * im)))) * 0.0)) / Float64(Float64(log(base) * log(base)) + Float64(0.0 * 0.0)))
end
function tmp = code(re, im, base)
	tmp = ((atan2(im, re) * log(base)) - (log(sqrt(((re * re) + (im * im)))) * 0.0)) / ((log(base) * log(base)) + (0.0 * 0.0));
end
code[re_, im_, base_] := N[(N[(N[(N[ArcTan[im / re], $MachinePrecision] * N[Log[base], $MachinePrecision]), $MachinePrecision] - N[(N[Log[N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * 0.0), $MachinePrecision]), $MachinePrecision] / N[(N[(N[Log[base], $MachinePrecision] * N[Log[base], $MachinePrecision]), $MachinePrecision] + N[(0.0 * 0.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot 0}{\log base \cdot \log base + 0 \cdot 0}
\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 4 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: 49.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \end{array} \]
(FPCore (re im base)
 :precision binary64
 (/
  (- (* (atan2 im re) (log base)) (* (log (sqrt (+ (* re re) (* im im)))) 0.0))
  (+ (* (log base) (log base)) (* 0.0 0.0))))
double code(double re, double im, double base) {
	return ((atan2(im, re) * log(base)) - (log(sqrt(((re * re) + (im * im)))) * 0.0)) / ((log(base) * log(base)) + (0.0 * 0.0));
}
real(8) function code(re, im, base)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8), intent (in) :: base
    code = ((atan2(im, re) * log(base)) - (log(sqrt(((re * re) + (im * im)))) * 0.0d0)) / ((log(base) * log(base)) + (0.0d0 * 0.0d0))
end function
public static double code(double re, double im, double base) {
	return ((Math.atan2(im, re) * Math.log(base)) - (Math.log(Math.sqrt(((re * re) + (im * im)))) * 0.0)) / ((Math.log(base) * Math.log(base)) + (0.0 * 0.0));
}
def code(re, im, base):
	return ((math.atan2(im, re) * math.log(base)) - (math.log(math.sqrt(((re * re) + (im * im)))) * 0.0)) / ((math.log(base) * math.log(base)) + (0.0 * 0.0))
function code(re, im, base)
	return Float64(Float64(Float64(atan(im, re) * log(base)) - Float64(log(sqrt(Float64(Float64(re * re) + Float64(im * im)))) * 0.0)) / Float64(Float64(log(base) * log(base)) + Float64(0.0 * 0.0)))
end
function tmp = code(re, im, base)
	tmp = ((atan2(im, re) * log(base)) - (log(sqrt(((re * re) + (im * im)))) * 0.0)) / ((log(base) * log(base)) + (0.0 * 0.0));
end
code[re_, im_, base_] := N[(N[(N[(N[ArcTan[im / re], $MachinePrecision] * N[Log[base], $MachinePrecision]), $MachinePrecision] - N[(N[Log[N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * 0.0), $MachinePrecision]), $MachinePrecision] / N[(N[(N[Log[base], $MachinePrecision] * N[Log[base], $MachinePrecision]), $MachinePrecision] + N[(0.0 * 0.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot 0}{\log base \cdot \log base + 0 \cdot 0}
\end{array}

Alternative 1: 99.5% accurate, 3.0× speedup?

\[\begin{array}{l} \\ \frac{\tan^{-1}_* \frac{im}{re}}{\log base} \end{array} \]
(FPCore (re im base) :precision binary64 (/ (atan2 im re) (log base)))
double code(double re, double im, double base) {
	return atan2(im, re) / log(base);
}
real(8) function code(re, im, base)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8), intent (in) :: base
    code = atan2(im, re) / log(base)
end function
public static double code(double re, double im, double base) {
	return Math.atan2(im, re) / Math.log(base);
}
def code(re, im, base):
	return math.atan2(im, re) / math.log(base)
function code(re, im, base)
	return Float64(atan(im, re) / log(base))
end
function tmp = code(re, im, base)
	tmp = atan2(im, re) / log(base);
end
code[re_, im_, base_] := N[(N[ArcTan[im / re], $MachinePrecision] / N[Log[base], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\tan^{-1}_* \frac{im}{re}}{\log base}
\end{array}
Derivation
  1. Initial program 47.0%

    \[\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \]
  2. Step-by-step derivation
    1. mul0-rgt99.3%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \color{blue}{0}}{\log base \cdot \log base + 0 \cdot 0} \]
    2. div-sub99.3%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0} - \frac{0}{\log base \cdot \log base + 0 \cdot 0}} \]
    3. div099.3%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0} - \color{blue}{0} \]
    4. --rgt-identity99.3%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0}} \]
    5. metadata-eval99.3%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + \color{blue}{0}} \]
    6. +-rgt-identity99.3%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\color{blue}{\log base \cdot \log base}} \]
    7. times-frac99.6%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base} \cdot \frac{\log base}{\log base}} \]
    8. *-inverses99.6%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re}}{\log base} \cdot \color{blue}{1} \]
    9. *-rgt-identity99.6%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base}} \]
  3. Simplified99.6%

    \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base}} \]
  4. Final simplification99.6%

    \[\leadsto \frac{\tan^{-1}_* \frac{im}{re}}{\log base} \]

Alternative 2: 13.5% accurate, 203.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;re \leq 5.1 \cdot 10^{+21}:\\ \;\;\;\;-0.25\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \end{array} \]
(FPCore (re im base) :precision binary64 (if (<= re 5.1e+21) -0.25 0.0))
double code(double re, double im, double base) {
	double tmp;
	if (re <= 5.1e+21) {
		tmp = -0.25;
	} else {
		tmp = 0.0;
	}
	return tmp;
}
real(8) function code(re, im, base)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8), intent (in) :: base
    real(8) :: tmp
    if (re <= 5.1d+21) then
        tmp = -0.25d0
    else
        tmp = 0.0d0
    end if
    code = tmp
end function
public static double code(double re, double im, double base) {
	double tmp;
	if (re <= 5.1e+21) {
		tmp = -0.25;
	} else {
		tmp = 0.0;
	}
	return tmp;
}
def code(re, im, base):
	tmp = 0
	if re <= 5.1e+21:
		tmp = -0.25
	else:
		tmp = 0.0
	return tmp
function code(re, im, base)
	tmp = 0.0
	if (re <= 5.1e+21)
		tmp = -0.25;
	else
		tmp = 0.0;
	end
	return tmp
end
function tmp_2 = code(re, im, base)
	tmp = 0.0;
	if (re <= 5.1e+21)
		tmp = -0.25;
	else
		tmp = 0.0;
	end
	tmp_2 = tmp;
end
code[re_, im_, base_] := If[LessEqual[re, 5.1e+21], -0.25, 0.0]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;re \leq 5.1 \cdot 10^{+21}:\\
\;\;\;\;-0.25\\

\mathbf{else}:\\
\;\;\;\;0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if re < 5.1e21

    1. Initial program 52.8%

      \[\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \]
    2. Step-by-step derivation
      1. mul0-rgt99.3%

        \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \color{blue}{0}}{\log base \cdot \log base + 0 \cdot 0} \]
      2. div-sub99.3%

        \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0} - \frac{0}{\log base \cdot \log base + 0 \cdot 0}} \]
      3. div099.3%

        \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0} - \color{blue}{0} \]
      4. --rgt-identity99.3%

        \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0}} \]
      5. metadata-eval99.3%

        \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + \color{blue}{0}} \]
      6. +-rgt-identity99.3%

        \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\color{blue}{\log base \cdot \log base}} \]
      7. times-frac99.5%

        \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base} \cdot \frac{\log base}{\log base}} \]
      8. *-inverses99.5%

        \[\leadsto \frac{\tan^{-1}_* \frac{im}{re}}{\log base} \cdot \color{blue}{1} \]
      9. *-rgt-identity99.5%

        \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base}} \]
    3. Simplified99.5%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base}} \]
    4. Applied egg-rr9.0%

      \[\leadsto \color{blue}{-0.25} \]

    if 5.1e21 < re

    1. Initial program 28.9%

      \[\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \]
    2. Step-by-step derivation
      1. mul0-rgt99.5%

        \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \color{blue}{0}}{\log base \cdot \log base + 0 \cdot 0} \]
      2. div-sub99.5%

        \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0} - \frac{0}{\log base \cdot \log base + 0 \cdot 0}} \]
      3. div099.5%

        \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0} - \color{blue}{0} \]
      4. --rgt-identity99.5%

        \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0}} \]
      5. metadata-eval99.5%

        \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + \color{blue}{0}} \]
      6. +-rgt-identity99.5%

        \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\color{blue}{\log base \cdot \log base}} \]
      7. times-frac99.7%

        \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base} \cdot \frac{\log base}{\log base}} \]
      8. *-inverses99.7%

        \[\leadsto \frac{\tan^{-1}_* \frac{im}{re}}{\log base} \cdot \color{blue}{1} \]
      9. *-rgt-identity99.7%

        \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base}} \]
    3. Simplified99.7%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base}} \]
    4. Applied egg-rr35.8%

      \[\leadsto \color{blue}{0} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification15.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;re \leq 5.1 \cdot 10^{+21}:\\ \;\;\;\;-0.25\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array} \]

Alternative 3: 7.1% accurate, 622.0× speedup?

\[\begin{array}{l} \\ -0.5 \end{array} \]
(FPCore (re im base) :precision binary64 -0.5)
double code(double re, double im, double base) {
	return -0.5;
}
real(8) function code(re, im, base)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8), intent (in) :: base
    code = -0.5d0
end function
public static double code(double re, double im, double base) {
	return -0.5;
}
def code(re, im, base):
	return -0.5
function code(re, im, base)
	return -0.5
end
function tmp = code(re, im, base)
	tmp = -0.5;
end
code[re_, im_, base_] := -0.5
\begin{array}{l}

\\
-0.5
\end{array}
Derivation
  1. Initial program 47.0%

    \[\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \]
  2. Step-by-step derivation
    1. mul0-rgt99.3%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \color{blue}{0}}{\log base \cdot \log base + 0 \cdot 0} \]
    2. div-sub99.3%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0} - \frac{0}{\log base \cdot \log base + 0 \cdot 0}} \]
    3. div099.3%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0} - \color{blue}{0} \]
    4. --rgt-identity99.3%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0}} \]
    5. metadata-eval99.3%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + \color{blue}{0}} \]
    6. +-rgt-identity99.3%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\color{blue}{\log base \cdot \log base}} \]
    7. times-frac99.6%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base} \cdot \frac{\log base}{\log base}} \]
    8. *-inverses99.6%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re}}{\log base} \cdot \color{blue}{1} \]
    9. *-rgt-identity99.6%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base}} \]
  3. Simplified99.6%

    \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base}} \]
  4. Applied egg-rr7.9%

    \[\leadsto \color{blue}{-0.5} \]
  5. Final simplification7.9%

    \[\leadsto -0.5 \]

Alternative 4: 7.3% accurate, 622.0× speedup?

\[\begin{array}{l} \\ -0.25 \end{array} \]
(FPCore (re im base) :precision binary64 -0.25)
double code(double re, double im, double base) {
	return -0.25;
}
real(8) function code(re, im, base)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8), intent (in) :: base
    code = -0.25d0
end function
public static double code(double re, double im, double base) {
	return -0.25;
}
def code(re, im, base):
	return -0.25
function code(re, im, base)
	return -0.25
end
function tmp = code(re, im, base)
	tmp = -0.25;
end
code[re_, im_, base_] := -0.25
\begin{array}{l}

\\
-0.25
\end{array}
Derivation
  1. Initial program 47.0%

    \[\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \]
  2. Step-by-step derivation
    1. mul0-rgt99.3%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base - \color{blue}{0}}{\log base \cdot \log base + 0 \cdot 0} \]
    2. div-sub99.3%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0} - \frac{0}{\log base \cdot \log base + 0 \cdot 0}} \]
    3. div099.3%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0} - \color{blue}{0} \]
    4. --rgt-identity99.3%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + 0 \cdot 0}} \]
    5. metadata-eval99.3%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\log base \cdot \log base + \color{blue}{0}} \]
    6. +-rgt-identity99.3%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re} \cdot \log base}{\color{blue}{\log base \cdot \log base}} \]
    7. times-frac99.6%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base} \cdot \frac{\log base}{\log base}} \]
    8. *-inverses99.6%

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re}}{\log base} \cdot \color{blue}{1} \]
    9. *-rgt-identity99.6%

      \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base}} \]
  3. Simplified99.6%

    \[\leadsto \color{blue}{\frac{\tan^{-1}_* \frac{im}{re}}{\log base}} \]
  4. Applied egg-rr8.0%

    \[\leadsto \color{blue}{-0.25} \]
  5. Final simplification8.0%

    \[\leadsto -0.25 \]

Reproduce

?
herbie shell --seed 2023297 
(FPCore (re im base)
  :name "math.log/2 on complex, imaginary part"
  :precision binary64
  (/ (- (* (atan2 im re) (log base)) (* (log (sqrt (+ (* re re) (* im im)))) 0.0)) (+ (* (log base) (log base)) (* 0.0 0.0))))