math.cos on complex, real part

?

Percentage Accurate: 100.0% → 100.0%
Time: 13.8s
Precision: binary64
Cost: 19712

?

\[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
\[\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]
(FPCore (re im)
 :precision binary64
 (* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))
(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));
}
double code(double re, double im) {
	return (0.5 * cos(re)) * (exp(-im) + exp(im));
}
real(8) function code(re, im)
    real(8), intent (in) :: re
    real(8), intent (in) :: im
    code = (0.5d0 * cos(re)) * (exp(-im) + exp(im))
end function
real(8) function code(re, im)
    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));
}
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))
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 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
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]
code[re_, im_] := N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)
\left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right)

Local Percentage Accuracy?

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.

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Initial program 100.0%

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

    \[\leadsto \left(0.5 \cdot \cos re\right) \cdot \left(e^{-im} + e^{im}\right) \]

Alternatives

Alternative 1
Accuracy93.7%
Cost14096
\[\begin{array}{l} t_0 := e^{-im} + e^{im}\\ t_1 := 0.5 \cdot \left(im \cdot im\right)\\ t_2 := \cos re \cdot t_1\\ \mathbf{if}\;im \leq -5.9 \cdot 10^{+152}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq -0.55:\\ \;\;\;\;0.5 \cdot t_0\\ \mathbf{elif}\;im \leq 2.3:\\ \;\;\;\;\cos re \cdot \left(t_1 + 1\right)\\ \mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\ \;\;\;\;t_0 \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
Alternative 2
Accuracy93.2%
Cost13712
\[\begin{array}{l} t_0 := 0.5 \cdot \left(im \cdot im\right)\\ t_1 := \cos re \cdot t_0\\ t_2 := 0.5 \cdot \left(e^{-im} + e^{im}\right)\\ \mathbf{if}\;im \leq -5.9 \cdot 10^{+152}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -0.55:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 2.3:\\ \;\;\;\;\cos re \cdot \left(t_0 + 1\right)\\ \mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 3
Accuracy85.5%
Cost7956
\[\begin{array}{l} t_0 := 0.5 \cdot \left(im \cdot im\right)\\ t_1 := \cos re \cdot t_0\\ t_2 := \frac{{im}^{4} \cdot 0.25 + -1}{t_0 + -1}\\ \mathbf{if}\;im \leq -5.9 \cdot 10^{+152}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -1.2 \cdot 10^{+77}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq -7200:\\ \;\;\;\;\left(im \cdot im\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \mathbf{elif}\;im \leq 3.3 \cdot 10^{+75}:\\ \;\;\;\;\cos re \cdot \left(t_0 + 1\right)\\ \mathbf{elif}\;im \leq 1.35 \cdot 10^{+154}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 4
Accuracy79.9%
Cost7760
\[\begin{array}{l} t_0 := 0.5 \cdot \left(im \cdot im\right)\\ t_1 := \cos re \cdot t_0\\ \mathbf{if}\;im \leq -2.1 \cdot 10^{+154}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -780:\\ \;\;\;\;\left(im \cdot im\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \mathbf{elif}\;im \leq 4.5 \cdot 10^{+22}:\\ \;\;\;\;\cos re \cdot \left(t_0 + 1\right)\\ \mathbf{elif}\;im \leq 6.6 \cdot 10^{+152}:\\ \;\;\;\;\mathsf{fma}\left(0.5, im \cdot im, 1\right) \cdot \left(1 + \left(re \cdot re\right) \cdot -0.5\right)\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 5
Accuracy79.7%
Cost7376
\[\begin{array}{l} t_0 := \cos re \cdot \left(0.5 \cdot \left(im \cdot im\right)\right)\\ t_1 := \left(im \cdot im\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \mathbf{if}\;im \leq -2.1 \cdot 10^{+154}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -780:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq 4.1 \cdot 10^{+14}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 6.6 \cdot 10^{+152}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Accuracy79.7%
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \left(im \cdot im\right)\\ t_1 := \cos re \cdot t_0\\ t_2 := \left(im \cdot im\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \mathbf{if}\;im \leq -2.1 \cdot 10^{+154}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -2300:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 6 \cdot 10^{+22}:\\ \;\;\;\;\cos re + t_0\\ \mathbf{elif}\;im \leq 6.6 \cdot 10^{+152}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 7
Accuracy79.9%
Cost7376
\[\begin{array}{l} t_0 := 0.5 \cdot \left(im \cdot im\right)\\ t_1 := \cos re \cdot t_0\\ t_2 := \left(im \cdot im\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \mathbf{if}\;im \leq -2.1 \cdot 10^{+154}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;im \leq -780:\\ \;\;\;\;t_2\\ \mathbf{elif}\;im \leq 4.5 \cdot 10^{+22}:\\ \;\;\;\;\cos re \cdot \left(t_0 + 1\right)\\ \mathbf{elif}\;im \leq 6.6 \cdot 10^{+152}:\\ \;\;\;\;t_2\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 8
Accuracy73.6%
Cost6728
\[\begin{array}{l} t_0 := \left(im \cdot im\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \mathbf{if}\;im \leq -780:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq 4.1 \cdot 10^{+14}:\\ \;\;\;\;\cos re\\ \mathbf{elif}\;im \leq 2.7 \cdot 10^{+193}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot im\right) + 1\\ \end{array} \]
Alternative 9
Accuracy48.4%
Cost1100
\[\begin{array}{l} t_0 := \left(im \cdot im\right) \cdot \left(0.5 + -0.25 \cdot \left(re \cdot re\right)\right)\\ \mathbf{if}\;re \leq -7.8 \cdot 10^{+89}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;re \leq 4.6 \cdot 10^{+136}:\\ \;\;\;\;0.5 \cdot \left(im \cdot im\right) + 1\\ \mathbf{elif}\;re \leq 2.36 \cdot 10^{+213}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.25 + \left(re \cdot re\right) \cdot 0.25\\ \end{array} \]
Alternative 10
Accuracy47.4%
Cost716
\[\begin{array}{l} t_0 := im \cdot \left(0.5 \cdot im\right)\\ \mathbf{if}\;im \leq -1.85 \cdot 10^{+158}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;im \leq -220:\\ \;\;\;\;0.25 + \left(re \cdot re\right) \cdot 0.25\\ \mathbf{elif}\;im \leq 1.4:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 11
Accuracy47.5%
Cost712
\[\begin{array}{l} \mathbf{if}\;im \leq -1.85 \cdot 10^{+158}:\\ \;\;\;\;im \cdot \left(0.5 \cdot im\right)\\ \mathbf{elif}\;im \leq -250:\\ \;\;\;\;0.25 + \left(re \cdot re\right) \cdot 0.25\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(im \cdot im\right) + 1\\ \end{array} \]
Alternative 12
Accuracy46.1%
Cost585
\[\begin{array}{l} \mathbf{if}\;im \leq -1.75 \cdot 10^{+19} \lor \neg \left(im \leq 1.3\right):\\ \;\;\;\;im \cdot \left(0.5 \cdot im\right)\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]
Alternative 13
Accuracy8.0%
Cost64
\[0.25 \]
Alternative 14
Accuracy27.6%
Cost64
\[1 \]

Error

Reproduce?

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