
(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 (or (<= im 0.009) (not (<= im 1.32e+154))) (* (sin re) (+ (* 0.5 (* im im)) 1.0)) (* (* 0.5 re) (+ (exp (- im)) (exp im)))))
double code(double re, double im) {
double tmp;
if ((im <= 0.009) || !(im <= 1.32e+154)) {
tmp = sin(re) * ((0.5 * (im * im)) + 1.0);
} else {
tmp = (0.5 * re) * (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.009d0) .or. (.not. (im <= 1.32d+154))) then
tmp = sin(re) * ((0.5d0 * (im * im)) + 1.0d0)
else
tmp = (0.5d0 * re) * (exp(-im) + exp(im))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if ((im <= 0.009) || !(im <= 1.32e+154)) {
tmp = Math.sin(re) * ((0.5 * (im * im)) + 1.0);
} else {
tmp = (0.5 * re) * (Math.exp(-im) + Math.exp(im));
}
return tmp;
}
def code(re, im): tmp = 0 if (im <= 0.009) or not (im <= 1.32e+154): tmp = math.sin(re) * ((0.5 * (im * im)) + 1.0) else: tmp = (0.5 * re) * (math.exp(-im) + math.exp(im)) return tmp
function code(re, im) tmp = 0.0 if ((im <= 0.009) || !(im <= 1.32e+154)) tmp = Float64(sin(re) * Float64(Float64(0.5 * Float64(im * im)) + 1.0)); else tmp = Float64(Float64(0.5 * re) * Float64(exp(Float64(-im)) + exp(im))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((im <= 0.009) || ~((im <= 1.32e+154))) tmp = sin(re) * ((0.5 * (im * im)) + 1.0); else tmp = (0.5 * re) * (exp(-im) + exp(im)); end tmp_2 = tmp; end
code[re_, im_] := If[Or[LessEqual[im, 0.009], N[Not[LessEqual[im, 1.32e+154]], $MachinePrecision]], N[(N[Sin[re], $MachinePrecision] * N[(N[(0.5 * N[(im * im), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * re), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 0.009 \lor \neg \left(im \leq 1.32 \cdot 10^{+154}\right):\\
\;\;\;\;\sin re \cdot \left(0.5 \cdot \left(im \cdot im\right) + 1\right)\\
\mathbf{else}:\\
\;\;\;\;\left(0.5 \cdot re\right) \cdot \left(e^{-im} + e^{im}\right)\\
\end{array}
\end{array}
if im < 0.00899999999999999932 or 1.31999999999999998e154 < 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 88.6%
Simplified88.6%
unpow288.6%
Applied egg-rr88.6%
if 0.00899999999999999932 < im < 1.31999999999999998e154Initial program 99.8%
distribute-rgt-in99.9%
cancel-sign-sub99.9%
distribute-rgt-out--99.8%
sub-neg99.8%
remove-double-neg99.8%
neg-sub099.8%
Simplified99.8%
Taylor expanded in re around 0 58.5%
Simplified58.5%
Final simplification85.2%
(FPCore (re im) :precision binary64 (if (or (<= im 950.0) (not (<= im 1.32e+154))) (* (sin re) (+ (* 0.5 (* im im)) 1.0)) (pow re -4.0)))
double code(double re, double im) {
double tmp;
if ((im <= 950.0) || !(im <= 1.32e+154)) {
tmp = sin(re) * ((0.5 * (im * im)) + 1.0);
} 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 <= 950.0d0) .or. (.not. (im <= 1.32d+154))) then
tmp = sin(re) * ((0.5d0 * (im * im)) + 1.0d0)
else
tmp = re ** (-4.0d0)
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if ((im <= 950.0) || !(im <= 1.32e+154)) {
tmp = Math.sin(re) * ((0.5 * (im * im)) + 1.0);
} else {
tmp = Math.pow(re, -4.0);
}
return tmp;
}
def code(re, im): tmp = 0 if (im <= 950.0) or not (im <= 1.32e+154): tmp = math.sin(re) * ((0.5 * (im * im)) + 1.0) else: tmp = math.pow(re, -4.0) return tmp
function code(re, im) tmp = 0.0 if ((im <= 950.0) || !(im <= 1.32e+154)) tmp = Float64(sin(re) * Float64(Float64(0.5 * Float64(im * im)) + 1.0)); else tmp = re ^ -4.0; end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if ((im <= 950.0) || ~((im <= 1.32e+154))) tmp = sin(re) * ((0.5 * (im * im)) + 1.0); else tmp = re ^ -4.0; end tmp_2 = tmp; end
code[re_, im_] := If[Or[LessEqual[im, 950.0], N[Not[LessEqual[im, 1.32e+154]], $MachinePrecision]], N[(N[Sin[re], $MachinePrecision] * N[(N[(0.5 * N[(im * im), $MachinePrecision]), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[Power[re, -4.0], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 950 \lor \neg \left(im \leq 1.32 \cdot 10^{+154}\right):\\
\;\;\;\;\sin re \cdot \left(0.5 \cdot \left(im \cdot im\right) + 1\right)\\
\mathbf{else}:\\
\;\;\;\;{re}^{-4}\\
\end{array}
\end{array}
if im < 950 or 1.31999999999999998e154 < 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 87.7%
Simplified87.7%
unpow287.7%
Applied egg-rr87.7%
if 950 < im < 1.31999999999999998e154Initial 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 57.7%
Simplified57.7%
Applied egg-rr24.3%
Final simplification81.3%
(FPCore (re im) :precision binary64 (if (<= im 780.0) (sin re) (if (<= im 9.2e+142) (pow re -4.0) (+ re (* 0.5 (* re (pow im 2.0)))))))
double code(double re, double im) {
double tmp;
if (im <= 780.0) {
tmp = sin(re);
} else if (im <= 9.2e+142) {
tmp = pow(re, -4.0);
} else {
tmp = re + (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 <= 780.0d0) then
tmp = sin(re)
else if (im <= 9.2d+142) then
tmp = re ** (-4.0d0)
else
tmp = re + (0.5d0 * (re * (im ** 2.0d0)))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (im <= 780.0) {
tmp = Math.sin(re);
} else if (im <= 9.2e+142) {
tmp = Math.pow(re, -4.0);
} else {
tmp = re + (0.5 * (re * Math.pow(im, 2.0)));
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 780.0: tmp = math.sin(re) elif im <= 9.2e+142: tmp = math.pow(re, -4.0) else: tmp = re + (0.5 * (re * math.pow(im, 2.0))) return tmp
function code(re, im) tmp = 0.0 if (im <= 780.0) tmp = sin(re); elseif (im <= 9.2e+142) tmp = re ^ -4.0; else tmp = Float64(re + Float64(0.5 * Float64(re * (im ^ 2.0)))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 780.0) tmp = sin(re); elseif (im <= 9.2e+142) tmp = re ^ -4.0; else tmp = re + (0.5 * (re * (im ^ 2.0))); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 780.0], N[Sin[re], $MachinePrecision], If[LessEqual[im, 9.2e+142], N[Power[re, -4.0], $MachinePrecision], N[(re + N[(0.5 * N[(re * N[Power[im, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 780:\\
\;\;\;\;\sin re\\
\mathbf{elif}\;im \leq 9.2 \cdot 10^{+142}:\\
\;\;\;\;{re}^{-4}\\
\mathbf{else}:\\
\;\;\;\;re + 0.5 \cdot \left(re \cdot {im}^{2}\right)\\
\end{array}
\end{array}
if im < 780Initial 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 70.6%
if 780 < im < 9.20000000000000009e142Initial 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 56.0%
Simplified56.0%
Applied egg-rr25.2%
if 9.20000000000000009e142 < 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 81.1%
Simplified81.1%
Taylor expanded in im around 0 81.1%
*-commutative81.1%
Simplified81.1%
Final simplification67.7%
(FPCore (re im) :precision binary64 (if (<= im 950.0) (sin re) (pow re -4.0)))
double code(double re, double im) {
double tmp;
if (im <= 950.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 <= 950.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 <= 950.0) {
tmp = Math.sin(re);
} else {
tmp = Math.pow(re, -4.0);
}
return tmp;
}
def code(re, im): tmp = 0 if im <= 950.0: tmp = math.sin(re) else: tmp = math.pow(re, -4.0) return tmp
function code(re, im) tmp = 0.0 if (im <= 950.0) tmp = sin(re); else tmp = re ^ -4.0; end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (im <= 950.0) tmp = sin(re); else tmp = re ^ -4.0; end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[im, 950.0], N[Sin[re], $MachinePrecision], N[Power[re, -4.0], $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;im \leq 950:\\
\;\;\;\;\sin re\\
\mathbf{else}:\\
\;\;\;\;{re}^{-4}\\
\end{array}
\end{array}
if im < 950Initial 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 70.6%
if 950 < 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 71.0%
Simplified71.0%
Applied egg-rr22.2%
(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 54.2%
(FPCore (re im) :precision binary64 (if (<= re 1.08e-10) re (/ re (+ re (- re re)))))
double code(double re, double im) {
double tmp;
if (re <= 1.08e-10) {
tmp = re;
} else {
tmp = re / (re + (re - re));
}
return tmp;
}
real(8) function code(re, im)
real(8), intent (in) :: re
real(8), intent (in) :: im
real(8) :: tmp
if (re <= 1.08d-10) then
tmp = re
else
tmp = re / (re + (re - re))
end if
code = tmp
end function
public static double code(double re, double im) {
double tmp;
if (re <= 1.08e-10) {
tmp = re;
} else {
tmp = re / (re + (re - re));
}
return tmp;
}
def code(re, im): tmp = 0 if re <= 1.08e-10: tmp = re else: tmp = re / (re + (re - re)) return tmp
function code(re, im) tmp = 0.0 if (re <= 1.08e-10) tmp = re; else tmp = Float64(re / Float64(re + Float64(re - re))); end return tmp end
function tmp_2 = code(re, im) tmp = 0.0; if (re <= 1.08e-10) tmp = re; else tmp = re / (re + (re - re)); end tmp_2 = tmp; end
code[re_, im_] := If[LessEqual[re, 1.08e-10], re, N[(re / N[(re + N[(re - re), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;re \leq 1.08 \cdot 10^{-10}:\\
\;\;\;\;re\\
\mathbf{else}:\\
\;\;\;\;\frac{re}{re + \left(re - re\right)}\\
\end{array}
\end{array}
if re < 1.08000000000000002e-10Initial 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 69.0%
Simplified69.0%
Taylor expanded in im around 0 33.5%
if 1.08000000000000002e-10 < re 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 23.3%
Simplified23.3%
Applied egg-rr8.1%
(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 57.2%
Simplified57.2%
Taylor expanded in im around 0 25.8%
herbie shell --seed 2024106
(FPCore (re im)
:name "math.sin on complex, real part"
:precision binary64
(* (* 0.5 (sin re)) (+ (exp (- 0.0 im)) (exp im))))