math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 6.5s
Alternatives: 8
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))
double code(double re, double im) {
	return (0.5 * cos(re)) * (exp(-im) + exp(im));
}
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 = (0.5d0 * cos(re)) * (exp(-im) + exp(im))
end function
public static double code(double re, double im) {
	return (0.5 * Math.cos(re)) * (Math.exp(-im) + Math.exp(im));
}
def code(re, im):
	return (0.5 * math.cos(re)) * (math.exp(-im) + math.exp(im))
function code(re, im)
	return Float64(Float64(0.5 * cos(re)) * Float64(exp(Float64(-im)) + exp(im)))
end
function tmp = code(re, im)
	tmp = (0.5 * cos(re)) * (exp(-im) + exp(im));
end
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\end{array}

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 8 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: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))
double code(double re, double im) {
	return (0.5 * cos(re)) * (exp(-im) + exp(im));
}
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 = (0.5d0 * cos(re)) * (exp(-im) + exp(im))
end function
public static double code(double re, double im) {
	return (0.5 * Math.cos(re)) * (Math.exp(-im) + Math.exp(im));
}
def code(re, im):
	return (0.5 * math.cos(re)) * (math.exp(-im) + math.exp(im))
function code(re, im)
	return Float64(Float64(0.5 * cos(re)) * Float64(exp(Float64(-im)) + exp(im)))
end
function tmp = code(re, im)
	tmp = (0.5 * cos(re)) * (exp(-im) + exp(im));
end
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\end{array}

Alternative 1: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))
double code(double re, double im) {
	return (0.5 * cos(re)) * (exp(-im) + exp(im));
}
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 = (0.5d0 * cos(re)) * (exp(-im) + exp(im))
end function
public static double code(double re, double im) {
	return (0.5 * Math.cos(re)) * (Math.exp(-im) + Math.exp(im));
}
def code(re, im):
	return (0.5 * math.cos(re)) * (math.exp(-im) + math.exp(im))
function code(re, im)
	return Float64(Float64(0.5 * cos(re)) * Float64(exp(Float64(-im)) + exp(im)))
end
function tmp = code(re, im)
	tmp = (0.5 * cos(re)) * (exp(-im) + exp(im));
end
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Add Preprocessing

Alternative 2: 99.5% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \cos re\\ t_1 := t\_0 \cdot \left(e^{-im} + e^{im}\right)\\ t_2 := \cosh im \cdot 2\\ \mathbf{if}\;t\_1 \leq -\infty:\\ \;\;\;\;\mathsf{fma}\left(re \cdot -0.25, re, 0.5\right) \cdot t\_2\\ \mathbf{elif}\;t\_1 \leq 0.9999984943428811:\\ \;\;\;\;t\_0 \cdot \mathsf{fma}\left(im, im, 2\right)\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot t\_2\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (cos re)))
        (t_1 (* t_0 (+ (exp (- im)) (exp im))))
        (t_2 (* (cosh im) 2.0)))
   (if (<= t_1 (- INFINITY))
     (* (fma (* re -0.25) re 0.5) t_2)
     (if (<= t_1 0.9999984943428811) (* t_0 (fma im im 2.0)) (* 0.5 t_2)))))
