The Wayback Machine - https://web.archive.org/web/20200520123649/https://github.com/TheAlgorithms/Python/pull/2000/files
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some trigonometric algorithm #2000

Open
wants to merge 5 commits into
base: master
from
Open
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -0,0 +1,135 @@

# package maths.py
# implement some math.py
# trigonometric algorithm

Pi = 3.14159265358979323846


def _abs(value):
# >>> _abs(-10)
# 10
# >>> _abs(10)
# 10
Comment on lines +10 to +13

This comment has been minimized.

Copy link
@cclauss

cclauss May 20, 2020

Member
Suggested change
# >>> _abs(-10)
# 10
# >>> _abs(10)
# 10
"""
>>> _abs(-10)
10
>>> _abs(-.5)
0.5
>>> _abs(0)
0
>>> _abs(.5)
0.5
>>> _abs(10)
10
"""

Doctests must be in docstrings, not in comments. Please do python3 -m docstring -v maths.py on your computer and make sure that all tests run and pass.

return -value if value < 0 else value


def _power(value, n):

This comment has been minimized.

Copy link
@cclauss

cclauss May 20, 2020

Member
Suggested change
def _power(value, n):
def _power(value, n):
"""
>>> from math import pow
>>> for i in range 360:
>>> assert _power(10, i) == pow(10, i), (i, _power(10, i), pow(10, i))
"""
# >>> _power(-10, 2)
# 100
# >>> _power(10, 2)
# 100
result = 1

if n == 0:
result = 1
else:
for i in range(n):
result = result * value

return result


# newton iteration method
def _sqrt(value):

This comment has been minimized.

Copy link
@cclauss

cclauss May 20, 2020

Member
Suggested change
def _sqrt(value):
def _sqrt(value):
"""
>>> _sqrt(9)
3
>>> _sqrt(25)
5
"""

The docstring must appear at the top of the function before any code.

if value < 0:
print("_sqrt: Value must be greater than or equal to 0")
else:
x = value
t = 0
# X(k+1) = 1/2 * (X(k) + vale/X(k))
while _abs(x - t) > 1e-15:
t = x
x = 0.5 * (x + (value / x))

# >>> _sqrt(9)
# 3
# >>> _sqrt(25)
# 5
return x


# factorial
def _factor(value):
# regulations 0! = 1

This comment has been minimized.

Copy link
@cclauss

cclauss May 20, 2020

Member
Suggested change
# regulations 0! = 1
"""
regulations 0! = 1
>>> from math import factorial
>>> for i in range 360:
>>> assert _factor(i) == factorial(i), (i, _factor(i), factorial(i))
"""
f = 1
if value:
for i in range(value):
f = f * (i + 1)
else:
f = 1

# >>> _factor(5)
# 120
# >>> _factor(2)
# 2
return f


# Another way to
# implement sin(x)
def __sin(value):
value = value * Pi / 180
t = value
x = 0
n = 1

while _abs(t) > 1e-15:
x = x + t
n = n + 1
t = -t * value * value / (2 * n - 1) / (2 * n - 2)

if x > 0 and x < 1e-15:
x = 0

# >>> _sin(90)
# 1
# >>> _sin(0)
# 0
return x


def _sin(value):
value = value * Pi / 180
t = 1
x = 0
n = 1

while _power(value, n) / _factor(n) > 1e-15:
x += t * _power(value, n) / _factor(n)
t = -1 * t
n = n + 2

# >>> _sin(90)
# 1
# >>> _sin(0)
# 0
return x


def _cos(value):
value = value * Pi / 180
n = 0
t = 1
x = 0

while _power(value, n) / _factor(n) > 1e-15:
x += t * _power(value, n) / _factor(n)
t = -1 * t
n = n + 2

# >>> _cos(90)
# 0
# >>> _cos(0)
# 1
return x


def _tan(value):
x = _sin(value) / _cos(value)

# >>> _tan(90)
# 0
# >>> _tan(45)
# 1
return x
Comment on lines +128 to +135

This comment has been minimized.

Copy link
@cclauss

cclauss May 20, 2020

Member
Suggested change
def _tan(value):
x = _sin(value) / _cos(value)
# >>> _tan(90)
# 0
# >>> _tan(45)
# 1
return x
def _tan(value):
"""
>>> _tan(90)
0
>>> _tan(45)
1
>>> from math import tan
>>> for i in range 360:
>>> assert _tan(i) == tan(i), (i, _tan(i), tan(i))
"""
return _sin(value) / _cos(value)
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.
HTTPS · web.archive.org
← Home