Ticket #4807: Changes.diff

File Changes.diff, 3.3 KB (added by gauravm043, 9 years ago)

Hi , I have rectified the errors mentioned in the ticket along with correcting ln(x), inv(x) . godiard and gonzalo_ Please review.

  • astparser.py

    diff --git a/astparser.py b/astparser.py
    index c97055c..a202944 100644
    a b class RuntimeError(ParserError): 
    107107            msg += ": %s" % (self._msg)
    108108        return msg
    109109
     110class ArguementError(ParserError):
     111     """Class for error during executing."""
     112     
     113     def __init__(self, msg, start, eqn, end=None):
     114         ParserError.__init__(self, msg, start, end)
     115     
     116     def __str__(self):
     117         msg=''
     118         msg += ": %s" % (self._msg)
     119         return msg
     120
    110121class Helper:
    111122
    112123    def __init__(self, parent):
    class AstParser: 
    462473                return ret
    463474            except Exception, e:
    464475                msg = str(e)
    465                 raise RuntimeError(msg, ofs)
     476                raise ArguementError(msg, ofs, 0)
     477               
    466478
    467479        elif isinstance(node, ast.Num):
    468480            if type(node.n) == types.FloatType:
  • calculate.py

    diff --git a/calculate.py b/calculate.py
    index 7ee58a4..3cdbd2f 100644
    a b from sugar.graphics.xocolor import XoColor 
    4242from shareable_activity import ShareableActivity
    4343from layout import CalcLayout
    4444from mathlib import MathLib
    45 from astparser import AstParser, ParserError, RuntimeError
     45from astparser import AstParser, ParserError, RuntimeError, ArguementError
    4646from svgimage import SVGImage
    4747
    4848from decimal import Decimal
  • functions.py

    diff --git a/functions.py b/functions.py
    index 8a737ff..b5efe99 100644
    a b abs.__doc__ = _( 
    112112'abs(x), return absolute value of x, which means -x for x < 0')
    113113
    114114def acos(x):
    115     return _inv_scale_angle(math.acos(x))
     115    if x>1 or x<-1:
     116        raise ValueError(_('acos(x) only defined for x E [-1,1]'))
     117    else:
     118        return _inv_scale_angle(math.acos(x))
    116119acos.__doc__ = _(
    117120'acos(x), return the arc cosine of x. This is the angle for which the cosine \
    118121is x. Defined for -1 <= x < 1')
    def add(x, y): 
    136139add.__doc__ = _('add(x, y), return x + y')
    137140
    138141def asin(x):
     142    if x>1 or x<-1:
     143        raise ValueError(_('asin(x) only defined for x E [-1,1]'))
    139144    return _inv_scale_angle(math.asin(x))
    140145asin.__doc__ = _(
    141146'asin(x), return the arc sine of x. This is the angle for which the sine is x. \
    def exp(x): 
    227232exp.__doc__ = _('exp(x), return the natural exponent of x. Given by e^x')
    228233
    229234def factorial(n):
     235    if n<0:
     236        raise ValueError(_('Factorial(x) is only defined for integers x>=0'))
     237
    230238    if type(n) not in (types.IntType, types.LongType):
    231         raise ValueError(_('Factorial only defined for integers'))
     239        raise ValueError(_('Factorial(x) is only defined for integers x>=0'))
    232240
    233241    if n == 0:
    234242        return 1
    def floor(x): 
    283291floor.__doc__ = _('floor(x), return the largest integer smaller than x.')
    284292
    285293def inv(x):
    286     return 1 / x
     294    if x==0:
     295        raise ValueError(_('Can not divide by zero'))
     296    return div(1, x)
    287297inv.__doc__ = _('inv(x), return the inverse of x, which is 1 / x')
    288298
    289299def is_int(n):
    is_int.__doc__ = ('is_int(n), determine whether n is an integer.') 
    304314
    305315def ln(x):
    306316    if float(x) > 0:
    307         return math.log(float(x))
     317        return div(math.log(float(x)),math.log(math.e))
    308318    else:
    309319        raise ValueError(_('Logarithm(x) only defined for x > 0'))
    310320ln.__doc__ = _(