math.cos on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 7.9s
Alternatives: 6
Speedup: 0.6×

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 6 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, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 0.5 \cdot \cos re\\ \mathsf{fma}\left(t\_0, e^{im}, \frac{t\_0}{e^{im}}\right) \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (cos re)))) (fma t_0 (exp im) (/ t_0 (exp im)))))
double code(double re, double im) {
	double t_0 = 0.5 * cos(re);
	return fma(t_0, exp(im), (t_0 / exp(im)));
}
function code(re, im)
	t_0 = Float64(0.5 * cos(re))
	return fma(t_0, exp(im), Float64(t_0 / exp(im)))
end
code[re_, im_] := Block[{t$95$0 = N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision]}, N[(t$95$0 * N[Exp[im], $MachinePrecision] + N[(t$95$0 / N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 0.5 \cdot \cos re\\
\mathsf{fma}\left(t\_0, e^{im}, \frac{t\_0}{e^{im}}\right)
\end{array}
\end{array}
Derivation
  1. Initial program 100.0%

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Applied rewrites100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(0.5 \cdot \cos re, e^{im}, \frac{0.5 \cdot \cos re}{e^{im}}\right)} \]
  3. Add Preprocessing

Alternative 2: 100.0% accurate, 1.2× speedup?

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

\\
\left(0.5 + 0.5\right) \cdot \left(\cos re \cdot \cosh 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. Applied rewrites100.0%

    \[\leadsto \color{blue}{\mathsf{fma}\left(0.5 \cdot \cos re, e^{im}, \frac{0.5 \cdot \cos re}{e^{im}}\right)} \]
  3. Applied rewrites100.0%

    \[\leadsto \color{blue}{\left(0.5 + 0.5\right) \cdot \left(\cos re \cdot \cosh im\right)} \]
  4. Add Preprocessing

Alternative 3: 99.1% 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)\\ \mathbf{if}\;t\_1 \leq -\infty:\\ \;\;\;\;\mathsf{fma}\left(re \cdot -0.25, re, 0.5\right) \cdot \left(\cosh im \cdot 2\right)\\ \mathbf{elif}\;t\_1 \leq 0.9988892330884848:\\ \;\;\;\;t\_0 \cdot 2\\ \mathbf{else}:\\ \;\;\;\;\left(0.5 \cdot 2\right) \cdot \cosh im\\ \end{array} \end{array} \]
(FPCore (re im)
 :precision binary64
 (let* ((t_0 (* 0.5 (cos re))) (t_1 (* t_0 (+ (exp (- im)) (exp im)))))
   (if (<= t_1 (- INFINITY))
     (* (fma (* re -0.25) re 0.5) (* (cosh im) 2.0))
     (if (<= t_1 0.9988892330884848) (* t_0 2.0) (* (* 0.5 2.0) (cosh im))))))
double code(double re, double im) {
	double t_0 = 0.5 * cos(re);
	double t_1 = t_0 * (exp(-im) + exp(im));
	double tmp;
	if (t_1 <= -((double) INFINITY)) {
		tmp = fma((re * -0.25), re, 0.5) * (cosh(im) * 2.0);
	} else if (t_1 <= 0.9988892330884848) {
		tmp = t_0 * 2.0;
	} else {
		tmp = (0.5 * 2.0) * cosh(im);
	}
	return tmp;
}
function code(re, im)
	t_0 = Float64(0.5 * cos(re))
	t_1 = Float64(t_0 * Float64(exp(Float64(-im)) + exp(im)))
	tmp = 0.0
	if (t_1 <= Float64(-Inf))
		tmp = Float64(fma(Float64(re * -0.25), re, 0.5) * Float64(cosh(im) * 2.0));
	elseif (t_1 <= 0.9988892330884848)
		tmp = Float64(t_0 * 2.0);
	else
		tmp = Float64(Float64(0.5 * 2.0) * cosh(im));
	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]}, If[LessEqual[t$95$1, (-Infinity)], N[(N[(N[(re * -0.25), $MachinePrecision] * re + 0.5), $MachinePrecision] * N[(N[Cosh[im], $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 0.9988892330884848], N[(t$95$0 * 2.0), $MachinePrecision], N[(N[(0.5 * 2.0), $MachinePrecision] * N[Cosh[im], $MachinePrecision]), $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)\\
\mathbf{if}\;t\_1 \leq -\infty:\\
\;\;\;\;\mathsf{fma}\left(re \cdot -0.25, re, 0.5\right) \cdot \left(\cosh im \cdot 2\right)\\

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

\mathbf{else}:\\
\;\;\;\;\left(0.5 \cdot 2\right) \cdot \cosh im\\


\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.3%

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

      \[\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.998889233088484763

    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.1%

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

    if 0.998889233088484763 < (*.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. Applied rewrites100.0%

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

      \[\leadsto \left(\color{blue}{\frac{1}{2}} \cdot 2\right) \cdot \cosh im \]
    4. Applied rewrites64.6%

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

Alternative 4: 77.3% accurate, 0.7× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;\left(0.5 \cdot 2\right) \cdot \cosh im\\


\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.0050000000000000001

    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.3%

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

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

    if -0.0050000000000000001 < (*.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. Applied rewrites100.0%

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

      \[\leadsto \left(\color{blue}{\frac{1}{2}} \cdot 2\right) \cdot \cosh im \]
    4. Applied rewrites64.6%

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

Alternative 5: 64.6% accurate, 3.6× speedup?

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

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

    \[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
  2. Applied rewrites100.0%

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

    \[\leadsto \left(\color{blue}{\frac{1}{2}} \cdot 2\right) \cdot \cosh im \]
  4. Applied rewrites64.6%

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

Alternative 6: 28.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.1%

    \[\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 rewrites28.2%

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

Reproduce

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