0

I have been writing code for a module I am making for my Discord Bot. I have been trying not to use any module as it is not helping in in importing stuff. So I thought I should write the code myself for both of them.

The problem here is that I don't really know how do we make them. I couldn't find them anywhere on the net as everywhere I only saw the use of math module which I don't want to use.

I don't know how do I work with them, so I want some help.

Thank You! :)

Bhavyadeep Yadav
  • 819
  • 8
  • 25
  • 1
    https://en.wikipedia.org/wiki/Taylor_series#Trigonometric_functions – rdas May 16 '21 at 11:54
  • 2
    What do you mean by *"I have been trying not to use any module as it is not helping in in importing stuff"*? – Reti43 May 16 '21 at 12:01
  • @Reti43 I import the module file to the main file of my Bot which I run on Heroku. Now this doesn't work out. – Bhavyadeep Yadav May 16 '21 at 12:05
  • 1
    While I don't know Heroku, if it runs python, it should at least support all the native libraries, of which `math` is one. – Reti43 May 16 '21 at 12:11
  • @BhavyadeepYadav I don't think that has made it any clearer. Do you have some code example which shows why you are having trouble using `math.sin`, for example? – L.Grozinger May 16 '21 at 12:11
  • @L.Grozinger There not a problem in module. But let me tell you. Main file: `a.py` and module file: `b.py`. I have to import `math` in `b.py` to use it but when I stage it to Heroku it would show an error. – Bhavyadeep Yadav May 16 '21 at 12:13
  • In that case, perhaps you could edit your question to include those extra debugging details, and also add the `heroku` tag? – L.Grozinger May 16 '21 at 12:17
  • If I were you, I would ask about **that** issue (in a different question) and see if that can be resolved, before I decide to roll my own algorithms. Also, are you possibly facing [this](https://stackoverflow.com/questions/46250019/python-flask-heroku-cannot-import-module/46251306) issue? – Reti43 May 16 '21 at 12:17
  • 3
    It's rather complicated... check here: [How does C compute sin() and other math functions?](https://stackoverflow.com/questions/2284860/how-does-c-compute-sin-and-other-math-functions) – Gligi May 16 '21 at 12:19
  • @L.Grozinger Yeah. I'l do it in just a while – Bhavyadeep Yadav May 17 '21 at 01:53
  • @Retu43 Let's forget Inhave any issue. I want to know about their actual wrokng in python. – Bhavyadeep Yadav May 17 '21 at 01:55

2 Answers2

6

Using Taylor expansion you get an approximation up to the desired precision.

http://hyperphysics.phy-astr.gsu.edu/hbase/tayser.html

def pow(base, exponent):
    return base ** exponent

def faktorial(n):
    value = float(1)
    for i in range(1, n+1):
        value = value * i
    return value

def cos(x):
    x = x * 3.14/180
    value = 1
    sign = -1
    n = 200 # precision
    i = 2
    while i < n:
        value = value + (pow(x, i)/faktorial(i) * sign)
        i = i + 2
        sign = sign * -1
    
    return value

def sin(x):
    x = x * 3.14/180
    value = x
    sign = -1
    n = 200 # precision
    i = 3
    while i < n:
        value = value + (pow(x, i)/faktorial(i) * sign)
        i = i + 2
        sign = sign * -1
    return value
Nemanja02
  • 81
  • 7
1
pi = 3.1415926535897932384626433832795028841971  # Value of constant pi

def f(n):  # Factorial Function
    if n == 1 or n == 0:
        return 1
    else:
        return n * f(n - 1)

def deg(x):
    rad = x * pi/180
    return rad

def sin(x):  # Taylor Expansion of sinx
    k = 0
    sinx = 0
    while x >= pi:
        x -= pi
    if pi > x > pi / 2:
        x = pi - x
    while k < 15:
        sinx += (-1)**k * x**(2*k + 1) / f(2*k + 1)
        k += 1
    return sinx

def cos(x):
    cosx = sin(pi / 2 - x)
    return cosx

I improved the code now. Now it gives you accurate results of up to 14 decimal places. Also instead of writing full Taylor expression formula, I used a while loop to do that. While loop here acts as a summation function of maths. I also shorten the code inside cos(x). Instead of writing Taylor's expression here, I used a conversion formula of sinx to cosx. Which reduces the calculation process. I made a little change in the code. Now you can calculate sinx of huge number too with the same accuracy.

Rishabh Semwal
  • 328
  • 1
  • 3
  • 12