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

前端 未结 3 1140
故里飘歌
故里飘歌 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:37

    import math
    x = .5
    def sin(x):
        sum = 0
        for a in range(0,50): #this number (50) to be changed for more accurate results
            sum+=(math.pow(-1,a))/(math.factorial(2*a+1))*(math.pow(x,2*a+1))
        return sum
    
    ans = sin(x)
    print(str.format('{0:.15f}', ans)) #change the 15 for more decimal places
    

    Here is an example of implementing the Taylor series using python as you suggested above. Changing to cos wouldn't be too hard after that.

    EDIT:

    Added in the formatting of the last line in order to actual print out more decimal places.

提交回复
热议问题