math.log10 on complex, imaginary part

Percentage Accurate: 98.7% → 99.8%
Time: 1.9s
Alternatives: 2
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \frac{\tan^{-1}_* \frac{im}{re}}{\log 10} \end{array} \]
(FPCore (re im) :precision binary64 (/ (atan2 im re) (log 10.0)))
double code(double re, double im) {
	return atan2(im, re) / log(10.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)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = atan2(im, re) / log(10.0d0)
end function
public static double code(double re, double im) {
	return Math.atan2(im, re) / Math.log(10.0);
}
def code(re, im):
	return math.atan2(im, re) / math.log(10.0)
function code(re, im)
	return Float64(atan(im, re) / log(10.0))
end
function tmp = code(re, im)
	tmp = atan2(im, re) / log(10.0);
end
code[re_, im_] := N[(N[ArcTan[im / re], $MachinePrecision] / N[Log[10.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\tan^{-1}_* \frac{im}{re}}{\log 10}
\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 2 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: 98.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\tan^{-1}_* \frac{im}{re}}{\log 10} \end{array} \]
(FPCore (re im) :precision binary64 (/ (atan2 im re) (log 10.0)))
double code(double re, double im) {
	return atan2(im, re) / log(10.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)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = atan2(im, re) / log(10.0d0)
end function
public static double code(double re, double im) {
	return Math.atan2(im, re) / Math.log(10.0);
}
def code(re, im):
	return math.atan2(im, re) / math.log(10.0)
function code(re, im)
	return Float64(atan(im, re) / log(10.0))
end
function tmp = code(re, im)
	tmp = atan2(im, re) / log(10.0);
end
code[re_, im_] := N[(N[ArcTan[im / re], $MachinePrecision] / N[Log[10.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\tan^{-1}_* \frac{im}{re}}{\log 10}
\end{array}

Alternative 1: 99.8% accurate, 1.0× speedup?

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

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

    \[\frac{\tan^{-1}_* \frac{im}{re}}{\log 10} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. remove-double-negN/A

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re}}{\color{blue}{\mathsf{neg}\left(\left(\mathsf{neg}\left(\log 10\right)\right)\right)}} \]
    2. lower-neg.f64N/A

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re}}{\color{blue}{-\left(\mathsf{neg}\left(\log 10\right)\right)}} \]
    3. lift-log.f64N/A

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re}}{-\left(\mathsf{neg}\left(\color{blue}{\log 10}\right)\right)} \]
    4. neg-logN/A

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re}}{-\color{blue}{\log \left(\frac{1}{10}\right)}} \]
    5. lower-log.f64N/A

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re}}{-\color{blue}{\log \left(\frac{1}{10}\right)}} \]
    6. metadata-eval99.7

      \[\leadsto \frac{\tan^{-1}_* \frac{im}{re}}{-\log \color{blue}{0.1}} \]
  4. Applied rewrites99.7%

    \[\leadsto \frac{\tan^{-1}_* \frac{im}{re}}{\color{blue}{-\log 0.1}} \]
  5. Add Preprocessing

Alternative 2: 98.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\tan^{-1}_* \frac{im}{re}}{\log 10} \end{array} \]
(FPCore (re im) :precision binary64 (/ (atan2 im re) (log 10.0)))
double code(double re, double im) {
	return atan2(im, re) / log(10.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)
use fmin_fmax_functions
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = atan2(im, re) / log(10.0d0)
end function
public static double code(double re, double im) {
	return Math.atan2(im, re) / Math.log(10.0);
}
def code(re, im):
	return math.atan2(im, re) / math.log(10.0)
function code(re, im)
	return Float64(atan(im, re) / log(10.0))
end
function tmp = code(re, im)
	tmp = atan2(im, re) / log(10.0);
end
code[re_, im_] := N[(N[ArcTan[im / re], $MachinePrecision] / N[Log[10.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

    \[\frac{\tan^{-1}_* \frac{im}{re}}{\log 10} \]
  2. Add Preprocessing
  3. Add Preprocessing

Reproduce

?
herbie shell --seed 2025038 
(FPCore (re im)
  :name "math.log10 on complex, imaginary part"
  :precision binary64
  (/ (atan2 im re) (log 10.0)))