math.sin on complex, real part

Percentage Accurate: 100.0% → 100.0%
Time: 6.7s
Alternatives: 9
Speedup: 1.0×

Specification

?
\[\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 (- 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:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 9 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 100.0% accurate, 1.0× speedup?

\[\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 (- 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}

Alternative 1: 100.0% accurate, 0.8× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \sin re \cdot \mathsf{fma}\left(0.5, e^{im_m}, \frac{0.5}{e^{im_m}}\right) \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (* (sin re) (fma 0.5 (exp im_m) (/ 0.5 (exp im_m)))))
im_m = fabs(im);
double code(double re, double im_m) {
	return sin(re) * fma(0.5, exp(im_m), (0.5 / exp(im_m)));
}
im_m = abs(im)
function code(re, im_m)
	return Float64(sin(re) * fma(0.5, exp(im_m), Float64(0.5 / exp(im_m))))
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := N[(N[Sin[re], $MachinePrecision] * N[(0.5 * N[Exp[im$95$m], $MachinePrecision] + N[(0.5 / N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|

\\
\sin re \cdot \mathsf{fma}\left(0.5, e^{im_m}, \frac{0.5}{e^{im_m}}\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 2: 100.0% accurate, 1.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \left(\sin re \cdot 0.5\right) \cdot \left(e^{im_m} + e^{-im_m}\right) \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (* (* (sin re) 0.5) (+ (exp im_m) (exp (- im_m)))))
im_m = fabs(im);
double code(double re, double im_m) {
	return (sin(re) * 0.5) * (exp(im_m) + exp(-im_m));
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = (sin(re) * 0.5d0) * (exp(im_m) + exp(-im_m))
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return (Math.sin(re) * 0.5) * (Math.exp(im_m) + Math.exp(-im_m));
}
im_m = math.fabs(im)
def code(re, im_m):
	return (math.sin(re) * 0.5) * (math.exp(im_m) + math.exp(-im_m))
im_m = abs(im)
function code(re, im_m)
	return Float64(Float64(sin(re) * 0.5) * Float64(exp(im_m) + exp(Float64(-im_m))))
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = (sin(re) * 0.5) * (exp(im_m) + exp(-im_m));
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := N[(N[(N[Sin[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(N[Exp[im$95$m], $MachinePrecision] + N[Exp[(-im$95$m)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|

\\
\left(\sin re \cdot 0.5\right) \cdot \left(e^{im_m} + e^{-im_m}\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 3: 74.2% accurate, 1.5× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im_m \leq 0.000135:\\ \;\;\;\;\sin re\\ \mathbf{elif}\;im_m \leq 2.4 \cdot 10^{+51}:\\ \;\;\;\;re \cdot \cosh im_m\\ \mathbf{else}:\\ \;\;\;\;0.001388888888888889 \cdot \left(\sin re \cdot {im_m}^{6}\right)\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= im_m 0.000135)
   (sin re)
   (if (<= im_m 2.4e+51)
     (* re (cosh im_m))
     (* 0.001388888888888889 (* (sin re) (pow im_m 6.0))))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (im_m <= 0.000135) {
		tmp = sin(re);
	} else if (im_m <= 2.4e+51) {
		tmp = re * cosh(im_m);
	} else {
		tmp = 0.001388888888888889 * (sin(re) * pow(im_m, 6.0));
	}
	return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    real(8) :: tmp
    if (im_m <= 0.000135d0) then
        tmp = sin(re)
    else if (im_m <= 2.4d+51) then
        tmp = re * cosh(im_m)
    else
        tmp = 0.001388888888888889d0 * (sin(re) * (im_m ** 6.0d0))
    end if
    code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if (im_m <= 0.000135) {
		tmp = Math.sin(re);
	} else if (im_m <= 2.4e+51) {
		tmp = re * Math.cosh(im_m);
	} else {
		tmp = 0.001388888888888889 * (Math.sin(re) * Math.pow(im_m, 6.0));
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if im_m <= 0.000135:
		tmp = math.sin(re)
	elif im_m <= 2.4e+51:
		tmp = re * math.cosh(im_m)
	else:
		tmp = 0.001388888888888889 * (math.sin(re) * math.pow(im_m, 6.0))
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (im_m <= 0.000135)
		tmp = sin(re);
	elseif (im_m <= 2.4e+51)
		tmp = Float64(re * cosh(im_m));
	else
		tmp = Float64(0.001388888888888889 * Float64(sin(re) * (im_m ^ 6.0)));
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (im_m <= 0.000135)
		tmp = sin(re);
	elseif (im_m <= 2.4e+51)
		tmp = re * cosh(im_m);
	else
		tmp = 0.001388888888888889 * (sin(re) * (im_m ^ 6.0));
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[im$95$m, 0.000135], N[Sin[re], $MachinePrecision], If[LessEqual[im$95$m, 2.4e+51], N[(re * N[Cosh[im$95$m], $MachinePrecision]), $MachinePrecision], N[(0.001388888888888889 * N[(N[Sin[re], $MachinePrecision] * N[Power[im$95$m, 6.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;im_m \leq 0.000135:\\
\;\;\;\;\sin re\\

\mathbf{elif}\;im_m \leq 2.4 \cdot 10^{+51}:\\
\;\;\;\;re \cdot \cosh im_m\\

\mathbf{else}:\\
\;\;\;\;0.001388888888888889 \cdot \left(\sin re \cdot {im_m}^{6}\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 4: 98.9% accurate, 1.5× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \sin re \cdot \left(0.5 + 0.5 \cdot e^{im_m}\right) \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 (* (sin re) (+ 0.5 (* 0.5 (exp im_m)))))
im_m = fabs(im);
double code(double re, double im_m) {
	return sin(re) * (0.5 + (0.5 * exp(im_m)));
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = sin(re) * (0.5d0 + (0.5d0 * exp(im_m)))
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return Math.sin(re) * (0.5 + (0.5 * Math.exp(im_m)));
}
im_m = math.fabs(im)
def code(re, im_m):
	return math.sin(re) * (0.5 + (0.5 * math.exp(im_m)))
im_m = abs(im)
function code(re, im_m)
	return Float64(sin(re) * Float64(0.5 + Float64(0.5 * exp(im_m))))
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = sin(re) * (0.5 + (0.5 * exp(im_m)));
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := N[(N[Sin[re], $MachinePrecision] * N[(0.5 + N[(0.5 * N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|

\\
\sin re \cdot \left(0.5 + 0.5 \cdot e^{im_m}\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 5: 68.6% accurate, 2.9× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im_m \leq 0.000165:\\ \;\;\;\;\sin re\\ \mathbf{else}:\\ \;\;\;\;re \cdot \cosh im_m\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= im_m 0.000165) (sin re) (* re (cosh im_m))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (im_m <= 0.000165) {
		tmp = sin(re);
	} else {
		tmp = re * cosh(im_m);
	}
	return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    real(8) :: tmp
    if (im_m <= 0.000165d0) then
        tmp = sin(re)
    else
        tmp = re * cosh(im_m)
    end if
    code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if (im_m <= 0.000165) {
		tmp = Math.sin(re);
	} else {
		tmp = re * Math.cosh(im_m);
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if im_m <= 0.000165:
		tmp = math.sin(re)
	else:
		tmp = re * math.cosh(im_m)
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (im_m <= 0.000165)
		tmp = sin(re);
	else
		tmp = Float64(re * cosh(im_m));
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (im_m <= 0.000165)
		tmp = sin(re);
	else
		tmp = re * cosh(im_m);
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[im$95$m, 0.000165], N[Sin[re], $MachinePrecision], N[(re * N[Cosh[im$95$m], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;im_m \leq 0.000165:\\
\;\;\;\;\sin re\\

\mathbf{else}:\\
\;\;\;\;re \cdot \cosh im_m\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 6: 53.9% accurate, 3.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;im_m \leq 1.1 \cdot 10^{+46}:\\ \;\;\;\;\sin re\\ \mathbf{else}:\\ \;\;\;\;re \cdot \left(0.5 \cdot im_m\right)\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= im_m 1.1e+46) (sin re) (* re (* 0.5 im_m))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (im_m <= 1.1e+46) {
		tmp = sin(re);
	} else {
		tmp = re * (0.5 * im_m);
	}
	return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    real(8) :: tmp
    if (im_m <= 1.1d+46) then
        tmp = sin(re)
    else
        tmp = re * (0.5d0 * im_m)
    end if
    code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if (im_m <= 1.1e+46) {
		tmp = Math.sin(re);
	} else {
		tmp = re * (0.5 * im_m);
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if im_m <= 1.1e+46:
		tmp = math.sin(re)
	else:
		tmp = re * (0.5 * im_m)
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (im_m <= 1.1e+46)
		tmp = sin(re);
	else
		tmp = Float64(re * Float64(0.5 * im_m));
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (im_m <= 1.1e+46)
		tmp = sin(re);
	else
		tmp = re * (0.5 * im_m);
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[im$95$m, 1.1e+46], N[Sin[re], $MachinePrecision], N[(re * N[(0.5 * im$95$m), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;im_m \leq 1.1 \cdot 10^{+46}:\\
\;\;\;\;\sin re\\

\mathbf{else}:\\
\;\;\;\;re \cdot \left(0.5 \cdot im_m\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 7: 30.3% accurate, 43.8× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ \begin{array}{l} \mathbf{if}\;re \leq 8.8 \cdot 10^{+23}:\\ \;\;\;\;re\\ \mathbf{else}:\\ \;\;\;\;re \cdot \left(0.5 \cdot im_m\right)\\ \end{array} \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m)
 :precision binary64
 (if (<= re 8.8e+23) re (* re (* 0.5 im_m))))
im_m = fabs(im);
double code(double re, double im_m) {
	double tmp;
	if (re <= 8.8e+23) {
		tmp = re;
	} else {
		tmp = re * (0.5 * im_m);
	}
	return tmp;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    real(8) :: tmp
    if (re <= 8.8d+23) then
        tmp = re
    else
        tmp = re * (0.5d0 * im_m)
    end if
    code = tmp
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	double tmp;
	if (re <= 8.8e+23) {
		tmp = re;
	} else {
		tmp = re * (0.5 * im_m);
	}
	return tmp;
}
im_m = math.fabs(im)
def code(re, im_m):
	tmp = 0
	if re <= 8.8e+23:
		tmp = re
	else:
		tmp = re * (0.5 * im_m)
	return tmp
im_m = abs(im)
function code(re, im_m)
	tmp = 0.0
	if (re <= 8.8e+23)
		tmp = re;
	else
		tmp = Float64(re * Float64(0.5 * im_m));
	end
	return tmp
end
im_m = abs(im);
function tmp_2 = code(re, im_m)
	tmp = 0.0;
	if (re <= 8.8e+23)
		tmp = re;
	else
		tmp = re * (0.5 * im_m);
	end
	tmp_2 = tmp;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := If[LessEqual[re, 8.8e+23], re, N[(re * N[(0.5 * im$95$m), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
im_m = \left|im\right|

\\
\begin{array}{l}
\mathbf{if}\;re \leq 8.8 \cdot 10^{+23}:\\
\;\;\;\;re\\

\mathbf{else}:\\
\;\;\;\;re \cdot \left(0.5 \cdot im_m\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 8: 32.9% accurate, 44.1× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ re \cdot \left(0.5 \cdot im_m + 1\right) \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 (* re (+ (* 0.5 im_m) 1.0)))
im_m = fabs(im);
double code(double re, double im_m) {
	return re * ((0.5 * im_m) + 1.0);
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = re * ((0.5d0 * im_m) + 1.0d0)
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return re * ((0.5 * im_m) + 1.0);
}
im_m = math.fabs(im)
def code(re, im_m):
	return re * ((0.5 * im_m) + 1.0)
im_m = abs(im)
function code(re, im_m)
	return Float64(re * Float64(Float64(0.5 * im_m) + 1.0))
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = re * ((0.5 * im_m) + 1.0);
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := N[(re * N[(N[(0.5 * im$95$m), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|

\\
re \cdot \left(0.5 \cdot im_m + 1\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 9: 27.0% accurate, 309.0× speedup?

\[\begin{array}{l} im_m = \left|im\right| \\ re \end{array} \]
im_m = (fabs.f64 im)
(FPCore (re im_m) :precision binary64 re)
im_m = fabs(im);
double code(double re, double im_m) {
	return re;
}
im_m = abs(im)
real(8) function code(re, im_m)
    real(8), intent (in) :: re
    real(8), intent (in) :: im_m
    code = re
end function
im_m = Math.abs(im);
public static double code(double re, double im_m) {
	return re;
}
im_m = math.fabs(im)
def code(re, im_m):
	return re
im_m = abs(im)
function code(re, im_m)
	return re
end
im_m = abs(im);
function tmp = code(re, im_m)
	tmp = re;
end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := re
\begin{array}{l}
im_m = \left|im\right|

\\
re
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Reproduce

?
herbie shell --seed 2023342 
(FPCore (re im)
  :name "math.sin on complex, real part"
  :precision binary64
  (* (* 0.5 (sin re)) (+ (exp (- 0.0 im)) (exp im))))