
(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));
}
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));
}
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}
Sampling outcomes in binary64 precision:
Herbie found 3 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(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));
}
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));
}
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}
(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));
}
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));
}
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}
Initial program 100.0%
Final simplification100.0%
(FPCore (re im)
:precision binary64
(let* ((t_0 (+ (exp (- im)) (exp im))))
(if (or (<= im -5e+89) (not (<= im 2.6e+255)))
(* t_0 (* 0.5 (+ 1.0 (* -0.5 (* re re)))))
(* 0.5 t_0))))
double code(double re, double im) {
double t_0 = exp(-im) + exp(im);
double tmp;
if ((im <= -5e+89) || !(im <= 2.6e+255)) {
tmp = t_0 * (0.5 * (1.0 + (-0.5 * (re * re))));
} else {
tmp = 0.5 * t_0;
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: t_0
real(8) :: tmp
t_0 = exp(-im) + exp(im)
if ((im <= (-5d+89)) .or. (.not. (im <= 2.6d+255))) then
tmp = t_0 * (0.5d0 * (1.0d0 + ((-0.5d0) * (re * re))))
else
tmp = 0.5d0 * t_0
end if
code = tmp
end function
public static double code(double re, double im) {
double t_0 = Math.exp(-im) + Math.exp(im);
double tmp;
if ((im <= -5e+89) || !(im <= 2.6e+255)) {
tmp = t_0 * (0.5 * (1.0 + (-0.5 * (re * re))));
} else {
tmp = 0.5 * t_0;
}
return tmp;
}
def code(re, im): t_0 = math.exp(-im) + math.exp(im) tmp = 0 if (im <= -5e+89) or not (im <= 2.6e+255): tmp = t_0 * (0.5 * (1.0 + (-0.5 * (re * re)))) else: tmp = 0.5 * t_0 return tmp
function code(re, im) t_0 = Float64(exp(Float64(-im)) + exp(im)) tmp = 0.0 if ((im <= -5e+89) || !(im <= 2.6e+255)) tmp = Float64(t_0 * Float64(0.5 * Float64(1.0 + Float64(-0.5 * Float64(re * re))))); else tmp = Float64(0.5 * t_0); end return tmp end
function tmp_2 = code(re, im) t_0 = exp(-im) + exp(im); tmp = 0.0; if ((im <= -5e+89) || ~((im <= 2.6e+255))) tmp = t_0 * (0.5 * (1.0 + (-0.5 * (re * re)))); else tmp = 0.5 * t_0; end tmp_2 = tmp; end
code[re_, im_] := Block[{t$95$0 = N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[im, -5e+89], N[Not[LessEqual[im, 2.6e+255]], $MachinePrecision]], N[(t$95$0 * N[(0.5 * N[(1.0 + N[(-0.5 * N[(re * re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(0.5 * t$95$0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := e^{-im} + e^{im}\\
\mathbf{if}\;im \leq -5 \cdot 10^{+89} \lor \neg \left(im \leq 2.6 \cdot 10^{+255}\right):\\
\;\;\;\;t_0 \cdot \left(0.5 \cdot \left(1 + -0.5 \cdot \left(re \cdot re\right)\right)\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot t_0\\
\end{array}
\end{array}
if im < -4.99999999999999983e89 or 2.6000000000000001e255 < im Initial program 100.0%
Taylor expanded in re around 0 89.7%
unpow289.7%
Simplified89.7%
if -4.99999999999999983e89 < im < 2.6000000000000001e255Initial program 100.0%
Taylor expanded in re around 0 63.7%
Final simplification69.6%
(FPCore (re im) :precision binary64 (* 0.5 (+ (exp (- im)) (exp im))))
double code(double re, double im) {
return 0.5 * (exp(-im) + exp(im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.5d0 * (exp(-im) + exp(im))
end function
public static double code(double re, double im) {
return 0.5 * (Math.exp(-im) + Math.exp(im));
}
def code(re, im): return 0.5 * (math.exp(-im) + math.exp(im))
function code(re, im) return Float64(0.5 * Float64(exp(Float64(-im)) + exp(im))) end
function tmp = code(re, im) tmp = 0.5 * (exp(-im) + exp(im)); end
code[re_, im_] := N[(0.5 * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \left(e^{-im} + e^{im}\right)
\end{array}
Initial program 100.0%
Taylor expanded in re around 0 63.7%
Final simplification63.7%
herbie shell --seed 2023195
(FPCore (re im)
:name "math.cos on complex, real part"
:precision binary64
(* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))