
(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 11 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 (if (or (<= im 8.0) (not (<= im 2.15e+151))) (* (cos re) (fma (* 0.5 im) im 1.0)) (* 0.5 (+ (exp (- im)) (exp im)))))
double code(double re, double im) {
double tmp;
if ((im <= 8.0) || !(im <= 2.15e+151)) {
tmp = cos(re) * fma((0.5 * im), im, 1.0);
} else {
tmp = 0.5 * (exp(-im) + exp(im));
}
return tmp;
}
function code(re, im) tmp = 0.0 if ((im <= 8.0) || !(im <= 2.15e+151)) tmp = Float64(cos(re) * fma(Float64(0.5 * im), im, 1.0)); else tmp = Float64(0.5 * Float64(exp(Float64(-im)) + exp(im))); end return tmp end
code[re_, im_] := If[Or[LessEqual[im, 8.0], N[Not[LessEqual[im, 2.15e+151]], $MachinePrecision]], N[(N[Cos[re], $MachinePrecision] * N[(N[(0.5 * im), $MachinePrecision] * im + 1.0), $MachinePrecision]), $MachinePrecision], N[(0.5 * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 8 \lor \neg \left(im \leq 2.15 \cdot 10^{+151}\right):\\
\;\;\;\;\cos re \cdot \mathsf{fma}\left(0.5 \cdot im, im, 1\right)\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\
\end{array}
\end{array}
if im < 8 or 2.14999999999999991e151 < im Initial program 100.0%
Taylor expanded in im around 0 88.0%
*-lft-identity88.0%
associate-*r*88.0%
distribute-rgt-out88.0%
Simplified88.0%
+-commutative88.0%
unpow288.0%
associate-*r*88.0%
fma-define88.0%
Applied egg-rr88.0%
if 8 < im < 2.14999999999999991e151Initial program 100.0%
Taylor expanded in re around 0 75.7%
Final simplification86.2%
(FPCore (re im)
:precision binary64
(if (<= im 8.0)
(cos re)
(if (<= im 2.15e+151)
(* 0.5 (+ (exp (- im)) (exp im)))
(* (cos re) (* 0.5 (pow im 2.0))))))
double code(double re, double im) {
double tmp;
if (im <= 8.0) {
tmp = cos(re);
} else if (im <= 2.15e+151) {
tmp = 0.5 * (exp(-im) + exp(im));
} else {
tmp = cos(re) * (0.5 * pow(im, 2.0));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 8.0d0) then
tmp = cos(re)
else if (im <= 2.15d+151) then
tmp = 0.5d0 * (exp(-im) + exp(im))
else
tmp = cos(re) * (0.5d0 * (im ** 2.0d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 8.0) {
tmp = Math.cos(re);
} else if (im <= 2.15e+151) {
tmp = 0.5 * (Math.exp(-im) + Math.exp(im));
} else {
tmp = Math.cos(re) * (0.5 * Math.pow(im, 2.0));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 8.0: tmp = math.cos(re) elif im <= 2.15e+151: tmp = 0.5 * (math.exp(-im) + math.exp(im)) else: tmp = math.cos(re) * (0.5 * math.pow(im, 2.0)) return tmp
function code(re, im) tmp = 0.0 if (im <= 8.0) tmp = cos(re); elseif (im <= 2.15e+151) tmp = Float64(0.5 * Float64(exp(Float64(-im)) + exp(im))); else tmp = Float64(cos(re) * Float64(0.5 * (im ^ 2.0))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 8.0) tmp = cos(re); elseif (im <= 2.15e+151) tmp = 0.5 * (exp(-im) + exp(im)); else tmp = cos(re) * (0.5 * (im ^ 2.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 8.0], N[Cos[re], $MachinePrecision], If[LessEqual[im, 2.15e+151], N[(0.5 * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[re], $MachinePrecision] * N[(0.5 * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 8:\\
\;\;\;\;\cos re\\
\mathbf{elif}\;im \leq 2.15 \cdot 10^{+151}:\\
\;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\
\mathbf{else}:\\
\;\;\;\;\cos re \cdot \left(0.5 \cdot {im}^{2}\right)\\
\end{array}
\end{array}
if im < 8Initial program 100.0%
Taylor expanded in im around 0 67.0%
if 8 < im < 2.14999999999999991e151Initial program 100.0%
Taylor expanded in re around 0 75.7%
if 2.14999999999999991e151 < im Initial program 100.0%
Taylor expanded in im around 0 97.4%
*-lft-identity97.4%
associate-*r*97.4%
distribute-rgt-out97.4%
Simplified97.4%
Taylor expanded in im around inf 97.4%
associate-*r*97.4%
Simplified97.4%
Final simplification72.3%
(FPCore (re im) :precision binary64 (if (<= im 8.0) (cos re) (* 0.5 (+ (exp (- im)) (exp im)))))
double code(double re, double im) {
double tmp;
if (im <= 8.0) {
tmp = cos(re);
} else {
tmp = 0.5 * (exp(-im) + exp(im));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 8.0d0) then
tmp = cos(re)
else
tmp = 0.5d0 * (exp(-im) + exp(im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 8.0) {
tmp = Math.cos(re);
} else {
tmp = 0.5 * (Math.exp(-im) + Math.exp(im));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 8.0: tmp = math.cos(re) else: tmp = 0.5 * (math.exp(-im) + math.exp(im)) return tmp
function code(re, im) tmp = 0.0 if (im <= 8.0) tmp = cos(re); else tmp = Float64(0.5 * Float64(exp(Float64(-im)) + exp(im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 8.0) tmp = cos(re); else tmp = 0.5 * (exp(-im) + exp(im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 8.0], N[Cos[re], $MachinePrecision], N[(0.5 * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 8:\\
\;\;\;\;\cos re\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\
\end{array}
\end{array}
if im < 8Initial program 100.0%
Taylor expanded in im around 0 67.0%
if 8 < im Initial program 100.0%
Taylor expanded in re around 0 70.4%
Final simplification68.0%
(FPCore (re im)
:precision binary64
(if (<= im 7e+39)
(cos re)
(if (<= im 8.5e+239)
(+ 0.25 (* 0.25 (pow re 2.0)))
(+ 1.0 (* (pow re 2.0) -0.5)))))
double code(double re, double im) {
double tmp;
if (im <= 7e+39) {
tmp = cos(re);
} else if (im <= 8.5e+239) {
tmp = 0.25 + (0.25 * pow(re, 2.0));
} else {
tmp = 1.0 + (pow(re, 2.0) * -0.5);
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 7d+39) then
tmp = cos(re)
else if (im <= 8.5d+239) then
tmp = 0.25d0 + (0.25d0 * (re ** 2.0d0))
else
tmp = 1.0d0 + ((re ** 2.0d0) * (-0.5d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 7e+39) {
tmp = Math.cos(re);
} else if (im <= 8.5e+239) {
tmp = 0.25 + (0.25 * Math.pow(re, 2.0));
} else {
tmp = 1.0 + (Math.pow(re, 2.0) * -0.5);
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 7e+39: tmp = math.cos(re) elif im <= 8.5e+239: tmp = 0.25 + (0.25 * math.pow(re, 2.0)) else: tmp = 1.0 + (math.pow(re, 2.0) * -0.5) return tmp
function code(re, im) tmp = 0.0 if (im <= 7e+39) tmp = cos(re); elseif (im <= 8.5e+239) tmp = Float64(0.25 + Float64(0.25 * (re ^ 2.0))); else tmp = Float64(1.0 + Float64((re ^ 2.0) * -0.5)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 7e+39) tmp = cos(re); elseif (im <= 8.5e+239) tmp = 0.25 + (0.25 * (re ^ 2.0)); else tmp = 1.0 + ((re ^ 2.0) * -0.5); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 7e+39], N[Cos[re], $MachinePrecision], If[LessEqual[im, 8.5e+239], N[(0.25 + N[(0.25 * N[Power[re, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(N[Power[re, 2.0], $MachinePrecision] * -0.5), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 7 \cdot 10^{+39}:\\
\;\;\;\;\cos re\\
\mathbf{elif}\;im \leq 8.5 \cdot 10^{+239}:\\
\;\;\;\;0.25 + 0.25 \cdot {re}^{2}\\
\mathbf{else}:\\
\;\;\;\;1 + {re}^{2} \cdot -0.5\\
\end{array}
\end{array}
if im < 7.0000000000000003e39Initial program 100.0%
Taylor expanded in im around 0 64.1%
if 7.0000000000000003e39 < im < 8.50000000000000021e239Initial program 100.0%
Applied egg-rr2.5%
Taylor expanded in re around 0 14.0%
*-commutative14.0%
Simplified14.0%
if 8.50000000000000021e239 < im Initial program 100.0%
Taylor expanded in im around 0 3.1%
Taylor expanded in re around 0 47.2%
Final simplification53.4%
(FPCore (re im)
:precision binary64
(if (<= im 5e+39)
(cos re)
(if (<= im 2e+126)
(+ 0.25 (* 0.25 (pow re 2.0)))
(+ 1.0 (* 0.5 (pow im 2.0))))))
double code(double re, double im) {
double tmp;
if (im <= 5e+39) {
tmp = cos(re);
} else if (im <= 2e+126) {
tmp = 0.25 + (0.25 * pow(re, 2.0));
} else {
tmp = 1.0 + (0.5 * pow(im, 2.0));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 5d+39) then
tmp = cos(re)
else if (im <= 2d+126) then
tmp = 0.25d0 + (0.25d0 * (re ** 2.0d0))
else
tmp = 1.0d0 + (0.5d0 * (im ** 2.0d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 5e+39) {
tmp = Math.cos(re);
} else if (im <= 2e+126) {
tmp = 0.25 + (0.25 * Math.pow(re, 2.0));
} else {
tmp = 1.0 + (0.5 * Math.pow(im, 2.0));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 5e+39: tmp = math.cos(re) elif im <= 2e+126: tmp = 0.25 + (0.25 * math.pow(re, 2.0)) else: tmp = 1.0 + (0.5 * math.pow(im, 2.0)) return tmp
function code(re, im) tmp = 0.0 if (im <= 5e+39) tmp = cos(re); elseif (im <= 2e+126) tmp = Float64(0.25 + Float64(0.25 * (re ^ 2.0))); else tmp = Float64(1.0 + Float64(0.5 * (im ^ 2.0))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 5e+39) tmp = cos(re); elseif (im <= 2e+126) tmp = 0.25 + (0.25 * (re ^ 2.0)); else tmp = 1.0 + (0.5 * (im ^ 2.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 5e+39], N[Cos[re], $MachinePrecision], If[LessEqual[im, 2e+126], N[(0.25 + N[(0.25 * N[Power[re, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(0.5 * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 5 \cdot 10^{+39}:\\
\;\;\;\;\cos re\\
\mathbf{elif}\;im \leq 2 \cdot 10^{+126}:\\
\;\;\;\;0.25 + 0.25 \cdot {re}^{2}\\
\mathbf{else}:\\
\;\;\;\;1 + 0.5 \cdot {im}^{2}\\
\end{array}
\end{array}
if im < 5.00000000000000015e39Initial program 100.0%
Taylor expanded in im around 0 64.1%
if 5.00000000000000015e39 < im < 1.99999999999999985e126Initial program 100.0%
Applied egg-rr2.6%
Taylor expanded in re around 0 14.9%
*-commutative14.9%
Simplified14.9%
if 1.99999999999999985e126 < im Initial program 100.0%
Taylor expanded in im around 0 88.2%
*-lft-identity88.2%
associate-*r*88.2%
distribute-rgt-out88.2%
Simplified88.2%
Taylor expanded in re around 0 58.7%
Final simplification58.7%
(FPCore (re im) :precision binary64 (if (<= im 1.5e+40) (cos re) (+ 0.25 (* 0.25 (pow re 2.0)))))
double code(double re, double im) {
double tmp;
if (im <= 1.5e+40) {
tmp = cos(re);
} else {
tmp = 0.25 + (0.25 * pow(re, 2.0));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 1.5d+40) then
tmp = cos(re)
else
tmp = 0.25d0 + (0.25d0 * (re ** 2.0d0))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 1.5e+40) {
tmp = Math.cos(re);
} else {
tmp = 0.25 + (0.25 * Math.pow(re, 2.0));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 1.5e+40: tmp = math.cos(re) else: tmp = 0.25 + (0.25 * math.pow(re, 2.0)) return tmp
function code(re, im) tmp = 0.0 if (im <= 1.5e+40) tmp = cos(re); else tmp = Float64(0.25 + Float64(0.25 * (re ^ 2.0))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 1.5e+40) tmp = cos(re); else tmp = 0.25 + (0.25 * (re ^ 2.0)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 1.5e+40], N[Cos[re], $MachinePrecision], N[(0.25 + N[(0.25 * N[Power[re, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 1.5 \cdot 10^{+40}:\\
\;\;\;\;\cos re\\
\mathbf{else}:\\
\;\;\;\;0.25 + 0.25 \cdot {re}^{2}\\
\end{array}
\end{array}
if im < 1.5000000000000001e40Initial program 100.0%
Taylor expanded in im around 0 64.1%
if 1.5000000000000001e40 < im Initial program 100.0%
Applied egg-rr2.4%
Taylor expanded in re around 0 11.8%
*-commutative11.8%
Simplified11.8%
Final simplification51.4%
(FPCore (re im) :precision binary64 (cos re))
double code(double re, double im) {
return cos(re);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = cos(re)
end function
public static double code(double re, double im) {
return Math.cos(re);
}
def code(re, im): return math.cos(re)
function code(re, im) return cos(re) end
function tmp = code(re, im) tmp = cos(re); end
code[re_, im_] := N[Cos[re], $MachinePrecision]
\begin{array}{l}
\\
\cos re
\end{array}
Initial program 100.0%
Taylor expanded in im around 0 49.3%
Final simplification49.3%
(FPCore (re im) :precision binary64 -4.0)
double code(double re, double im) {
return -4.0;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = -4.0d0
end function
public static double code(double re, double im) {
return -4.0;
}
def code(re, im): return -4.0
function code(re, im) return -4.0 end
function tmp = code(re, im) tmp = -4.0; end
code[re_, im_] := -4.0
\begin{array}{l}
\\
-4
\end{array}
Initial program 100.0%
Taylor expanded in im around 0 49.3%
Applied egg-rr4.1%
*-commutative4.1%
Simplified4.1%
Taylor expanded in re around 0 3.8%
Final simplification3.8%
(FPCore (re im) :precision binary64 0.25)
double code(double re, double im) {
return 0.25;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 0.25d0
end function
public static double code(double re, double im) {
return 0.25;
}
def code(re, im): return 0.25
function code(re, im) return 0.25 end
function tmp = code(re, im) tmp = 0.25; end
code[re_, im_] := 0.25
\begin{array}{l}
\\
0.25
\end{array}
Initial program 100.0%
Applied egg-rr7.1%
Taylor expanded in re around 0 7.2%
Final simplification7.2%
(FPCore (re im) :precision binary64 1.0)
double code(double re, double im) {
return 1.0;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = 1.0d0
end function
public static double code(double re, double im) {
return 1.0;
}
def code(re, im): return 1.0
function code(re, im) return 1.0 end
function tmp = code(re, im) tmp = 1.0; end
code[re_, im_] := 1.0
\begin{array}{l}
\\
1
\end{array}
Initial program 100.0%
Taylor expanded in im around 0 49.3%
Taylor expanded in re around 0 26.4%
Final simplification26.4%
herbie shell --seed 2024075
(FPCore (re im)
:name "math.cos on complex, real part"
:precision binary64
(* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))