Python: Calculate sine/cosine with a precision of up to 1 million digits

前端 未结 3 1141
故里飘歌
故里飘歌 2020-12-19 14:24

Question is pretty self-explanatory. I\'ve seen a couple of examples for pi but not for trigo functions. Maybe one could use a Taylor series as done here but I\'m not entire

3条回答
  •  一生所求
    2020-12-19 14:41

    mpmath is the way:

    from mpmath import mp
    precision = 1000000
    mp.dps = precision
    mp.cos(0.1)
    

    If unable to install mpmath or any other module you could try polynomial approximation as suggested.

    where Rn is the Lagrange Remainder

    Note that Rn grows fast as soon as x moves away from the center x0, so be careful using Maclaurin series (Taylor series centered in 0) when trying to calculate sin(x) or cos(x) of arbitrary x.

提交回复
热议问题