
(FPCore (re im) :precision binary64 (* (* 0.5 (sin re)) (+ (exp (- 0.0 im)) (exp im))))
double code(double re, double im) {
return (0.5 * sin(re)) * (exp((0.0 - im)) + exp(im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = (0.5d0 * sin(re)) * (exp((0.0d0 - im)) + exp(im))
end function
public static double code(double re, double im) {
return (0.5 * Math.sin(re)) * (Math.exp((0.0 - im)) + Math.exp(im));
}
def code(re, im): return (0.5 * math.sin(re)) * (math.exp((0.0 - im)) + math.exp(im))
function code(re, im) return Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(0.0 - im)) + exp(im))) end
function tmp = code(re, im) tmp = (0.5 * sin(re)) * (exp((0.0 - im)) + exp(im)); end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[N[(0.0 - im), $MachinePrecision]], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(0.5 \cdot \sin re\right) \cdot \left(e^{0 - im} + e^{im}\right)
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (re im) :precision binary64 (* (* 0.5 (sin re)) (+ (exp (- 0.0 im)) (exp im))))
double code(double re, double im) {
return (0.5 * sin(re)) * (exp((0.0 - im)) + exp(im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = (0.5d0 * sin(re)) * (exp((0.0d0 - im)) + exp(im))
end function
public static double code(double re, double im) {
return (0.5 * Math.sin(re)) * (Math.exp((0.0 - im)) + Math.exp(im));
}
def code(re, im): return (0.5 * math.sin(re)) * (math.exp((0.0 - im)) + math.exp(im))
function code(re, im) return Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(0.0 - im)) + exp(im))) end
function tmp = code(re, im) tmp = (0.5 * sin(re)) * (exp((0.0 - im)) + exp(im)); end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[N[(0.0 - im), $MachinePrecision]], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(0.5 \cdot \sin re\right) \cdot \left(e^{0 - im} + e^{im}\right)
\end{array}
(FPCore (re im) :precision binary64 (* (* 0.5 (sin re)) (+ (exp (- im)) (exp im))))
double code(double re, double im) {
return (0.5 * sin(re)) * (exp(-im) + exp(im));
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = (0.5d0 * sin(re)) * (exp(-im) + exp(im))
end function
public static double code(double re, double im) {
return (0.5 * Math.sin(re)) * (Math.exp(-im) + Math.exp(im));
}
def code(re, im): return (0.5 * math.sin(re)) * (math.exp(-im) + math.exp(im))
function code(re, im) return Float64(Float64(0.5 * sin(re)) * Float64(exp(Float64(-im)) + exp(im))) end
function tmp = code(re, im) tmp = (0.5 * sin(re)) * (exp(-im) + exp(im)); end
code[re_, im_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(0.5 \cdot \sin re\right) \cdot \left(e^{-im} + e^{im}\right)
\end{array}
Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
(FPCore (re im)
:precision binary64
(if (<= im 0.058)
(* (sin re) (fma (* 0.5 im) im 1.0))
(if (<= im 3.7e+149)
(* (* 0.5 re) (+ (exp (- im)) (exp im)))
(* (sin re) (* 0.5 (pow im 2.0))))))
double code(double re, double im) {
double tmp;
if (im <= 0.058) {
tmp = sin(re) * fma((0.5 * im), im, 1.0);
} else if (im <= 3.7e+149) {
tmp = (0.5 * re) * (exp(-im) + exp(im));
} else {
tmp = sin(re) * (0.5 * pow(im, 2.0));
}
return tmp;
}
function code(re, im) tmp = 0.0 if (im <= 0.058) tmp = Float64(sin(re) * fma(Float64(0.5 * im), im, 1.0)); elseif (im <= 3.7e+149) tmp = Float64(Float64(0.5 * re) * Float64(exp(Float64(-im)) + exp(im))); else tmp = Float64(sin(re) * Float64(0.5 * (im ^ 2.0))); end return tmp end
code[re_, im_] := If[LessEqual[im, 0.058], N[(N[Sin[re], $MachinePrecision] * N[(N[(0.5 * im), $MachinePrecision] * im + 1.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[im, 3.7e+149], N[(N[(0.5 * re), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Sin[re], $MachinePrecision] * N[(0.5 * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 0.058:\\
\;\;\;\;\sin re \cdot \mathsf{fma}\left(0.5 \cdot im, im, 1\right)\\
\mathbf{elif}\;im \leq 3.7 \cdot 10^{+149}:\\
\;\;\;\;\left(0.5 \cdot re\right) \cdot \left(e^{-im} + e^{im}\right)\\
\mathbf{else}:\\
\;\;\;\;\sin re \cdot \left(0.5 \cdot {im}^{2}\right)\\
\end{array}
\end{array}
if im < 0.0580000000000000029Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in im around 0 84.4%
Simplified84.4%
unpow284.4%
associate-*r*84.4%
fma-define84.4%
Applied egg-rr84.4%
if 0.0580000000000000029 < im < 3.69999999999999978e149Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in re around 0 85.2%
Simplified85.2%
if 3.69999999999999978e149 < im Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in im around 0 97.8%
Simplified97.8%
Taylor expanded in im around inf 97.8%
Final simplification86.6%
(FPCore (re im) :precision binary64 (if (or (<= im 580.0) (not (<= im 4.3e+142))) (* (sin re) (fma (* 0.5 im) im 1.0)) (log1p (expm1 re))))
double code(double re, double im) {
double tmp;
if ((im <= 580.0) || !(im <= 4.3e+142)) {
tmp = sin(re) * fma((0.5 * im), im, 1.0);
} else {
tmp = log1p(expm1(re));
}
return tmp;
}
function code(re, im) tmp = 0.0 if ((im <= 580.0) || !(im <= 4.3e+142)) tmp = Float64(sin(re) * fma(Float64(0.5 * im), im, 1.0)); else tmp = log1p(expm1(re)); end return tmp end
code[re_, im_] := If[Or[LessEqual[im, 580.0], N[Not[LessEqual[im, 4.3e+142]], $MachinePrecision]], N[(N[Sin[re], $MachinePrecision] * N[(N[(0.5 * im), $MachinePrecision] * im + 1.0), $MachinePrecision]), $MachinePrecision], N[Log[1 + N[(Exp[re] - 1), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 580 \lor \neg \left(im \leq 4.3 \cdot 10^{+142}\right):\\
\;\;\;\;\sin re \cdot \mathsf{fma}\left(0.5 \cdot im, im, 1\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(re\right)\right)\\
\end{array}
\end{array}
if im < 580 or 4.30000000000000012e142 < im Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in im around 0 86.2%
Simplified86.2%
unpow286.2%
associate-*r*86.2%
fma-define86.2%
Applied egg-rr86.2%
if 580 < im < 4.30000000000000012e142Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in re around 0 84.0%
Simplified84.0%
Applied egg-rr29.3%
Final simplification80.7%
(FPCore (re im) :precision binary64 (if (<= im 0.45) (sin re) (if (<= im 4.3e+142) (log1p (expm1 re)) (* (sin re) (* 0.5 (pow im 2.0))))))
double code(double re, double im) {
double tmp;
if (im <= 0.45) {
tmp = sin(re);
} else if (im <= 4.3e+142) {
tmp = log1p(expm1(re));
} else {
tmp = sin(re) * (0.5 * pow(im, 2.0));
}
return tmp;
}
public static double code(double re, double im) {
double tmp;
if (im <= 0.45) {
tmp = Math.sin(re);
} else if (im <= 4.3e+142) {
tmp = Math.log1p(Math.expm1(re));
} else {
tmp = Math.sin(re) * (0.5 * Math.pow(im, 2.0));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 0.45: tmp = math.sin(re) elif im <= 4.3e+142: tmp = math.log1p(math.expm1(re)) else: tmp = math.sin(re) * (0.5 * math.pow(im, 2.0)) return tmp
function code(re, im) tmp = 0.0 if (im <= 0.45) tmp = sin(re); elseif (im <= 4.3e+142) tmp = log1p(expm1(re)); else tmp = Float64(sin(re) * Float64(0.5 * (im ^ 2.0))); end return tmp end
code[re_, im_] := If[LessEqual[im, 0.45], N[Sin[re], $MachinePrecision], If[LessEqual[im, 4.3e+142], N[Log[1 + N[(Exp[re] - 1), $MachinePrecision]], $MachinePrecision], N[(N[Sin[re], $MachinePrecision] * N[(0.5 * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 0.45:\\
\;\;\;\;\sin re\\
\mathbf{elif}\;im \leq 4.3 \cdot 10^{+142}:\\
\;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(re\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\sin re \cdot \left(0.5 \cdot {im}^{2}\right)\\
\end{array}
\end{array}
if im < 0.450000000000000011Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in im around 0 64.7%
if 0.450000000000000011 < im < 4.30000000000000012e142Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in re around 0 84.6%
Simplified84.6%
Applied egg-rr29.1%
if 4.30000000000000012e142 < im Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in im around 0 95.7%
Simplified95.7%
Taylor expanded in im around inf 95.7%
Final simplification66.2%
(FPCore (re im) :precision binary64 (if (<= im 85000000.0) (sin re) (if (<= im 1.45e+108) (pow re -4.0) (* (* 0.5 re) (pow im 2.0)))))
double code(double re, double im) {
double tmp;
if (im <= 85000000.0) {
tmp = sin(re);
} else if (im <= 1.45e+108) {
tmp = pow(re, -4.0);
} else {
tmp = (0.5 * re) * 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 <= 85000000.0d0) then
tmp = sin(re)
else if (im <= 1.45d+108) then
tmp = re ** (-4.0d0)
else
tmp = (0.5d0 * re) * (im ** 2.0d0)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 85000000.0) {
tmp = Math.sin(re);
} else if (im <= 1.45e+108) {
tmp = Math.pow(re, -4.0);
} else {
tmp = (0.5 * re) * Math.pow(im, 2.0);
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 85000000.0: tmp = math.sin(re) elif im <= 1.45e+108: tmp = math.pow(re, -4.0) else: tmp = (0.5 * re) * math.pow(im, 2.0) return tmp
function code(re, im) tmp = 0.0 if (im <= 85000000.0) tmp = sin(re); elseif (im <= 1.45e+108) tmp = re ^ -4.0; else tmp = Float64(Float64(0.5 * re) * (im ^ 2.0)); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 85000000.0) tmp = sin(re); elseif (im <= 1.45e+108) tmp = re ^ -4.0; else tmp = (0.5 * re) * (im ^ 2.0); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 85000000.0], N[Sin[re], $MachinePrecision], If[LessEqual[im, 1.45e+108], N[Power[re, -4.0], $MachinePrecision], N[(N[(0.5 * re), $MachinePrecision] * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 85000000:\\
\;\;\;\;\sin re\\
\mathbf{elif}\;im \leq 1.45 \cdot 10^{+108}:\\
\;\;\;\;{re}^{-4}\\
\mathbf{else}:\\
\;\;\;\;\left(0.5 \cdot re\right) \cdot {im}^{2}\\
\end{array}
\end{array}
if im < 8.5e7Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in im around 0 64.2%
if 8.5e7 < im < 1.45000000000000004e108Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in re around 0 87.5%
Simplified87.5%
Applied egg-rr26.3%
if 1.45000000000000004e108 < im Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in im around 0 81.3%
Simplified81.3%
Taylor expanded in im around inf 81.3%
Taylor expanded in re around 0 66.8%
associate-*r*66.8%
*-commutative66.8%
associate-*l*66.8%
Simplified66.8%
Final simplification62.3%
(FPCore (re im) :precision binary64 (if (<= im 85000000.0) (sin re) (pow re -4.0)))
double code(double re, double im) {
double tmp;
if (im <= 85000000.0) {
tmp = sin(re);
} else {
tmp = pow(re, -4.0);
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (im <= 85000000.0d0) then
tmp = sin(re)
else
tmp = re ** (-4.0d0)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 85000000.0) {
tmp = Math.sin(re);
} else {
tmp = Math.pow(re, -4.0);
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 85000000.0: tmp = math.sin(re) else: tmp = math.pow(re, -4.0) return tmp
function code(re, im) tmp = 0.0 if (im <= 85000000.0) tmp = sin(re); else tmp = re ^ -4.0; end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 85000000.0) tmp = sin(re); else tmp = re ^ -4.0; end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 85000000.0], N[Sin[re], $MachinePrecision], N[Power[re, -4.0], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 85000000:\\
\;\;\;\;\sin re\\
\mathbf{else}:\\
\;\;\;\;{re}^{-4}\\
\end{array}
\end{array}
if im < 8.5e7Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in im around 0 64.2%
if 8.5e7 < im Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in re around 0 80.3%
Simplified80.3%
Applied egg-rr19.4%
(FPCore (re im) :precision binary64 (sin re))
double code(double re, double im) {
return sin(re);
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = sin(re)
end function
public static double code(double re, double im) {
return Math.sin(re);
}
def code(re, im): return math.sin(re)
function code(re, im) return sin(re) end
function tmp = code(re, im) tmp = sin(re); end
code[re_, im_] := N[Sin[re], $MachinePrecision]
\begin{array}{l}
\\
\sin re
\end{array}
Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in im around 0 48.3%
(FPCore (re im) :precision binary64 re)
double code(double re, double im) {
return re;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
code = re
end function
public static double code(double re, double im) {
return re;
}
def code(re, im): return re
function code(re, im) return re end
function tmp = code(re, im) tmp = re; end
code[re_, im_] := re
\begin{array}{l}
\\
re
\end{array}
Initial program 100.0%
distribute-rgt-in100.0%
cancel-sign-sub100.0%
distribute-rgt-out--100.0%
sub-neg100.0%
remove-double-neg100.0%
neg-sub0100.0%
Simplified100.0%
Taylor expanded in re around 0 64.3%
Simplified64.3%
Taylor expanded in im around 0 25.8%
herbie shell --seed 2024091
(FPCore (re im)
:name "math.sin on complex, real part"
:precision binary64
(* (* 0.5 (sin re)) (+ (exp (- 0.0 im)) (exp im))))