double code(double re, double im) {
	double t_0 = 0.5 * cos(re);
	double t_1 = t_0 * (exp(-im) + exp(im));
	double t_2 = cosh(im) * 2.0;
	double tmp;
	if (t_1 <= -((double) INFINITY)) {
		tmp = fma((re * -0.25), re, 0.5) * t_2;
	} else if (t_1 <= 0.9999984943428811) {
		tmp = t_0 * fma(im, im, 2.0);
	} else {
		tmp = 0.5 * t_2;
	}
	return tmp;
}
function code(re, im)
	t_0 = Float64(0.5 * cos(re))
	t_1 = Float64(t_0 * Float64(exp(Float64(-im)) + exp(im)))
	t_2 = Float64(cosh(im) * 2.0)
	tmp = 0.0
	if (t_1 <= Float64(-Inf))
		tmp = Float64(fma(Float64(re * -0.25), re, 0.5) * t_2);
	elseif (t_1 <= 0.9999984943428811)
		tmp = Float64(t_0 * fma(im, im, 2.0));
	else
		tmp = Float64(0.5 * t_2);
	end
	return tmp
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[Cosh[im], $MachinePrecision] * 2.0), $MachinePrecision]}, If[LessEqual[t$95$1, (-Infinity)], N[(N[(N[(re * -0.25), $MachinePrecision] * re + 0.5), $MachinePrecision] * t$95$2), $MachinePrecision], If[LessEqual[t$95$1, 0.9999984943428811], N[(t$95$0 * N[(im * im + 2.0), $MachinePrecision]), $MachinePrecision], N[(0.5 * t$95$2), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \cos re\\
t_1 := t\_0 \cdot \left(e^{-im} + e^{im}\right)\\
t_2 := \cosh im \cdot 2\\
\mathbf{if}\;t\_1 \leq -\infty:\\
\;\;\;\;\mathsf{fma}\left(re \cdot -0.25, re, 0.5\right) \cdot t\_2\\

\mathbf{elif}\;t\_1 \leq 0.9999984943428811:\\
\;\;\;\;t\_0 \cdot \mathsf{fma}\left(im, im, 2\right)\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot t\_2\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))) < -inf.0

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Taylor expanded in re around 0

      \[\leadsto \color{blue}{\left(\frac{1}{2} + \frac{-1}{4} \cdot {re}^{2}\right)} \cdot \left(e^{-im} + e^{im}\right) \]
    3. Applied rewrites62.8%

      \[\leadsto \color{blue}{\left(0.5 + -0.25 \cdot {re}^{2}\right)} \cdot \left(e^{-im} + e^{im}\right) \]
    4. Applied rewrites62.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(re \cdot -0.25, re, 0.5\right) \cdot \left(\cosh im \cdot 2\right)} \]

    if -inf.0 < (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))) < 0.999998494342881128

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Taylor expanded in im around 0

      \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(\color{blue}{\left(1 + -1 \cdot im\right)} + e^{im}\right) \]
    3. Applied rewrites75.9%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{\left(1 + -1 \cdot im\right)} + e^{im}\right) \]
    4. Applied rewrites75.9%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\left(1 + -1 \cdot im\right) + \color{blue}{\frac{1}{e^{-im}}}\right) \]
    5. Taylor expanded in im around 0

      \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \color{blue}{\left(2 + {im}^{2}\right)} \]
    6. Applied rewrites76.1%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{\left(2 + {im}^{2}\right)} \]
    7. Applied rewrites76.1%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \mathsf{fma}\left(im, \color{blue}{im}, 2\right) \]

    if 0.999998494342881128 < (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)))

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Taylor expanded in re around 0

      \[\leadsto \color{blue}{\frac{1}{2}} \cdot \left(e^{-im} + e^{im}\right) \]
    3. Applied rewrites65.3%

      \[\leadsto \color{blue}{0.5} \cdot \left(e^{-im} + e^{im}\right) \]
    4. Applied rewrites65.3%

      \[\leadsto \color{blue}{0.5 \cdot \left(\cosh im \cdot 2\right)} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 3: 99.3% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \cos re\\ t_1 := t\_0 \cdot \left(e^{-im} + e^{im}\right)\\ t_2 := \cosh im \cdot 2\\ \mathbf{if}\;t\_1 \leq -\infty:\\ \;\;\;\;\mathsf{fma}\left(re \cdot -0.25, re, 0.5\right) \cdot t\_2\\ \mathbf{elif}\;t\_1 \leq 0.9999984943428811:\\ \;\;\;\;t\_0 \cdot 2\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot t\_2\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (cos re)))
        (t_1 (* t_0 (+ (exp (- im)) (exp im))))
        (t_2 (* (cosh im) 2.0)))
   (if (<= t_1 (- INFINITY))
     (* (fma (* re -0.25) re 0.5) t_2)
     (if (<= t_1 0.9999984943428811) (* t_0 2.0) (* 0.5 t_2)))))
