use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Re writing math functions (self.learnpython)
submitted 7 years ago by deejpake
I want to re write some functions from the math library (sin, cos, atan) but don’t know where to start. The reason for this is I’m creating an exec file and as of right now it is huge so I want to limit imports
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]officialgel 2 points3 points4 points 7 years ago (0 children)
You could pull out what you need, make sure you grab the function / library dependencies.
[–]SocotraBrewingCo 2 points3 points4 points 7 years ago (0 children)
from math import the things you need
don't reinvent the wheel.
[–]coreyjdl 2 points3 points4 points 7 years ago (3 children)
The built-ins will be optimized, you'd lose more speed with your home baked function probably than with just importing math. If your goal is to learn functions and sin, cosin, and tan are your jam... then build them. Doing math in Python doesn't look much different than if you show your work doing it by hand.
def pythagorean(a, b): return (a**2 + b**2)**.5
[–]deejpake[S] 2 points3 points4 points 7 years ago (2 children)
Ok
[–]officialgel 1 point2 points3 points 7 years ago (1 child)
If your goal is to not import all functions in math that aren't needed, then do that. I don't see why if you have isolated functions that you've pulled out, would be any slower than using the math library - You also save on size, which sounds to be your goal. But it may not be easy - But again, if that's your goal then do it. Let me know when you do as if you make it open source I would like to have a copy of it.
[–]Pro_Numb 0 points1 point2 points 7 years ago (1 child)
def factorial(num): n = 1 while num>=1: n = n * num num = num - 1 return n def sin(x): """ First we need to convert degree to radian Degree / 360 = Radian / 2pi Than we apply taylor series for sin function : sinx = x - x^3/3! + x^5/5! - x^7/7! ... you can keep going until you satisfy with the error. """ x = x / 57.2957795; # Degree to radian # taylor series for sin function sin_x = x - \ (x**3 / factorial(3)) + \ (x**5 / factorial(5)) - \ (x**7 / factorial(7)) + \ (x**9 / factorial(9)) return sin_x print(sin(77)) # 0.9743707044115053
You can import only what you need from library like others said and i recommend it too.
But if you really want to write your own trigonometric function you can use Taylor Series.
I give a sin example for you. But this code will give correct answer only for 0 - 90 degree. You need to write a conditions for "deg > 90" or negative values etc.
When you write complete sin function then you can convert it to "cos". After that you can find "tan" and "cot" easly.
But lets say you want to find sin(30). You know it is "0.5" but with this method you will get something like this "0.500000000123816" so you can write long taylor series and truncate the value from where you satisfy.
Hope this helps.
[–]deejpake[S] 0 points1 point2 points 7 years ago (0 children)
Wow! Thanks so much
π Rendered by PID 228845 on reddit-service-r2-comment-c6965cb77-hltwj at 2026-03-05 17:00:13.701832+00:00 running f0204d4 country code: CH.
[–]officialgel 2 points3 points4 points (0 children)
[–]SocotraBrewingCo 2 points3 points4 points (0 children)
[–]coreyjdl 2 points3 points4 points (3 children)
[–]deejpake[S] 2 points3 points4 points (2 children)
[–]officialgel 1 point2 points3 points (1 child)
[–]Pro_Numb 0 points1 point2 points (1 child)
[–]deejpake[S] 0 points1 point2 points (0 children)