
(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:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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}
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}
Initial program 49.4%
lift-+.f64N/A
lift-*.f64N/A
mul0-rgtN/A
+-rgt-identity49.4
lift-+.f64N/A
lift-*.f64N/A
metadata-evalN/A
+-rgt-identity49.4
lower-/.f64N/A
+-rgt-identityN/A
metadata-evalN/A
lift-*.f64N/A
lift-+.f64N/A
lift-+.f64N/A
lift-*.f64N/A
metadata-evalN/A
+-rgt-identityN/A
Applied rewrites99.4%
Taylor expanded in re around 0
+-commutativeN/A
associate-*r/N/A
associate-*l/N/A
metadata-evalN/A
associate-*r/N/A
unpow2N/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
lower-log.f6428.3
Applied rewrites28.3%
Applied rewrites29.9%
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}
Initial program 49.4%
Taylor expanded in re around 0
lower-/.f64N/A
lower-log.f64N/A
lower-log.f6431.1
Applied rewrites31.1%
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}
Initial program 49.4%
lift-+.f64N/A
lift-*.f64N/A
mul0-rgtN/A
+-rgt-identity49.4
lift-+.f64N/A
lift-*.f64N/A
metadata-evalN/A
+-rgt-identity49.4
lower-/.f64N/A
+-rgt-identityN/A
metadata-evalN/A
lift-*.f64N/A
lift-+.f64N/A
lift-+.f64N/A
lift-*.f64N/A
metadata-evalN/A
+-rgt-identityN/A
Applied rewrites99.4%
Taylor expanded in re around 0
+-commutativeN/A
associate-*r/N/A
associate-*l/N/A
metadata-evalN/A
associate-*r/N/A
unpow2N/A
associate-*r*N/A
lower-fma.f64N/A
lower-*.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
lower-log.f6428.3
Applied rewrites28.3%
Taylor expanded in re around inf
Applied rewrites2.9%
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))))