
(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 20 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}
im_m = (fabs.f64 im) (FPCore (re im_m) :precision binary64 (fma (* 0.5 (sin re)) (exp (- im_m)) (* (sin re) (* 0.5 (exp im_m)))))
im_m = fabs(im);
double code(double re, double im_m) {
return fma((0.5 * sin(re)), exp(-im_m), (sin(re) * (0.5 * exp(im_m))));
}
im_m = abs(im) function code(re, im_m) return fma(Float64(0.5 * sin(re)), exp(Float64(-im_m)), Float64(sin(re) * Float64(0.5 * exp(im_m)))) end
im_m = N[Abs[im], $MachinePrecision] code[re_, im$95$m_] := N[(N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision] * N[Exp[(-im$95$m)], $MachinePrecision] + N[(N[Sin[re], $MachinePrecision] * N[(0.5 * N[Exp[im$95$m], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
im_m = \left|im\right|
\\
\mathsf{fma}\left(0.5 \cdot \sin re, e^{-im\_m}, \sin re \cdot \left(0.5 \cdot e^{im\_m}\right)\right)
\end{array}
Initial program 100.0%
lift-*.f64N/A
lift-+.f64N/A
distribute-lft-inN/A
*-commutativeN/A
lower-fma.f64N/A
lift--.f64N/A
sub0-negN/A
lower-neg.f64N/A
*-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
associate-*l*N/A
lower-*.f64N/A
lower-*.f64100.0
Applied rewrites100.0%
im_m = (fabs.f64 im)
(FPCore (re im_m)
:precision binary64
(let* ((t_0 (* 0.5 (sin re))) (t_1 (* t_0 (+ (exp im_m) (exp (- im_m))))))
(if (<= t_1 -5e+290)
(* (cosh im_m) (fma re (* (* re re) -0.16666666666666666) re))
(if (<= t_1 5e+22)
(* t_0 (fma im_m im_m 2.0))
(*
(* 0.5 re)
(fma
im_m
(fma
(fma (* im_m im_m) 0.002777777777777778 0.08333333333333333)
(* im_m (* im_m im_m))
im_m)
2.0))))))im_m = fabs(im);
double code(double re, double im_m) {
double t_0 = 0.5 * sin(re);
double t_1 = t_0 * (exp(im_m) + exp(-im_m));
double tmp;
if (t_1 <= -5e+290) {
tmp = cosh(im_m) * fma(re, ((re * re) * -0.16666666666666666), re);
} else if (t_1 <= 5e+22) {
tmp = t_0 * fma(im_m, im_m, 2.0);
} else {
tmp = (0.5 * re) * fma(im_m, fma(fma((im_m * im_m), 0.002777777777777778, 0.08333333333333333), (im_m * (im_m * im_m)), im_m), 2.0);
}
return tmp;
}
im_m = abs(im) function code(re, im_m) t_0 = Float64(0.5 * sin(re)) t_1 = Float64(t_0 * Float64(exp(im_m) + exp(Float64(-im_m)))) tmp = 0.0 if (t_1 <= -5e+290) tmp = Float64(cosh(im_m) * fma(re, Float64(Float64(re * re) * -0.16666666666666666), re)); elseif (t_1 <= 5e+22) tmp = Float64(t_0 * fma(im_m, im_m, 2.0)); else tmp = Float64(Float64(0.5 * re) * fma(im_m, fma(fma(Float64(im_m * im_m), 0.002777777777777778, 0.08333333333333333), Float64(im_m * Float64(im_m * im_m)), im_m), 2.0)); end return tmp end
im_m = N[Abs[im], $MachinePrecision]
code[re_, im$95$m_] := Block[{t$95$0 = N[(0.5 * N[Sin[re], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 * N[(N[Exp[im$95$m], $MachinePrecision] + N[Exp[(-im$95$m)], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$1, -5e+290], N[(N[Cosh[im$95$m], $MachinePrecision] * N[(re * N[(N[(re * re), $MachinePrecision] * -0.16666666666666666), $MachinePrecision] + re), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 5e+22], N[(t$95$0 * N[(im$95$m * im$95$m + 2.0), $MachinePrecision]), $MachinePrecision], N[(N[(0.5 * re), $MachinePrecision] * N[(im$95$m * N[(N[(N[(im$95$m * im$95$m), $MachinePrecision] * 0.002777777777777778 + 0.08333333333333333), $MachinePrecision] * N[(im$95$m * N[(im$95$m * im$95$m), $MachinePrecision]), $MachinePrecision] + im$95$m), $MachinePrecision] + 2.0), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
im_m = \left|im\right|
\\
\begin{array}{l}
t_0 := 0.5 \cdot \sin re\\
t_1 := t\_0 \cdot \left(e^{im\_m} + e^{-im\_m}\right)\\
\mathbf{if}\;t\_1 \leq -5 \cdot 10^{+290}:\\
\;\;\;\;\cosh im\_m \cdot \mathsf{fma}\left(re, \left(re \cdot re\right) \cdot -0.16666666666666666, re\right)\\
\mathbf{elif}\;t\_1 \leq 5 \cdot 10^{+22}:\\
\;\;\;\;t\_0 \cdot \mathsf{fma}\left(im\_m, im\_m, 2\right)\\
\mathbf{else}:\\
\;\;\;\;\left(0.5 \cdot re\right) \cdot \mathsf{fma}\left(im\_m, \mathsf{fma}\left(\mathsf{fma}\left(im\_m \cdot im\_m, 0.002777777777777778, 0.08333333333333333\right), im\_m \cdot \left(im\_m \cdot im\_m\right), im\_m\right), 2\right)\\
\end{array}
\end{array}
if (*.f64 (*.f64 #s(literal 1/2 binary64) (sin.f64 re)) (+.f64 (exp.f64 (-.f64 #s(literal 0 binary64) im)) (exp.f64 im))) < -4.9999999999999998e290Initial program 100.0%
lift-*.f64N/A
lift-+.f64N/A
distribute-lft-inN/A
*-commutativeN/A
lower-fma.f64N/A
lift--.f64N/A
sub0-negN/A
lower-neg.f64N/A
*-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
associate-*l*N/A
lower-*.f64N/A
lower-*.f64100.0
Applied rewrites100.0%
lift-fma.f64N/A
lift-exp.f64N/A
lift-neg.f64N/A
neg-sub0N/A
lift-*.f64N/A
lift-*.f64N/A
associate-*r*N/A
*-commutativeN/A
lift-*.f64N/A
distribute-lft-inN/A
lift-exp.f64N/A
lift-*.f64N/A
associate-*l*N/A
*-commutativeN/A
metadata-evalN/A
div-invN/A
associate-*r/N/A
Applied rewrites100.0%
Taylor expanded in re around 0
+-commutativeN/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.f64N/A
unpow2N/A
lower-*.f6474.6
Applied rewrites74.6%
if -4.9999999999999998e290 < (*.f64 (*.f64 #s(literal 1/2 binary64) (sin.f64 re)) (+.f64 (exp.f64 (-.f64 #s(literal 0 binary64) im)) (exp.f64 im))) < 4.9999999999999996e22Initial program 100.0%
Taylor expanded in im around 0
+-commutativeN/A
unpow2N/A
lower-fma.f6498.9
Applied rewrites98.9%
if 4.9999999999999996e22 < (*.f64 (*.f64 #s(literal 1/2 binary64) (sin.f64 re)) (+.f64 (exp.f64 (-.f64 #s(literal 0 binary64) im)) (exp.f64 im))) Initial program 99.9%
Taylor expanded in im around 0
+-commutativeN/A
unpow2N/A
associate-*l*N/A
lower-fma.f64N/A
Applied rewrites84.1%
Taylor expanded in re around 0
*-commutativeN/A
lower-*.f6464.9
Applied rewrites64.9%
Final simplification84.5%
herbie shell --seed 2024226
(FPCore (re im)
:name "math.sin on complex, real part"
:precision binary64
(* (* 0.5 (sin re)) (+ (exp (- 0.0 im)) (exp im))))