
(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 18 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 (fma (* (sin re) (exp (- im))) 0.5 (* (sin re) (* 0.5 (exp im)))))
double code(double re, double im) {
return fma((sin(re) * exp(-im)), 0.5, (sin(re) * (0.5 * exp(im))));
}
function code(re, im) return fma(Float64(sin(re) * exp(Float64(-im))), 0.5, Float64(sin(re) * Float64(0.5 * exp(im)))) end
code[re_, im_] := N[(N[(N[Sin[re], $MachinePrecision] * N[Exp[(-im)], $MachinePrecision]), $MachinePrecision] * 0.5 + N[(N[Sin[re], $MachinePrecision] * N[(0.5 * N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\sin re \cdot e^{-im}, 0.5, \sin re \cdot \left(0.5 \cdot e^{im}\right)\right)
\end{array}
Initial program 100.0%
lift-*.f64N/A
lift-+.f64N/A
distribute-rgt-inN/A
lift-*.f64N/A
*-commutativeN/A
associate-*r*N/A
lower-fma.f64N/A
*-commutativeN/A
lower-*.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%
(FPCore (re im)
:precision binary64
(let* ((t_0 (* (* (sin re) 0.5) (+ (exp (- im)) (exp im)))))
(if (<= t_0 (- INFINITY))
(*
(fma (* re re) (* re -0.16666666666666666) re)
(fma
(* im im)
(fma im (* im (* (* im im) 0.001388888888888889)) 0.5)
1.0))
(if (<= t_0 5.0)
(*
(sin re)
(fma (* im im) (fma im (* im 0.041666666666666664) 0.5) 1.0))
(*
(* re 0.5)
(fma
(* im im)
(fma
(* im im)
(fma (* im im) 0.002777777777777778 0.08333333333333333)
1.0)
2.0))))))
double code(double re, double im) {
double t_0 = (sin(re) * 0.5) * (exp(-im) + exp(im));
double tmp;
if (t_0 <= -((double) INFINITY)) {
tmp = fma((re * re), (re * -0.16666666666666666), re) * fma((im * im), fma(im, (im * ((im * im) * 0.001388888888888889)), 0.5), 1.0);
} else if (t_0 <= 5.0) {
tmp = sin(re) * fma((im * im), fma(im, (im * 0.041666666666666664), 0.5), 1.0);
} else {
tmp = (re * 0.5) * fma((im * im), fma((im * im), fma((im * im), 0.002777777777777778, 0.08333333333333333), 1.0), 2.0);
}
return tmp;
}
function code(re, im) t_0 = Float64(Float64(sin(re) * 0.5) * Float64(exp(Float64(-im)) + exp(im))) tmp = 0.0 if (t_0 <= Float64(-Inf)) tmp = Float64(fma(Float64(re * re), Float64(re * -0.16666666666666666), re) * fma(Float64(im * im), fma(im, Float64(im * Float64(Float64(im * im) * 0.001388888888888889)), 0.5), 1.0)); elseif (t_0 <= 5.0) tmp = Float64(sin(re) * fma(Float64(im * im), fma(im, Float64(im * 0.041666666666666664), 0.5), 1.0)); else tmp = Float64(Float64(re * 0.5) * fma(Float64(im * im), fma(Float64(im * im), fma(Float64(im * im), 0.002777777777777778, 0.08333333333333333), 1.0), 2.0)); end return tmp end
code[re_, im_] := Block[{t$95$0 = N[(N[(N[Sin[re], $MachinePrecision] * 0.5), $MachinePrecision] * N[(N[Exp[(-im)], $MachinePrecision] + N[Exp[im], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, (-Infinity)], N[(N[(N[(re * re), $MachinePrecision] * N[(re * -0.16666666666666666), $MachinePrecision] + re), $MachinePrecision] * N[(N[(im * im), $MachinePrecision] * N[(im * N[(im * N[(N[(im * im), $MachinePrecision] * 0.001388888888888889), $MachinePrecision]), $MachinePrecision] + 0.5), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 5.0], N[(N[Sin[re], $MachinePrecision] * N[(N[(im * im), $MachinePrecision] * N[(im * N[(im * 0.041666666666666664), $MachinePrecision] + 0.5), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision], N[(N[(re * 0.5), $MachinePrecision] * N[(N[(im * im), $MachinePrecision] * N[(N[(im * im), $MachinePrecision] * N[(N[(im * im), $MachinePrecision] * 0.002777777777777778 + 0.08333333333333333), $MachinePrecision] + 1.0), $MachinePrecision] + 2.0), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\sin re \cdot 0.5\right) \cdot \left(e^{-im} + e^{im}\right)\\
\mathbf{if}\;t\_0 \leq -\infty:\\
\;\;\;\;\mathsf{fma}\left(re \cdot re, re \cdot -0.16666666666666666, re\right) \cdot \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im, im \cdot \left(\left(im \cdot im\right) \cdot 0.001388888888888889\right), 0.5\right), 1\right)\\
\mathbf{elif}\;t\_0 \leq 5:\\
\;\;\;\;\sin re \cdot \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im, im \cdot 0.041666666666666664, 0.5\right), 1\right)\\
\mathbf{else}:\\
\;\;\;\;\left(re \cdot 0.5\right) \cdot \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im \cdot im, \mathsf{fma}\left(im \cdot im, 0.002777777777777778, 0.08333333333333333\right), 1\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))) < -inf.0Initial program 100.0%
lift-*.f64N/A
*-commutativeN/A
lift-*.f64N/A
associate-*r*N/A
lower-*.f64N/A
*-commutativeN/A
lift-+.f64N/A
+-commutativeN/A
lift-exp.f64N/A
lift-exp.f64N/A
lift--.f64N/A
sub0-negN/A
cosh-undefN/A
associate-*r*N/A
metadata-evalN/A
exp-0N/A
lower-*.f64N/A
exp-0N/A
lower-cosh.f64100.0
Applied rewrites100.0%
Taylor expanded in im around 0
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
+-commutativeN/A
unpow2N/A
associate-*l*N/A
lower-fma.f64N/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6483.5
Applied rewrites83.5%
Taylor expanded in re around 0
+-commutativeN/A
distribute-rgt-inN/A
*-commutativeN/A
associate-*l*N/A
*-lft-identityN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
*-commutativeN/A
lower-*.f6464.9
Applied rewrites64.9%
Taylor expanded in im around inf
Applied rewrites64.9%
if -inf.0 < (*.f64 (*.f64 #s(literal 1/2 binary64) (sin.f64 re)) (+.f64 (exp.f64 (-.f64 #s(literal 0 binary64) im)) (exp.f64 im))) < 5Initial program 100.0%
Taylor expanded in im around 0
+-commutativeN/A
distribute-rgt-inN/A
associate-+l+N/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
associate-*l*N/A
*-commutativeN/A
associate-*r*N/A
*-commutativeN/A
associate-*r*N/A
distribute-lft1-inN/A
Applied rewrites99.1%
if 5 < (*.f64 (*.f64 #s(literal 1/2 binary64) (sin.f64 re)) (+.f64 (exp.f64 (-.f64 #s(literal 0 binary64) im)) (exp.f64 im))) Initial program 100.0%
Taylor expanded in im around 0
+-commutativeN/A
unpow2N/A
associate-*l*N/A
lower-fma.f64N/A
Applied rewrites85.3%
Taylor expanded in im around 0
+-commutativeN/A
unpow2N/A
lower-fma.f6454.6
Applied rewrites54.6%
Taylor expanded in re around 0
lower-*.f6446.6
Applied rewrites46.6%
Taylor expanded in im around 0
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6465.7
Applied rewrites65.7%
Final simplification82.5%
herbie shell --seed 2024229
(FPCore (re im)
:name "math.sin on complex, real part"
:precision binary64
(* (* 0.5 (sin re)) (+ (exp (- 0.0 im)) (exp im))))