Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew2 <andrew.xia@live.ca>2013-12-03 03:48:04 (GMT)
committer Andrew2 <andrew.xia@live.ca>2013-12-03 03:48:04 (GMT)
commit6c9516300511aaa0d708e6582744c5b593c23df4 (patch)
treef8591f969a93e59ba1f587d68e9bac7c7b6fdf09
parent928e10ff671126d7a6efe85b4fa4e6cfe41c3833 (diff)
Outlined in ticket 2697. Works by checking amount of digits in "x" thenHEADmaster
rounding "x" to the appropriate amount of digits. Also added code to return 0 when "x" is pi or pi/2 to the appropriate sin, cos or tan function. Work by Andrew Xia
-rw-r--r--functions.py70
1 files changed, 55 insertions, 15 deletions
diff --git a/functions.py b/functions.py
index 8a737ff..f335d19 100644
--- a/functions.py
+++ b/functions.py
@@ -70,6 +70,7 @@ _FUNCTIONS = [
_('tan'),
_('tanh'),
_('xor'),
+ _('countDigits')
]
def _d(val):
@@ -112,13 +113,17 @@ abs.__doc__ = _(
'abs(x), return absolute value of x, which means -x for x < 0')
def acos(x):
- return _inv_scale_angle(math.acos(x))
+ n = countDigits(x)
+ x = _inv_scale_angle(math.acos(x))
+ return round(x, int(n - math.ceil(math.log10(abs(x)))))
acos.__doc__ = _(
'acos(x), return the arc cosine of x. This is the angle for which the cosine \
is x. Defined for -1 <= x < 1')
def acosh(x):
- return math.acosh(x)
+ n = countDigits(x)
+ x = math.acosh(x)
+ return round(x, int(n - math.ceil(math.log10(abs(x)))))
acosh.__doc__ = _(
'acosh(x), return the arc hyperbolic cosine of x. This is the value y for \
which the hyperbolic cosine equals x.')
@@ -136,25 +141,33 @@ def add(x, y):
add.__doc__ = _('add(x, y), return x + y')
def asin(x):
- return _inv_scale_angle(math.asin(x))
+ n = countDigits(x)
+ x = _inv_scale_angle(math.asin(x))
+ return round(x, int(n - math.ceil(math.log10(abs(x)))))
asin.__doc__ = _(
'asin(x), return the arc sine of x. This is the angle for which the sine is x. \
Defined for -1 <= x <= 1')
def asinh(x):
- return math.asinh(x)
+ n = countDigits(x)
+ x = math.asinh(x)
+ return round(x, int(n - math.ceil(math.log10(abs(x)))))
asinh.__doc__ = _(
'asinh(x), return the arc hyperbolic sine of x. This is the value y for \
which the hyperbolic sine equals x.')
def atan(x):
- return _inv_scale_angle(math.atan(x))
+ n = countDigits(x)
+ x = _inv_scale_angle(math.atan(x))
+ return round(x, int(n - math.ceil(math.log10(abs(x)))))
atan.__doc__ = _(
'atan(x), return the arc tangent of x. This is the angle for which the tangent \
is x. Defined for all x')
def atanh(x):
- return math.atanh(x)
+ n = countDigits(x)
+ x = math.atanh(x)
+ return round(x, int(n - math.ceil(math.log10(abs(x)))))
atanh.__doc__ = _(
'atanh(x), return the arc hyperbolic tangent of x. This is the value y for \
which the hyperbolic tangent equals x.')
@@ -183,13 +196,19 @@ def ceil(x):
ceil.__doc__ = _('ceil(x), return the smallest integer larger than x.')
def cos(x):
- return math.cos(_scale_angle(x))
+ if(x % (math.pi/2) == 0 and x % math.pi != 0):
+ return 0
+ n = countDigits(x)
+ x = math.cos(_scale_angle(x))
+ return round(x, int(n - math.ceil(math.log10(abs(x)))))
cos.__doc__ = _(
'cos(x), return the cosine of x. This is the x-coordinate on the unit circle \
at the angle x')
def cosh(x):
- return math.cosh(x)
+ n = countDigits(x)
+ x = math.cosh(x)
+ return round(x, int(n - math.ceil(math.log10(abs(x)))))
cosh.__doc__ = _(
'cosh(x), return the hyperbolic cosine of x. Given by (exp(x) + exp(-x)) / 2')
@@ -371,9 +390,9 @@ rand_int.__doc__ = _(
'rand_int([<maxval>]), return a random integer between 0 and <maxval>. \
<maxval> is an optional argument and is set to 65535 by default.')
-def round(x):
+"""def round(x):
return math.round(float(x))
-round.__doc__ = _('round(x), return the integer nearest to x.')
+round.__doc__ = _('round(x), return the integer nearest to x.')"""
def shift_left(x, y):
if is_int(x) and is_int(y):
@@ -392,20 +411,28 @@ shift_right.__doc__ = _(
'shift_right(x, y), shift x by y bits to the right (divide by 2 per bit)')
def sin(x):
- return math.sin(_scale_angle(x))
+ if(x % math.pi == 0):
+ return 0
+ n = countDigits(x)
+ x = math.sin(_scale_angle(x))
+ return round(x, int(n - math.ceil(math.log10(abs(x)))))
sin.__doc__ = _(
'sin(x), return the sine of x. This is the y-coordinate on the unit circle at \
the angle x')
def sinh(x):
- return math.sinh(x)
+ n = countDigits(x)
+ x = math.sinh(x)
+ return round(x, int(n - math.ceil(math.log10(abs(x)))))
sinh.__doc__ = _(
'sinh(x), return the hyperbolic sine of x. Given by (exp(x) - exp(-x)) / 2')
def sinc(x):
if float(x) == 0.0:
return 1
- return sin(x) / x
+ n = countDigits(x)
+ x = sin(x) / x
+ return round(x, int(n - math.ceil(math.log10(abs(x)))))
sinc.__doc__ = _(
'sinc(x), return the sinc of x. This is given by sin(x) / x.')
@@ -427,14 +454,20 @@ def sub(x, y):
sub.__doc__ = _('sub(x, y), return x - y')
def tan(x):
- return math.tan(_scale_angle(x))
+ if(x % math.pi == 0):
+ return 0
+ n = countDigits(x)
+ x = math.tan(_scale_angle(x))
+ return round(x, int(n - math.ceil(math.log10(abs(x)))))
tan.__doc__ = _(
'tan(x), return the tangent of x. This is the slope of the line from the origin \
of the unit circle to the point on the unit circle defined by the angle x. Given \
by sin(x) / cos(x)')
def tanh(x):
- return math.tanh(x)
+ n = countDigits(x)
+ x = math.tanh(x)
+ return round(x, int(n - math.ceil(math.log10(abs(x)))))
tanh.__doc__ = _(
'tanh(x), return the hyperbolic tangent of x. Given by sinh(x) / cosh(x)')
@@ -444,3 +477,10 @@ xor.__doc__ = _(
'xor(x, y), logical xor. Returns True if either x is True (and y is False) \
or y is True (and x is False), else returns False')
+def countDigits(x):
+ if(x % 1 == 0):
+ return len(str(x))
+ else:
+ return len(str(x)) - 1
+countDigits.__doc__ = _(
+'Returns the number of digits in x')