double code(double re, double im) {
	double t_0 = 0.5 * cos(re);
	double t_1 = t_0 * (exp(-im) + exp(im));
	double t_2 = cosh(im) * 2.0;
	double tmp;
	if (t_1 <= -((double) INFINITY)) {
		tmp = fma((re * -0.25), re, 0.5) * t_2;
	} else if (t_1 <= 0.9999984943428811) {
		tmp = t_0 * 2.0;
	} else {
		tmp = 0.5 * t_2;
	}
	return tmp;
}
function code(re, im)
	t_0 = Float64(0.5 * cos(re))
	t_1 = Float64(t_0 * Float64(exp(Float64(-im)) + exp(im)))
	t_2 = Float64(cosh(im) * 2.0)
	tmp = 0.0
	if (t_1 <= Float64(-Inf))
		tmp = Float64(fma(Float64(re * -0.25), re, 0.5) * t_2);
	elseif (t_1 <= 0.9999984943428811)
		tmp = Float64(t_0 * 2.0);
	else
		tmp = Float64(0.5 * t_2);
	end
	return tmp
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[Cosh[im], $MachinePrecision] * 2.0), $MachinePrecision]}, If[LessEqual[t$95$1, (-Infinity)], N[(N[(N[(re * -0.25), $MachinePrecision] * re + 0.5), $MachinePrecision] * t$95$2), $MachinePrecision], If[LessEqual[t$95$1, 0.9999984943428811], N[(t$95$0 * 2.0), $MachinePrecision], N[(0.5 * t$95$2), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \cos re\\
t_1 := t\_0 \cdot \left(e^{-im} + e^{im}\right)\\
t_2 := \cosh im \cdot 2\\
\mathbf{if}\;t\_1 \leq -\infty:\\
\;\;\;\;\mathsf{fma}\left(re \cdot -0.25, re, 0.5\right) \cdot t\_2\\

\mathbf{elif}\;t\_1 \leq 0.9999984943428811:\\
\;\;\;\;t\_0 \cdot 2\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot t\_2\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))) < -inf.0

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Taylor expanded in re around 0

      \[\leadsto \color{blue}{\left(\frac{1}{2} + \frac{-1}{4} \cdot {re}^{2}\right)} \cdot \left(e^{-im} + e^{im}\right) \]
    3. Applied rewrites62.8%

      \[\leadsto \color{blue}{\left(0.5 + -0.25 \cdot {re}^{2}\right)} \cdot \left(e^{-im} + e^{im}\right) \]
    4. Applied rewrites62.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(re \cdot -0.25, re, 0.5\right) \cdot \left(\cosh im \cdot 2\right)} \]

    if -inf.0 < (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))) < 0.999998494342881128

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Taylor expanded in im around 0

      \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \color{blue}{2} \]
    3. Applied rewrites51.4%

      \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{2} \]

    if 0.999998494342881128 < (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)))

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Taylor expanded in re around 0

      \[\leadsto \color{blue}{\frac{1}{2}} \cdot \left(e^{-im} + e^{im}\right) \]
    3. Applied rewrites65.3%

      \[\leadsto \color{blue}{0.5} \cdot \left(e^{-im} + e^{im}\right) \]
    4. Applied rewrites65.3%

      \[\leadsto \color{blue}{0.5 \cdot \left(\cosh im \cdot 2\right)} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 4: 77.9% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \left(0.5 \cdot \cos re\right) \cdot \left(\left(1 - im\right) + \frac{1}{e^{-im}}\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (+ (- 1.0 im) (/ 1.0 (exp (- im))))))
double code(double re, double im) {
	return (0.5 * cos(re)) * ((1.0 - im) + (1.0 / exp(-im)));
}
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 = (0.5d0 * cos(re)) * ((1.0d0 - im) + (1.0d0 / exp(-im)))
end function
public static double code(double re, double im) {
	return (0.5 * Math.cos(re)) * ((1.0 - im) + (1.0 / Math.exp(-im)));
}
def code(re, im):
	return (0.5 * math.cos(re)) * ((1.0 - im) + (1.0 / math.exp(-im)))
function code(re, im)
	return Float64(Float64(0.5 * cos(re)) * Float64(Float64(1.0 - im) + Float64(1.0 / exp(Float64(-im)))))
end
function tmp = code(re, im)
	tmp = (0.5 * cos(re)) * ((1.0 - im) + (1.0 / exp(-im)));
end
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[(1.0 - im), $MachinePrecision] + N[(1.0 / N[Exp[(-im)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(0.5 \cdot \cos re\right) \cdot \left(\left(1 - im\right) + \frac{1}{e^{-im}}\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Taylor expanded in im around 0

    \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(\color{blue}{\left(1 + -1 \cdot im\right)} + e^{im}\right) \]
  3. Applied rewrites75.9%

    \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{\left(1 + -1 \cdot im\right)} + e^{im}\right) \]
  4. Applied rewrites75.9%

    \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\left(1 + -1 \cdot im\right) + \color{blue}{\frac{1}{e^{-im}}}\right) \]
  5. Applied rewrites75.9%

    \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{\left(1 - im\right)} + \frac{1}{e^{-im}}\right) \]
  6. Add Preprocessing

Alternative 5: 75.9% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \left(0.5 \cdot \cos re\right) \cdot \left(\left(1 - im\right) + e^{im}\right) \end{array} \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (+ (- 1.0 im) (exp im))))
double code(double re, double im) {
	return (0.5 * cos(re)) * ((1.0 - im) + exp(im));
}
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 = (0.5d0 * cos(re)) * ((1.0d0 - im) + exp(im))
end function
public static double code(double re, double im) {
	return (0.5 * Math.cos(re)) * ((1.0 - im) + Math.exp(im));
}
def code(re, im):
	return (0.5 * math.cos(re)) * ((1.0 - im) + math.exp(im))
function code(re, im)
	return Float64(Float64(0.5 * cos(re)) * Float64(Float64(1.0 - im) + exp(im)))
end
function tmp = code(re, im)
	tmp = (0.5 * cos(re)) * ((1.0 - im) + exp(im));
end
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[(1.0 - im), $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\left(0.5 \cdot \cos re\right) \cdot \left(\left(1 - im\right) + e^{im}\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Taylor expanded in im around 0

    \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \left(\color{blue}{\left(1 + -1 \cdot im\right)} + e^{im}\right) \]
  3. Applied rewrites75.9%

    \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(\color{blue}{\left(1 + -1 \cdot im\right)} + e^{im}\right) \]
  4. Applied rewrites75.9%

    \[\leadsto \color{blue}{\left(0.5 \cdot \cos re\right) \cdot \left(\left(1 - im\right) + e^{im}\right)} \]
  5. Add Preprocessing

Alternative 6: 75.9% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cosh im \cdot 2\\ \mathbf{if}\;\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \leq -0.05:\\ \;\;\;\;\mathsf{fma}\left(re \cdot -0.25, re, 0.5\right) \cdot t\_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* (cosh im) 2.0)))
   (if (<= (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))) -0.05)
     (* (fma (* re -0.25) re 0.5) t_0)
     (* 0.5 t_0))))
double code(double re, double im) {
	double t_0 = cosh(im) * 2.0;
	double tmp;
	if (((0.5 * cos(re)) * (exp(-im) + exp(im))) <= -0.05) {
		tmp = fma((re * -0.25), re, 0.5) * t_0;
	} else {
		tmp = 0.5 * t_0;
	}
	return tmp;
}
function code(re, im)
	t_0 = Float64(cosh(im) * 2.0)
	tmp = 0.0
	if (Float64(Float64(0.5 * cos(re)) * Float64(exp(Float64(-im)) + exp(im))) <= -0.05)
		tmp = Float64(fma(Float64(re * -0.25), re, 0.5) * t_0);
	else
		tmp = Float64(0.5 * t_0);
	end
	return tmp
end
code[re_, im_] := Block[{t$95$0 = N[(N[Cosh[im], $MachinePrecision] * 2.0), $MachinePrecision]}, If[LessEqual[N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -0.05], N[(N[(N[(re * -0.25), $MachinePrecision] * re + 0.5), $MachinePrecision] * t$95$0), $MachinePrecision], N[(0.5 * t$95$0), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cosh im \cdot 2\\
\mathbf{if}\;\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \leq -0.05:\\
\;\;\;\;\mathsf{fma}\left(re \cdot -0.25, re, 0.5\right) \cdot t\_0\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im))) < -0.050000000000000003

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Taylor expanded in re around 0

      \[\leadsto \color{blue}{\left(\frac{1}{2} + \frac{-1}{4} \cdot {re}^{2}\right)} \cdot \left(e^{-im} + e^{im}\right) \]
    3. Applied rewrites62.8%

      \[\leadsto \color{blue}{\left(0.5 + -0.25 \cdot {re}^{2}\right)} \cdot \left(e^{-im} + e^{im}\right) \]
    4. Applied rewrites62.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left(re \cdot -0.25, re, 0.5\right) \cdot \left(\cosh im \cdot 2\right)} \]

    if -0.050000000000000003 < (*.f64 (*.f64 #s(literal 1/2 binary64) (cos.f64 re)) (+.f64 (exp.f64 (neg.f64 im)) (exp.f64 im)))

    1. Initial program 100.0%

      \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
    2. Taylor expanded in re around 0

      \[\leadsto \color{blue}{\frac{1}{2}} \cdot \left(e^{-im} + e^{im}\right) \]
    3. Applied rewrites65.3%

      \[\leadsto \color{blue}{0.5} \cdot \left(e^{-im} + e^{im}\right) \]
    4. Applied rewrites65.3%

      \[\leadsto \color{blue}{0.5 \cdot \left(\cosh im \cdot 2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 7: 65.3% accurate, 3.6× speedup?

\[\begin{array}{l} \\ 0.5 \cdot \left(\cosh im \cdot 2\right) \end{array} \]
(FPCore (re im) :precision binary64 (* 0.5 (* (cosh im) 2.0)))
double code(double re, double im) {
	return 0.5 * (cosh(im) * 2.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 = 0.5d0 * (cosh(im) * 2.0d0)
end function
public static double code(double re, double im) {
	return 0.5 * (Math.cosh(im) * 2.0);
}
def code(re, im):
	return 0.5 * (math.cosh(im) * 2.0)
function code(re, im)
	return Float64(0.5 * Float64(cosh(im) * 2.0))
end
function tmp = code(re, im)
	tmp = 0.5 * (cosh(im) * 2.0);
end
code[re_, im_] := N[(0.5 * N[(N[Cosh[im], $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot \left(\cosh im \cdot 2\right)
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Taylor expanded in re around 0

    \[\leadsto \color{blue}{\frac{1}{2}} \cdot \left(e^{-im} + e^{im}\right) \]
  3. Applied rewrites65.3%

    \[\leadsto \color{blue}{0.5} \cdot \left(e^{-im} + e^{im}\right) \]
  4. Applied rewrites65.3%

    \[\leadsto \color{blue}{0.5 \cdot \left(\cosh im \cdot 2\right)} \]
  5. Add Preprocessing

Alternative 8: 29.2% accurate, 15.9× speedup?

\[\begin{array}{l} \\ 0.5 \cdot 2 \end{array} \]
(FPCore (re im) :precision binary64 (* 0.5 2.0))
double code(double re, double im) {
	return 0.5 * 2.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 = 0.5d0 * 2.0d0
end function
public static double code(double re, double im) {
	return 0.5 * 2.0;
}
def code(re, im):
	return 0.5 * 2.0
function code(re, im)
	return Float64(0.5 * 2.0)
end
function tmp = code(re, im)
	tmp = 0.5 * 2.0;
end
code[re_, im_] := N[(0.5 * 2.0), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot 2
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Taylor expanded in im around 0

    \[\leadsto \left(\frac{1}{2} \cdot \cos re\right) \cdot \color{blue}{2} \]
  3. Applied rewrites51.4%

    \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \color{blue}{2} \]
  4. Taylor expanded in re around 0

    \[\leadsto \color{blue}{\frac{1}{2}} \cdot 2 \]
  5. Applied rewrites29.2%

    \[\leadsto \color{blue}{0.5} \cdot 2 \]
  6. Add Preprocessing

Reproduce

?
herbie shell --seed 2025161 
(FPCore (re im)
  :name "math.cos on complex, real part"
  :precision binary64
  (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))