math.log/2 on complex, real part

Percentage Accurate: 52.8% → 99.1%
Time: 8.1s
Alternatives: 3
Speedup: 2.7×

Specification

?
\[\begin{array}{l} \\ \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \end{array} \]
(FPCore (re im base)
 :precision binary64
 (/
  (+ (* (log (sqrt (+ (* re re) (* im im)))) (log base)) (* (atan2 im re) 0.0))
  (+ (* (log base) (log base)) (* 0.0 0.0))))
double code(double re, double im, double base) {
	return ((log(sqrt(((re * re) + (im * im)))) * log(base)) + (atan2(im, re) * 0.0)) / ((log(base) * log(base)) + (0.0 * 0.0));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(re, im, base)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    real(8), intent (in) :: base
    code = ((log(sqrt(((re * re) + (im * im)))) * log(base)) + (atan2(im, re) * 0.0d0)) / ((log(base) * log(base)) + (0.0d0 * 0.0d0))
end function
public static double code(double re, double im, double base) {
	return ((Math.log(Math.sqrt(((re * re) + (im * im)))) * Math.log(base)) + (Math.atan2(im, re) * 0.0)) / ((Math.log(base) * Math.log(base)) + (0.0 * 0.0));
}
def code(re, im, base):
	return ((math.log(math.sqrt(((re * re) + (im * im)))) * math.log(base)) + (math.atan2(im, re) * 0.0)) / ((math.log(base) * math.log(base)) + (0.0 * 0.0))
function code(re, im, base)
	return Float64(Float64(Float64(log(sqrt(Float64(Float64(re * re) + Float64(im * im)))) * log(base)) + Float64(atan(im, re) * 0.0)) / Float64(Float64(log(base) * log(base)) + Float64(0.0 * 0.0)))
end
function tmp = code(re, im, base)
	tmp = ((log(sqrt(((re * re) + (im * im)))) * log(base)) + (atan2(im, re) * 0.0)) / ((log(base) * log(base)) + (0.0 * 0.0));
end
code[re_, im_, base_] := N[(N[(N[(N[Log[N[Sqrt[N[(N[(re * re), $MachinePrecision] + N[(im * im), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * N[Log[base], $MachinePrecision]), $MachinePrecision] + N[(N[ArcTan[im / re], $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{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \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 3 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} \\ \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \end{array} \]
(FPCore (re im base)
 :precision binary64
 (/
  (+ (* (log (sqrt (+ (* re re) (* im im)))) (log base)) (* (atan2 im re) 0.0))
  (+ (* (log base) (log base)) (* 0.0 0.0))))
double code(double re, double im, double base) {
	return ((log(sqrt(((re * re) + (im * im)))) * log(base)) + (atan2(im, re) * 0.0)) / ((log(base) * log(base)) + (0.0 * 0.0));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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

Alternative 1: 99.1% accurate, 2.3× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ re_m = \left|re\right| \\ [re_m, im_m, base] = \mathsf{sort}([re_m, im_m, base])\\ \\ \frac{\mathsf{fma}\left(\frac{\frac{0.5 \cdot re\_m}{im\_m}}{im\_m}, re\_m, \log im\_m\right)}{\log base} \end{array} \]
im_m = (fabs.f64 im)
re_m = (fabs.f64 re)
NOTE: re_m, im_m, and base should be sorted in increasing order before calling this function.
(FPCore (re_m im_m base)
 :precision binary64
 (/ (fma (/ (/ (* 0.5 re_m) im_m) im_m) re_m (log im_m)) (log base)))
im_m = fabs(im);
re_m = fabs(re);
assert(re_m < im_m && im_m < base);
double code(double re_m, double im_m, double base) {
	return fma((((0.5 * re_m) / im_m) / im_m), re_m, log(im_m)) / log(base);
}
im_m = abs(im)
re_m = abs(re)
re_m, im_m, base = sort([re_m, im_m, base])
function code(re_m, im_m, base)
	return Float64(fma(Float64(Float64(Float64(0.5 * re_m) / im_m) / im_m), re_m, log(im_m)) / log(base))
end
im_m = N[Abs[im], $MachinePrecision]
re_m = N[Abs[re], $MachinePrecision]
NOTE: re_m, im_m, and base should be sorted in increasing order before calling this function.
code[re$95$m_, im$95$m_, base_] := N[(N[(N[(N[(N[(0.5 * re$95$m), $MachinePrecision] / im$95$m), $MachinePrecision] / im$95$m), $MachinePrecision] * re$95$m + N[Log[im$95$m], $MachinePrecision]), $MachinePrecision] / N[Log[base], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
re_m = \left|re\right|
\\
[re_m, im_m, base] = \mathsf{sort}([re_m, im_m, base])\\
\\
\frac{\mathsf{fma}\left(\frac{\frac{0.5 \cdot re\_m}{im\_m}}{im\_m}, re\_m, \log im\_m\right)}{\log base}
\end{array}
Derivation
  1. Initial program 49.4%

    \[\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift-+.f64N/A

      \[\leadsto \frac{\color{blue}{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \cdot 0}}{\log base \cdot \log base + 0 \cdot 0} \]
    2. lift-*.f64N/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \color{blue}{\tan^{-1}_* \frac{im}{re} \cdot 0}}{\log base \cdot \log base + 0 \cdot 0} \]
    3. mul0-rgtN/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \color{blue}{0}}{\log base \cdot \log base + 0 \cdot 0} \]
    4. +-rgt-identity49.4

      \[\leadsto \frac{\color{blue}{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}}{\log base \cdot \log base + 0 \cdot 0} \]
    5. lift-+.f64N/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\color{blue}{\log base \cdot \log base + 0 \cdot 0}} \]
    6. lift-*.f64N/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base + \color{blue}{0 \cdot 0}} \]
    7. metadata-evalN/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base + \color{blue}{0}} \]
    8. +-rgt-identity49.4

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\color{blue}{\log base \cdot \log base}} \]
    9. lower-/.f64N/A

      \[\leadsto \color{blue}{\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base}} \]
    10. +-rgt-identityN/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\color{blue}{\log base \cdot \log base + 0}} \]
    11. metadata-evalN/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base + \color{blue}{0 \cdot 0}} \]
    12. lift-*.f64N/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base + \color{blue}{0 \cdot 0}} \]
    13. lift-+.f64N/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\color{blue}{\log base \cdot \log base + 0 \cdot 0}} \]
    14. lift-+.f64N/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\color{blue}{\log base \cdot \log base + 0 \cdot 0}} \]
    15. lift-*.f64N/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base + \color{blue}{0 \cdot 0}} \]
    16. metadata-evalN/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base + \color{blue}{0}} \]
    17. +-rgt-identityN/A

      \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\color{blue}{\log base \cdot \log base}} \]
  4. Applied rewrites99.4%

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

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

      \[\leadsto \frac{\color{blue}{\frac{1}{2} \cdot \frac{{re}^{2}}{{im}^{2}} + \log im}}{\log base} \]
    2. associate-*r/N/A

      \[\leadsto \frac{\color{blue}{\frac{\frac{1}{2} \cdot {re}^{2}}{{im}^{2}}} + \log im}{\log base} \]
    3. associate-*l/N/A

      \[\leadsto \frac{\color{blue}{\frac{\frac{1}{2}}{{im}^{2}} \cdot {re}^{2}} + \log im}{\log base} \]
    4. metadata-evalN/A

      \[\leadsto \frac{\frac{\color{blue}{\frac{1}{2} \cdot 1}}{{im}^{2}} \cdot {re}^{2} + \log im}{\log base} \]
    5. associate-*r/N/A

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

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

      \[\leadsto \frac{\color{blue}{\left(\left(\frac{1}{2} \cdot \frac{1}{{im}^{2}}\right) \cdot re\right) \cdot re} + \log im}{\log base} \]
    8. lower-fma.f64N/A

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\left(\frac{1}{2} \cdot \frac{1}{{im}^{2}}\right) \cdot re, re, \log im\right)}}{\log base} \]
    9. lower-*.f64N/A

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

      \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{\frac{1}{2} \cdot 1}{{im}^{2}}} \cdot re, re, \log im\right)}{\log base} \]
    11. metadata-evalN/A

      \[\leadsto \frac{\mathsf{fma}\left(\frac{\color{blue}{\frac{1}{2}}}{{im}^{2}} \cdot re, re, \log im\right)}{\log base} \]
    12. lower-/.f64N/A

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

      \[\leadsto \frac{\mathsf{fma}\left(\frac{\frac{1}{2}}{\color{blue}{im \cdot im}} \cdot re, re, \log im\right)}{\log base} \]
    14. lower-*.f64N/A

      \[\leadsto \frac{\mathsf{fma}\left(\frac{\frac{1}{2}}{\color{blue}{im \cdot im}} \cdot re, re, \log im\right)}{\log base} \]
    15. lower-log.f6428.3

      \[\leadsto \frac{\mathsf{fma}\left(\frac{0.5}{im \cdot im} \cdot re, re, \color{blue}{\log im}\right)}{\log base} \]
  7. Applied rewrites28.3%

    \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{0.5}{im \cdot im} \cdot re, re, \log im\right)}}{\log base} \]
  8. Step-by-step derivation
    1. Applied rewrites29.9%

      \[\leadsto \frac{\mathsf{fma}\left(\frac{\frac{0.5 \cdot re}{im}}{im}, re, \log im\right)}{\log base} \]
    2. Add Preprocessing

    Alternative 2: 98.8% accurate, 2.7× speedup?

    \[\begin{array}{l} im_m = \left|im\right| \\ re_m = \left|re\right| \\ [re_m, im_m, base] = \mathsf{sort}([re_m, im_m, base])\\ \\ \frac{\log im\_m}{\log base} \end{array} \]
    im_m = (fabs.f64 im)
    re_m = (fabs.f64 re)
    NOTE: re_m, im_m, and base should be sorted in increasing order before calling this function.
    (FPCore (re_m im_m base) :precision binary64 (/ (log im_m) (log base)))
    im_m = fabs(im);
    re_m = fabs(re);
    assert(re_m < im_m && im_m < base);
    double code(double re_m, double im_m, double base) {
    	return log(im_m) / log(base);
    }
    
    im_m =     private
    re_m =     private
    NOTE: re_m, im_m, and base should be sorted in increasing order before calling this function.
    module fmin_fmax_functions
        implicit none
        private
        public fmax
        public fmin
    
        interface fmax
            module procedure fmax88
            module procedure fmax44
            module procedure fmax84
            module procedure fmax48
        end interface
        interface fmin
            module procedure fmin88
            module procedure fmin44
            module procedure fmin84
            module procedure fmin48
        end interface
    contains
        real(8) function fmax88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(4) function fmax44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(8) function fmax84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmax48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
        end function
        real(8) function fmin88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(4) function fmin44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(8) function fmin84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmin48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
        end function
    end module
    
    real(8) function code(re_m, im_m, base)
    use fmin_fmax_functions
        real(8), intent (in) :: re_m
        real(8), intent (in) :: im_m
        real(8), intent (in) :: base
        code = log(im_m) / log(base)
    end function
    
    im_m = Math.abs(im);
    re_m = Math.abs(re);
    assert re_m < im_m && im_m < base;
    public static double code(double re_m, double im_m, double base) {
    	return Math.log(im_m) / Math.log(base);
    }
    
    im_m = math.fabs(im)
    re_m = math.fabs(re)
    [re_m, im_m, base] = sort([re_m, im_m, base])
    def code(re_m, im_m, base):
    	return math.log(im_m) / math.log(base)
    
    im_m = abs(im)
    re_m = abs(re)
    re_m, im_m, base = sort([re_m, im_m, base])
    function code(re_m, im_m, base)
    	return Float64(log(im_m) / log(base))
    end
    
    im_m = abs(im);
    re_m = abs(re);
    re_m, im_m, base = num2cell(sort([re_m, im_m, base])){:}
    function tmp = code(re_m, im_m, base)
    	tmp = log(im_m) / log(base);
    end
    
    im_m = N[Abs[im], $MachinePrecision]
    re_m = N[Abs[re], $MachinePrecision]
    NOTE: re_m, im_m, and base should be sorted in increasing order before calling this function.
    code[re$95$m_, im$95$m_, base_] := N[(N[Log[im$95$m], $MachinePrecision] / N[Log[base], $MachinePrecision]), $MachinePrecision]
    
    \begin{array}{l}
    im_m = \left|im\right|
    \\
    re_m = \left|re\right|
    \\
    [re_m, im_m, base] = \mathsf{sort}([re_m, im_m, base])\\
    \\
    \frac{\log im\_m}{\log base}
    \end{array}
    
    Derivation
    1. Initial program 49.4%

      \[\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \]
    2. Add Preprocessing
    3. Taylor expanded in re around 0

      \[\leadsto \color{blue}{\frac{\log im}{\log base}} \]
    4. Step-by-step derivation
      1. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{\log im}{\log base}} \]
      2. lower-log.f64N/A

        \[\leadsto \frac{\color{blue}{\log im}}{\log base} \]
      3. lower-log.f6431.1

        \[\leadsto \frac{\log im}{\color{blue}{\log base}} \]
    5. Applied rewrites31.1%

      \[\leadsto \color{blue}{\frac{\log im}{\log base}} \]
    6. Add Preprocessing

    Alternative 3: 3.1% accurate, 4.1× speedup?

    \[\begin{array}{l} im_m = \left|im\right| \\ re_m = \left|re\right| \\ [re_m, im_m, base] = \mathsf{sort}([re_m, im_m, base])\\ \\ \frac{\left(\frac{0.5}{im\_m \cdot im\_m} \cdot re\_m\right) \cdot re\_m}{\log base} \end{array} \]
    im_m = (fabs.f64 im)
    re_m = (fabs.f64 re)
    NOTE: re_m, im_m, and base should be sorted in increasing order before calling this function.
    (FPCore (re_m im_m base)
     :precision binary64
     (/ (* (* (/ 0.5 (* im_m im_m)) re_m) re_m) (log base)))
    im_m = fabs(im);
    re_m = fabs(re);
    assert(re_m < im_m && im_m < base);
    double code(double re_m, double im_m, double base) {
    	return (((0.5 / (im_m * im_m)) * re_m) * re_m) / log(base);
    }
    
    im_m =     private
    re_m =     private
    NOTE: re_m, im_m, and base should be sorted in increasing order before calling this function.
    module fmin_fmax_functions
        implicit none
        private
        public fmax
        public fmin
    
        interface fmax
            module procedure fmax88
            module procedure fmax44
            module procedure fmax84
            module procedure fmax48
        end interface
        interface fmin
            module procedure fmin88
            module procedure fmin44
            module procedure fmin84
            module procedure fmin48
        end interface
    contains
        real(8) function fmax88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(4) function fmax44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(8) function fmax84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmax48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
        end function
        real(8) function fmin88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(4) function fmin44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(8) function fmin84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmin48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
        end function
    end module
    
    real(8) function code(re_m, im_m, base)
    use fmin_fmax_functions
        real(8), intent (in) :: re_m
        real(8), intent (in) :: im_m
        real(8), intent (in) :: base
        code = (((0.5d0 / (im_m * im_m)) * re_m) * re_m) / log(base)
    end function
    
    im_m = Math.abs(im);
    re_m = Math.abs(re);
    assert re_m < im_m && im_m < base;
    public static double code(double re_m, double im_m, double base) {
    	return (((0.5 / (im_m * im_m)) * re_m) * re_m) / Math.log(base);
    }
    
    im_m = math.fabs(im)
    re_m = math.fabs(re)
    [re_m, im_m, base] = sort([re_m, im_m, base])
    def code(re_m, im_m, base):
    	return (((0.5 / (im_m * im_m)) * re_m) * re_m) / math.log(base)
    
    im_m = abs(im)
    re_m = abs(re)
    re_m, im_m, base = sort([re_m, im_m, base])
    function code(re_m, im_m, base)
    	return Float64(Float64(Float64(Float64(0.5 / Float64(im_m * im_m)) * re_m) * re_m) / log(base))
    end
    
    im_m = abs(im);
    re_m = abs(re);
    re_m, im_m, base = num2cell(sort([re_m, im_m, base])){:}
    function tmp = code(re_m, im_m, base)
    	tmp = (((0.5 / (im_m * im_m)) * re_m) * re_m) / log(base);
    end
    
    im_m = N[Abs[im], $MachinePrecision]
    re_m = N[Abs[re], $MachinePrecision]
    NOTE: re_m, im_m, and base should be sorted in increasing order before calling this function.
    code[re$95$m_, im$95$m_, base_] := N[(N[(N[(N[(0.5 / N[(im$95$m * im$95$m), $MachinePrecision]), $MachinePrecision] * re$95$m), $MachinePrecision] * re$95$m), $MachinePrecision] / N[Log[base], $MachinePrecision]), $MachinePrecision]
    
    \begin{array}{l}
    im_m = \left|im\right|
    \\
    re_m = \left|re\right|
    \\
    [re_m, im_m, base] = \mathsf{sort}([re_m, im_m, base])\\
    \\
    \frac{\left(\frac{0.5}{im\_m \cdot im\_m} \cdot re\_m\right) \cdot re\_m}{\log base}
    \end{array}
    
    Derivation
    1. Initial program 49.4%

      \[\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \cdot 0}{\log base \cdot \log base + 0 \cdot 0} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-+.f64N/A

        \[\leadsto \frac{\color{blue}{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \tan^{-1}_* \frac{im}{re} \cdot 0}}{\log base \cdot \log base + 0 \cdot 0} \]
      2. lift-*.f64N/A

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \color{blue}{\tan^{-1}_* \frac{im}{re} \cdot 0}}{\log base \cdot \log base + 0 \cdot 0} \]
      3. mul0-rgtN/A

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base + \color{blue}{0}}{\log base \cdot \log base + 0 \cdot 0} \]
      4. +-rgt-identity49.4

        \[\leadsto \frac{\color{blue}{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}}{\log base \cdot \log base + 0 \cdot 0} \]
      5. lift-+.f64N/A

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\color{blue}{\log base \cdot \log base + 0 \cdot 0}} \]
      6. lift-*.f64N/A

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base + \color{blue}{0 \cdot 0}} \]
      7. metadata-evalN/A

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base + \color{blue}{0}} \]
      8. +-rgt-identity49.4

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\color{blue}{\log base \cdot \log base}} \]
      9. lower-/.f64N/A

        \[\leadsto \color{blue}{\frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base}} \]
      10. +-rgt-identityN/A

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\color{blue}{\log base \cdot \log base + 0}} \]
      11. metadata-evalN/A

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base + \color{blue}{0 \cdot 0}} \]
      12. lift-*.f64N/A

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base + \color{blue}{0 \cdot 0}} \]
      13. lift-+.f64N/A

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\color{blue}{\log base \cdot \log base + 0 \cdot 0}} \]
      14. lift-+.f64N/A

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\color{blue}{\log base \cdot \log base + 0 \cdot 0}} \]
      15. lift-*.f64N/A

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base + \color{blue}{0 \cdot 0}} \]
      16. metadata-evalN/A

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\log base \cdot \log base + \color{blue}{0}} \]
      17. +-rgt-identityN/A

        \[\leadsto \frac{\log \left(\sqrt{re \cdot re + im \cdot im}\right) \cdot \log base}{\color{blue}{\log base \cdot \log base}} \]
    4. Applied rewrites99.4%

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

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

        \[\leadsto \frac{\color{blue}{\frac{1}{2} \cdot \frac{{re}^{2}}{{im}^{2}} + \log im}}{\log base} \]
      2. associate-*r/N/A

        \[\leadsto \frac{\color{blue}{\frac{\frac{1}{2} \cdot {re}^{2}}{{im}^{2}}} + \log im}{\log base} \]
      3. associate-*l/N/A

        \[\leadsto \frac{\color{blue}{\frac{\frac{1}{2}}{{im}^{2}} \cdot {re}^{2}} + \log im}{\log base} \]
      4. metadata-evalN/A

        \[\leadsto \frac{\frac{\color{blue}{\frac{1}{2} \cdot 1}}{{im}^{2}} \cdot {re}^{2} + \log im}{\log base} \]
      5. associate-*r/N/A

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

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

        \[\leadsto \frac{\color{blue}{\left(\left(\frac{1}{2} \cdot \frac{1}{{im}^{2}}\right) \cdot re\right) \cdot re} + \log im}{\log base} \]
      8. lower-fma.f64N/A

        \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\left(\frac{1}{2} \cdot \frac{1}{{im}^{2}}\right) \cdot re, re, \log im\right)}}{\log base} \]
      9. lower-*.f64N/A

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

        \[\leadsto \frac{\mathsf{fma}\left(\color{blue}{\frac{\frac{1}{2} \cdot 1}{{im}^{2}}} \cdot re, re, \log im\right)}{\log base} \]
      11. metadata-evalN/A

        \[\leadsto \frac{\mathsf{fma}\left(\frac{\color{blue}{\frac{1}{2}}}{{im}^{2}} \cdot re, re, \log im\right)}{\log base} \]
      12. lower-/.f64N/A

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

        \[\leadsto \frac{\mathsf{fma}\left(\frac{\frac{1}{2}}{\color{blue}{im \cdot im}} \cdot re, re, \log im\right)}{\log base} \]
      14. lower-*.f64N/A

        \[\leadsto \frac{\mathsf{fma}\left(\frac{\frac{1}{2}}{\color{blue}{im \cdot im}} \cdot re, re, \log im\right)}{\log base} \]
      15. lower-log.f6428.3

        \[\leadsto \frac{\mathsf{fma}\left(\frac{0.5}{im \cdot im} \cdot re, re, \color{blue}{\log im}\right)}{\log base} \]
    7. Applied rewrites28.3%

      \[\leadsto \frac{\color{blue}{\mathsf{fma}\left(\frac{0.5}{im \cdot im} \cdot re, re, \log im\right)}}{\log base} \]
    8. Taylor expanded in re around inf

      \[\leadsto \frac{\frac{1}{2} \cdot \color{blue}{\frac{{re}^{2}}{{im}^{2}}}}{\log base} \]
    9. Step-by-step derivation
      1. Applied rewrites2.9%

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

      Reproduce

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