run black, assuage pylint
This commit is contained in:
parent
483088a9fc
commit
9f160966c7
4 changed files with 733 additions and 647 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -11,6 +11,8 @@
|
||||||
#
|
#
|
||||||
# The algorithms use range reductions and taylor polynomaials
|
# The algorithms use range reductions and taylor polynomaials
|
||||||
|
|
||||||
|
# pylint: disable=invalid-name
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Trig functions using jepler_udecimal
|
Trig functions using jepler_udecimal
|
||||||
|
|
||||||
|
|
@ -46,9 +48,10 @@ Decimal('0.5646424733950353572009454457')
|
||||||
|
|
||||||
from . import Decimal, localcontext
|
from . import Decimal, localcontext
|
||||||
|
|
||||||
__all__ = ['atan', 'sin', 'cos']
|
__all__ = ["atan", "sin", "cos"]
|
||||||
|
|
||||||
|
_point2 = Decimal(".2")
|
||||||
|
|
||||||
_point2 = Decimal('.2')
|
|
||||||
|
|
||||||
def atan(x, context=None):
|
def atan(x, context=None):
|
||||||
"""Compute the arctangent of the specified value, in radians"""
|
"""Compute the arctangent of the specified value, in radians"""
|
||||||
|
|
@ -63,19 +66,29 @@ def atan(x, context=None):
|
||||||
n = -1
|
n = -1
|
||||||
x = -x
|
x = -x
|
||||||
|
|
||||||
# Hard code values for inputs +-1 and +-.2
|
# Hard code values for inputs +-1 and +-.2
|
||||||
if scale < 65:
|
if scale < 65:
|
||||||
if x == 1:
|
if x == 1:
|
||||||
return Decimal('.7853981633974483096156608458198757210492923498437764552437361480') / n
|
return (
|
||||||
|
Decimal(
|
||||||
|
".7853981633974483096156608458198757210492923498437764552437361480"
|
||||||
|
)
|
||||||
|
/ n
|
||||||
|
)
|
||||||
if x == _point2:
|
if x == _point2:
|
||||||
return Decimal('.1973955598498807583700497651947902934475851037878521015176889402') / n
|
return (
|
||||||
|
Decimal(
|
||||||
|
".1973955598498807583700497651947902934475851037878521015176889402"
|
||||||
|
)
|
||||||
|
/ n
|
||||||
|
)
|
||||||
|
|
||||||
if x > _point2:
|
if x > _point2:
|
||||||
ctx.prec += 5
|
ctx.prec += 5
|
||||||
a=atan(_point2)
|
a = atan(_point2)
|
||||||
else:
|
else:
|
||||||
a=0
|
a = 0
|
||||||
|
|
||||||
ctx.prec = scale + 3
|
ctx.prec = scale + 3
|
||||||
|
|
||||||
# This very efficient range reduction reduces 1e300 to under .2 in
|
# This very efficient range reduction reduces 1e300 to under .2 in
|
||||||
|
|
@ -83,7 +96,7 @@ def atan(x, context=None):
|
||||||
m = 0
|
m = 0
|
||||||
while x > _point2:
|
while x > _point2:
|
||||||
m += 1
|
m += 1
|
||||||
x = (x-_point2)/(1+_point2*x)
|
x = (x - _point2) / (1 + _point2 * x)
|
||||||
|
|
||||||
r = u = x
|
r = u = x
|
||||||
f = -x * x
|
f = -x * x
|
||||||
|
|
@ -92,12 +105,13 @@ def atan(x, context=None):
|
||||||
|
|
||||||
while t.logb() > -ctx.prec:
|
while t.logb() > -ctx.prec:
|
||||||
u *= f
|
u *= f
|
||||||
t = u/i
|
t = u / i
|
||||||
i += 2
|
i += 2
|
||||||
r += t
|
r += t
|
||||||
|
|
||||||
r += m*a
|
r += m * a
|
||||||
return r/n
|
return r / n
|
||||||
|
|
||||||
|
|
||||||
def sin(x, context=None):
|
def sin(x, context=None):
|
||||||
"""Compute the sine of the specified value, in radians"""
|
"""Compute the sine of the specified value, in radians"""
|
||||||
|
|
@ -110,23 +124,23 @@ def sin(x, context=None):
|
||||||
|
|
||||||
scale = ctx.prec
|
scale = ctx.prec
|
||||||
|
|
||||||
ctx.prec = int(1.1*scale+2)
|
ctx.prec = int(1.1 * scale + 2)
|
||||||
a = atan(1)
|
a = atan(1)
|
||||||
q = (x//a + 2)//4
|
q = (x // a + 2) // 4
|
||||||
x -= 4*q*a
|
x -= 4 * q * a
|
||||||
if q % 2:
|
if q % 2:
|
||||||
x = -x
|
x = -x
|
||||||
ctx.prec = scale+2
|
ctx.prec = scale + 2
|
||||||
r = a = x
|
r = a = x
|
||||||
q = -x*x
|
q = -x * x
|
||||||
i=3
|
i = 3
|
||||||
lim = Decimal(f"10e-{ctx.prec}")
|
|
||||||
while a.logb() > -ctx.prec:
|
while a.logb() > -ctx.prec:
|
||||||
a *= q/(i*(i-1))
|
a *= q / (i * (i - 1))
|
||||||
r += a
|
r += a
|
||||||
i += 2
|
i += 2
|
||||||
|
|
||||||
return r/1
|
return r / 1
|
||||||
|
|
||||||
|
|
||||||
def cos(x, context=None):
|
def cos(x, context=None):
|
||||||
"""Compute the cosine of the specified value, in radians"""
|
"""Compute the cosine of the specified value, in radians"""
|
||||||
|
|
@ -135,9 +149,10 @@ def cos(x, context=None):
|
||||||
|
|
||||||
with localcontext(context) as ctx:
|
with localcontext(context) as ctx:
|
||||||
scale = ctx.prec
|
scale = ctx.prec
|
||||||
ctx.prec = int(scale*1.2)
|
ctx.prec = int(scale * 1.2)
|
||||||
r = sin(2*atan(1)+x)
|
r = sin(2 * atan(1) + x)
|
||||||
return r/1
|
return r / 1
|
||||||
|
|
||||||
|
|
||||||
def tan(x, context=None):
|
def tan(x, context=None):
|
||||||
"""Compute the tangent of the specified value, in radians"""
|
"""Compute the tangent of the specified value, in radians"""
|
||||||
|
|
@ -145,11 +160,11 @@ def tan(x, context=None):
|
||||||
x = Decimal(x)
|
x = Decimal(x)
|
||||||
|
|
||||||
with localcontext(context) as ctx:
|
with localcontext(context) as ctx:
|
||||||
scale = ctx.prec
|
|
||||||
ctx.prec += 2
|
ctx.prec += 2
|
||||||
s = sin(x)
|
s = sin(x)
|
||||||
r = s / (1-s*s).sqrt()
|
r = s / (1 - s * s).sqrt()
|
||||||
return r/1
|
return r / 1
|
||||||
|
|
||||||
|
|
||||||
def asin(x, context=None):
|
def asin(x, context=None):
|
||||||
"""Compute the arcsine of the specified value, in radians"""
|
"""Compute the arcsine of the specified value, in radians"""
|
||||||
|
|
@ -157,10 +172,10 @@ def asin(x, context=None):
|
||||||
x = Decimal(x)
|
x = Decimal(x)
|
||||||
|
|
||||||
with localcontext(context) as ctx:
|
with localcontext(context) as ctx:
|
||||||
scale = ctx.prec
|
|
||||||
ctx.prec += 2
|
ctx.prec += 2
|
||||||
r = atan(x / (1 - x*x).sqrt())
|
r = atan(x / (1 - x * x).sqrt())
|
||||||
return r/1
|
return r / 1
|
||||||
|
|
||||||
|
|
||||||
def acos(x, context=None):
|
def acos(x, context=None):
|
||||||
"""Compute the arccosine of the specified value, in radians"""
|
"""Compute the arccosine of the specified value, in radians"""
|
||||||
|
|
@ -168,13 +183,13 @@ def acos(x, context=None):
|
||||||
x = Decimal(x)
|
x = Decimal(x)
|
||||||
|
|
||||||
with localcontext(context) as ctx:
|
with localcontext(context) as ctx:
|
||||||
scale = ctx.prec
|
|
||||||
ctx.prec += 2
|
ctx.prec += 2
|
||||||
r = atan((1-x*x).sqrt()/x)
|
r = atan((1 - x * x).sqrt() / x)
|
||||||
if r < 0:
|
if r < 0:
|
||||||
r += 4*atan(1)
|
r += 4 * atan(1)
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
||||||
Decimal.tan = tan
|
Decimal.tan = tan
|
||||||
Decimal.sin = sin
|
Decimal.sin = sin
|
||||||
Decimal.cos = cos
|
Decimal.cos = cos
|
||||||
|
|
|
||||||
6
setup.py
6
setup.py
|
|
@ -34,9 +34,7 @@ setup(
|
||||||
# Author details
|
# Author details
|
||||||
author="Adafruit Industries",
|
author="Adafruit Industries",
|
||||||
author_email="circuitpython@adafruit.com",
|
author_email="circuitpython@adafruit.com",
|
||||||
install_requires=[
|
install_requires=["Adafruit-Blinka",],
|
||||||
"Adafruit-Blinka",
|
|
||||||
],
|
|
||||||
# Choose your license
|
# Choose your license
|
||||||
license="MIT",
|
license="MIT",
|
||||||
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
|
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
|
||||||
|
|
@ -52,7 +50,7 @@ setup(
|
||||||
],
|
],
|
||||||
# What does your project relate to?
|
# What does your project relate to?
|
||||||
keywords="adafruit blinka circuitpython micropython udecimal numeric helper arbitrary- "
|
keywords="adafruit blinka circuitpython micropython udecimal numeric helper arbitrary- "
|
||||||
"precision math",
|
"precision math",
|
||||||
# You can just specify the packages manually here if your project is
|
# You can just specify the packages manually here if your project is
|
||||||
# simple. Or you can use find_packages().
|
# simple. Or you can use find_packages().
|
||||||
# TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER,
|
# TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER,
|
||||||
|
|
|
||||||
4
test.py
4
test.py
|
|
@ -3,10 +3,12 @@ import doctest
|
||||||
import jepler_udecimal
|
import jepler_udecimal
|
||||||
import jepler_udecimal.utrig
|
import jepler_udecimal.utrig
|
||||||
|
|
||||||
|
|
||||||
def load_tests(loader, tests, ignore):
|
def load_tests(loader, tests, ignore):
|
||||||
tests.addTests(doctest.DocTestSuite(jepler_udecimal))
|
tests.addTests(doctest.DocTestSuite(jepler_udecimal))
|
||||||
tests.addTests(doctest.DocTestSuite(jepler_udecimal.utrig))
|
tests.addTests(doctest.DocTestSuite(jepler_udecimal.utrig))
|
||||||
return tests
|
return tests
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue