
(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 9 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 (<= im 0.025)
(cos re)
(if (<= im 1.2e+77)
(* 0.5 (+ (exp (- im)) (exp im)))
(* (pow im 4.0) (* (cos re) 0.041666666666666664)))))
double code(double re, double im) {
double tmp;
if (im <= 0.025) {
tmp = cos(re);
} else if (im <= 1.2e+77) {
tmp = 0.5 * (exp(-im) + exp(im));
} else {
tmp = pow(im, 4.0) * (cos(re) * 0.041666666666666664);
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 0.025d0) then
tmp = cos(re)
else if (im <= 1.2d+77) then
tmp = 0.5d0 * (exp(-im) + exp(im))
else
tmp = (im ** 4.0d0) * (cos(re) * 0.041666666666666664d0)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 0.025) {
tmp = Math.cos(re);
} else if (im <= 1.2e+77) {
tmp = 0.5 * (Math.exp(-im) + Math.exp(im));
} else {
tmp = Math.pow(im, 4.0) * (Math.cos(re) * 0.041666666666666664);
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 0.025: tmp = math.cos(re) elif im <= 1.2e+77: tmp = 0.5 * (math.exp(-im) + math.exp(im)) else: tmp = math.pow(im, 4.0) * (math.cos(re) * 0.041666666666666664) return tmp
function code(re, im) tmp = 0.0 if (im <= 0.025) tmp = cos(re); elseif (im <= 1.2e+77) tmp = Float64(0.5 * Float64(exp(Float64(-im)) + exp(im))); else tmp = Float64((im ^ 4.0) * Float64(cos(re) * 0.041666666666666664)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 0.025) tmp = cos(re); elseif (im <= 1.2e+77) tmp = 0.5 * (exp(-im) + exp(im)); else tmp = (im ^ 4.0) * (cos(re) * 0.041666666666666664); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 0.025], N[Cos[re], $MachinePrecision], If[LessEqual[im, 1.2e+77], N[(0.5 * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Power[im, 4.0], $MachinePrecision] * N[(N[Cos[re], $MachinePrecision] * 0.041666666666666664), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 0.025:\\
\;\;\;\;\cos re\\
\mathbf{elif}\;im \leq 1.2 \cdot 10^{+77}:\\
\;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\
\mathbf{else}:\\
\;\;\;\;{im}^{4} \cdot \left(\cos re \cdot 0.041666666666666664\right)\\
\end{array}
\end{array}
if im < 0.025000000000000001Initial program 100.0%
Taylor expanded in im around 0 62.3%
if 0.025000000000000001 < im < 1.1999999999999999e77Initial program 100.0%
Taylor expanded in re around 0 66.7%
if 1.1999999999999999e77 < im Initial program 100.0%
Taylor expanded in im around 0 100.0%
Taylor expanded in im around inf 100.0%
associate-*r*100.0%
*-commutative100.0%
associate-*l*100.0%
Simplified100.0%
Final simplification70.6%
(FPCore (re im)
:precision binary64
(if (<= im 0.094)
(* (* 0.5 (cos re)) (+ 2.0 (pow im 2.0)))
(if (<= im 1.2e+77)
(* 0.5 (+ (exp (- im)) (exp im)))
(* (pow im 4.0) (* (cos re) 0.041666666666666664)))))
double code(double re, double im) {
double tmp;
if (im <= 0.094) {
tmp = (0.5 * cos(re)) * (2.0 + pow(im, 2.0));
} else if (im <= 1.2e+77) {
tmp = 0.5 * (exp(-im) + exp(im));
} else {
tmp = pow(im, 4.0) * (cos(re) * 0.041666666666666664);
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 0.094d0) then
tmp = (0.5d0 * cos(re)) * (2.0d0 + (im ** 2.0d0))
else if (im <= 1.2d+77) then
tmp = 0.5d0 * (exp(-im) + exp(im))
else
tmp = (im ** 4.0d0) * (cos(re) * 0.041666666666666664d0)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 0.094) {
tmp = (0.5 * Math.cos(re)) * (2.0 + Math.pow(im, 2.0));
} else if (im <= 1.2e+77) {
tmp = 0.5 * (Math.exp(-im) + Math.exp(im));
} else {
tmp = Math.pow(im, 4.0) * (Math.cos(re) * 0.041666666666666664);
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 0.094: tmp = (0.5 * math.cos(re)) * (2.0 + math.pow(im, 2.0)) elif im <= 1.2e+77: tmp = 0.5 * (math.exp(-im) + math.exp(im)) else: tmp = math.pow(im, 4.0) * (math.cos(re) * 0.041666666666666664) return tmp
function code(re, im) tmp = 0.0 if (im <= 0.094) tmp = Float64(Float64(0.5 * cos(re)) * Float64(2.0 + (im ^ 2.0))); elseif (im <= 1.2e+77) tmp = Float64(0.5 * Float64(exp(Float64(-im)) + exp(im))); else tmp = Float64((im ^ 4.0) * Float64(cos(re) * 0.041666666666666664)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 0.094) tmp = (0.5 * cos(re)) * (2.0 + (im ^ 2.0)); elseif (im <= 1.2e+77) tmp = 0.5 * (exp(-im) + exp(im)); else tmp = (im ^ 4.0) * (cos(re) * 0.041666666666666664); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 0.094], N[(N[(0.5 * N[Cos[re], $MachinePrecision]), $MachinePrecision] * N[(2.0 + N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 1.2e+77], N[(0.5 * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Power[im, 4.0], $MachinePrecision] * N[(N[Cos[re], $MachinePrecision] * 0.041666666666666664), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 0.094:\\
\;\;\;\;\left(0.5 \cdot \cos re\right) \cdot \left(2 + {im}^{2}\right)\\
\mathbf{elif}\;im \leq 1.2 \cdot 10^{+77}:\\
\;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\
\mathbf{else}:\\
\;\;\;\;{im}^{4} \cdot \left(\cos re \cdot 0.041666666666666664\right)\\
\end{array}
\end{array}
if im < 0.094Initial program 100.0%
Taylor expanded in im around 0 84.6%
if 0.094 < im < 1.1999999999999999e77Initial program 100.0%
Taylor expanded in re around 0 66.7%
if 1.1999999999999999e77 < im Initial program 100.0%
Taylor expanded in im around 0 100.0%
Taylor expanded in im around inf 100.0%
associate-*r*100.0%
*-commutative100.0%
associate-*l*100.0%
Simplified100.0%
Final simplification87.1%
(FPCore (re im) :precision binary64 (if (<= im 0.037) (cos re) (* 0.5 (+ (exp (- im)) (exp im)))))
double code(double re, double im) {
double tmp;
if (im <= 0.037) {
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 <= 0.037d0) 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 <= 0.037) {
tmp = Math.cos(re);
} else {
tmp = 0.5 * (Math.exp(-im) + Math.exp(im));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 0.037: 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 <= 0.037) 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 <= 0.037) tmp = cos(re); else tmp = 0.5 * (exp(-im) + exp(im)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 0.037], 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 0.037:\\
\;\;\;\;\cos re\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(e^{-im} + e^{im}\right)\\
\end{array}
\end{array}
if im < 0.0369999999999999982Initial program 100.0%
Taylor expanded in im around 0 62.3%
if 0.0369999999999999982 < im Initial program 100.0%
Taylor expanded in re around 0 67.2%
Final simplification63.5%
(FPCore (re im)
:precision binary64
(if (<= im 12500.0)
(cos re)
(if (<= im 1.1e+79)
(+ 1.0 (* re (* re -0.5)))
(* (pow im 4.0) 0.041666666666666664))))
double code(double re, double im) {
double tmp;
if (im <= 12500.0) {
tmp = cos(re);
} else if (im <= 1.1e+79) {
tmp = 1.0 + (re * (re * -0.5));
} else {
tmp = pow(im, 4.0) * 0.041666666666666664;
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 12500.0d0) then
tmp = cos(re)
else if (im <= 1.1d+79) then
tmp = 1.0d0 + (re * (re * (-0.5d0)))
else
tmp = (im ** 4.0d0) * 0.041666666666666664d0
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 12500.0) {
tmp = Math.cos(re);
} else if (im <= 1.1e+79) {
tmp = 1.0 + (re * (re * -0.5));
} else {
tmp = Math.pow(im, 4.0) * 0.041666666666666664;
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 12500.0: tmp = math.cos(re) elif im <= 1.1e+79: tmp = 1.0 + (re * (re * -0.5)) else: tmp = math.pow(im, 4.0) * 0.041666666666666664 return tmp
function code(re, im) tmp = 0.0 if (im <= 12500.0) tmp = cos(re); elseif (im <= 1.1e+79) tmp = Float64(1.0 + Float64(re * Float64(re * -0.5))); else tmp = Float64((im ^ 4.0) * 0.041666666666666664); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 12500.0) tmp = cos(re); elseif (im <= 1.1e+79) tmp = 1.0 + (re * (re * -0.5)); else tmp = (im ^ 4.0) * 0.041666666666666664; end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 12500.0], N[Cos[re], $MachinePrecision], If[LessEqual[im, 1.1e+79], N[(1.0 + N[(re * N[(re * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Power[im, 4.0], $MachinePrecision] * 0.041666666666666664), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 12500:\\
\;\;\;\;\cos re\\
\mathbf{elif}\;im \leq 1.1 \cdot 10^{+79}:\\
\;\;\;\;1 + re \cdot \left(re \cdot -0.5\right)\\
\mathbf{else}:\\
\;\;\;\;{im}^{4} \cdot 0.041666666666666664\\
\end{array}
\end{array}
if im < 12500Initial program 100.0%
Taylor expanded in im around 0 62.0%
if 12500 < im < 1.0999999999999999e79Initial program 100.0%
Taylor expanded in im around 0 3.1%
Taylor expanded in re around 0 35.4%
Simplified35.4%
log1p-expm1-u43.2%
Applied egg-rr43.2%
log1p-expm135.4%
*-commutative35.4%
unpow235.4%
associate-*r*35.4%
Applied egg-rr35.4%
if 1.0999999999999999e79 < im Initial program 100.0%
Taylor expanded in im around 0 100.0%
Taylor expanded in im around inf 100.0%
associate-*r*100.0%
*-commutative100.0%
associate-*l*100.0%
Simplified100.0%
Taylor expanded in re around 0 68.5%
Final simplification62.1%
(FPCore (re im) :precision binary64 (if (<= im 10500.0) (cos re) (+ 1.0 (* re (* re -0.5)))))
double code(double re, double im) {
double tmp;
if (im <= 10500.0) {
tmp = cos(re);
} else {
tmp = 1.0 + (re * (re * -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 <= 10500.0d0) then
tmp = cos(re)
else
tmp = 1.0d0 + (re * (re * (-0.5d0)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 10500.0) {
tmp = Math.cos(re);
} else {
tmp = 1.0 + (re * (re * -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 10500.0: tmp = math.cos(re) else: tmp = 1.0 + (re * (re * -0.5)) return tmp
function code(re, im) tmp = 0.0 if (im <= 10500.0) tmp = cos(re); else tmp = Float64(1.0 + Float64(re * Float64(re * -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 10500.0) tmp = cos(re); else tmp = 1.0 + (re * (re * -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 10500.0], N[Cos[re], $MachinePrecision], N[(1.0 + N[(re * N[(re * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 10500:\\
\;\;\;\;\cos re\\
\mathbf{else}:\\
\;\;\;\;1 + re \cdot \left(re \cdot -0.5\right)\\
\end{array}
\end{array}
if im < 10500Initial program 100.0%
Taylor expanded in im around 0 62.0%
if 10500 < im Initial program 100.0%
Taylor expanded in im around 0 3.1%
Taylor expanded in re around 0 16.4%
Simplified16.4%
log1p-expm1-u34.9%
Applied egg-rr34.9%
log1p-expm116.4%
*-commutative16.4%
unpow216.4%
associate-*r*16.4%
Applied egg-rr16.4%
Final simplification50.2%
(FPCore (re im) :precision binary64 (if (<= im 10500.0) 1.0 (+ 1.0 (* re (* re -0.5)))))
double code(double re, double im) {
double tmp;
if (im <= 10500.0) {
tmp = 1.0;
} else {
tmp = 1.0 + (re * (re * -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 <= 10500.0d0) then
tmp = 1.0d0
else
tmp = 1.0d0 + (re * (re * (-0.5d0)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 10500.0) {
tmp = 1.0;
} else {
tmp = 1.0 + (re * (re * -0.5));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 10500.0: tmp = 1.0 else: tmp = 1.0 + (re * (re * -0.5)) return tmp
function code(re, im) tmp = 0.0 if (im <= 10500.0) tmp = 1.0; else tmp = Float64(1.0 + Float64(re * Float64(re * -0.5))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 10500.0) tmp = 1.0; else tmp = 1.0 + (re * (re * -0.5)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 10500.0], 1.0, N[(1.0 + N[(re * N[(re * -0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 10500:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;1 + re \cdot \left(re \cdot -0.5\right)\\
\end{array}
\end{array}
if im < 10500Initial program 100.0%
Taylor expanded in im around 0 62.0%
Taylor expanded in re around 0 30.8%
if 10500 < im Initial program 100.0%
Taylor expanded in im around 0 3.1%
Taylor expanded in re around 0 16.4%
Simplified16.4%
log1p-expm1-u34.9%
Applied egg-rr34.9%
log1p-expm116.4%
*-commutative16.4%
unpow216.4%
associate-*r*16.4%
Applied egg-rr16.4%
Final simplification27.1%
(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.5%
Taylor expanded in re around 0 7.5%
Final simplification7.5%
(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 46.8%
Taylor expanded in re around 0 23.5%
Final simplification23.5%
herbie shell --seed 2023326
(FPCore (re im)
:name "math.cos on complex, real part"
:precision binary64
(* (* 0.5 (cos re)) (+ (exp (- im)) (exp im))))