Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(76)

Delta Between Two Patch Sets: Lib/_pydecimal.py

Issue 27779: Sync-up docstrings in C version of the the decimal module
Left Patch Set: Created 3 years, 4 months ago
Right Patch Set: Created 3 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Modules/_decimal/docstrings.h » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # Copyright (c) 2004 Python Software Foundation. 1 # Copyright (c) 2004 Python Software Foundation.
2 # All rights reserved. 2 # All rights reserved.
3 3
4 # Written by Eric Price <eprice at tjhsst.edu> 4 # Written by Eric Price <eprice at tjhsst.edu>
5 # and Facundo Batista <facundo at taniquetil.com.ar> 5 # and Facundo Batista <facundo at taniquetil.com.ar>
6 # and Raymond Hettinger <python at rcn.com> 6 # and Raymond Hettinger <python at rcn.com>
7 # and Aahz <aahz at pobox.com> 7 # and Aahz <aahz at pobox.com>
8 # and Tim Peters 8 # and Tim Peters
9 9
10 # This module should be kept in sync with the latest updates of the 10 # This module should be kept in sync with the latest updates of the
(...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 # that are used in almost every arithmetic operation on 615 # that are used in almost every arithmetic operation on
616 # Decimals. This is an internal detail: the as_tuple function 616 # Decimals. This is an internal detail: the as_tuple function
617 # and the Decimal constructor still deal with tuples of 617 # and the Decimal constructor still deal with tuples of
618 # digits. 618 # digits.
619 619
620 self = object.__new__(cls) 620 self = object.__new__(cls)
621 621
622 # From a string 622 # From a string
623 # REs insist on real strings, so we can too. 623 # REs insist on real strings, so we can too.
624 if isinstance(value, str): 624 if isinstance(value, str):
625 m = _parser(value.strip()) 625 m = _parser(value.strip().replace("_", ""))
626 if m is None: 626 if m is None:
627 if context is None: 627 if context is None:
628 context = getcontext() 628 context = getcontext()
629 return context._raise_error(ConversionSyntax, 629 return context._raise_error(ConversionSyntax,
630 "Invalid literal for Decimal: %r" % value) 630 "Invalid literal for Decimal: %r" % value)
631 631
632 if m.group('sign') == "-": 632 if m.group('sign') == "-":
633 self._sign = 1 633 self._sign = 1
634 else: 634 else:
635 self._sign = 0 635 self._sign = 0
(...skipping 3519 matching lines...) Expand 10 before | Expand all | Expand 10 after
4155 context = context.copy() 4155 context = context.copy()
4156 # so you don't change the calling context 4156 # so you don't change the calling context
4157 # if an error occurs in the middle. 4157 # if an error occurs in the middle.
4158 rounding = context._set_rounding(ROUND_UP) 4158 rounding = context._set_rounding(ROUND_UP)
4159 val = self.__sub__(other, context=context) 4159 val = self.__sub__(other, context=context)
4160 context._set_rounding(rounding) 4160 context._set_rounding(rounding)
4161 4161
4162 This will make it round up for that operation. 4162 This will make it round up for that operation.
4163 """ 4163 """
4164 rounding = self.rounding 4164 rounding = self.rounding
4165 self.rounding= type 4165 self.rounding = type
4166 return rounding 4166 return rounding
4167 4167
4168 def create_decimal(self, num='0'): 4168 def create_decimal(self, num='0'):
4169 """Creates a new Decimal instance but using self as context. 4169 """Creates a new Decimal instance but using self as context.
4170 4170
4171 This method implements the to-number operation of the 4171 This method implements the to-number operation of the
4172 IBM Decimal specification.""" 4172 IBM Decimal specification."""
4173 4173
4174 if isinstance(num, str) and num != num.strip(): 4174 if isinstance(num, str) and (num != num.strip() or '_' in num):
4175 return self._raise_error(ConversionSyntax, 4175 return self._raise_error(ConversionSyntax,
4176 "no trailing or leading whitespace is " 4176 "trailing or leading whitespace and "
4177 "permitted.") 4177 "underscores are not permitted.")
4178 4178
4179 d = Decimal(num, context=self) 4179 d = Decimal(num, context=self)
4180 if d._isnan() and len(d._int) > self.prec - self.clamp: 4180 if d._isnan() and len(d._int) > self.prec - self.clamp:
4181 return self._raise_error(ConversionSyntax, 4181 return self._raise_error(ConversionSyntax,
4182 "diagnostic info too long in NaN") 4182 "diagnostic info too long in NaN")
4183 return d._fix(self) 4183 return d._fix(self)
4184 4184
4185 def create_decimal_from_float(self, f): 4185 def create_decimal_from_float(self, f):
4186 """Creates a new Decimal instance from a float but rounding using self 4186 """Creates a new Decimal instance from a float but rounding using self
4187 as the context. 4187 as the context.
4188 4188
4189 >>> context = Context(prec=5, rounding=ROUND_DOWN) 4189 >>> context = Context(prec=5, rounding=ROUND_DOWN)
4190 >>> context.create_decimal_from_float(3.1415926535897932) 4190 >>> context.create_decimal_from_float(3.1415926535897932)
4191 Decimal('3.1415') 4191 Decimal('3.1415')
4192 >>> context = Context(prec=5, traps=[Inexact]) 4192 >>> context = Context(prec=5, traps=[Inexact])
4193 >>> context.create_decimal_from_float(3.1415926535897932) 4193 >>> context.create_decimal_from_float(3.1415926535897932)
4194 Traceback (most recent call last): 4194 Traceback (most recent call last):
4195 ... 4195 ...
4196 decimal.Inexact 4196 decimal.Inexact: None
4197 4197
4198 """ 4198 """
4199 d = Decimal.from_float(f) # An exact conversion 4199 d = Decimal.from_float(f) # An exact conversion
4200 return d._fix(self) # Apply the context rounding 4200 return d._fix(self) # Apply the context rounding
4201 4201
4202 # Methods 4202 # Methods
4203 def abs(self, a): 4203 def abs(self, a):
4204 """Returns the absolute value of the operand. 4204 """Returns the absolute value of the operand.
4205 4205
4206 If the operand is negative, the result is the same as using the minus 4206 If the operand is negative, the result is the same as using the minus
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
4300 """ 4300 """
4301 a = _convert_other(a, raiseit=True) 4301 a = _convert_other(a, raiseit=True)
4302 return a.compare(b, context=self) 4302 return a.compare(b, context=self)
4303 4303
4304 def compare_signal(self, a, b): 4304 def compare_signal(self, a, b):
4305 """Compares the values of the two operands numerically. 4305 """Compares the values of the two operands numerically.
4306 4306
4307 It's pretty much like compare(), but all NaNs signal, with signaling 4307 It's pretty much like compare(), but all NaNs signal, with signaling
4308 NaNs taking precedence over quiet NaNs. 4308 NaNs taking precedence over quiet NaNs.
4309 4309
4310 >>> c = ExtendedContext 4310 >>> ExtendedContext.compare_signal(Decimal('2.1'), Decimal('3'))
4311 >>> c.compare_signal(Decimal('2.1'), Decimal('3'))
4312 Decimal('-1') 4311 Decimal('-1')
4313 >>> c.compare_signal(Decimal('2.1'), Decimal('2.1')) 4312 >>> ExtendedContext.compare_signal(Decimal('2.1'), Decimal('2.1'))
4314 Decimal('0') 4313 Decimal('0')
4315 >>> c.flags[InvalidOperation] = 0 4314 >>> ExtendedContext.flags[InvalidOperation] = 0
4316 >>> print(c.flags[InvalidOperation]) 4315 >>> print(ExtendedContext.flags[InvalidOperation])
4317 0 4316 0
4318 >>> c.compare_signal(Decimal('NaN'), Decimal('2.1')) 4317 >>> ExtendedContext.compare_signal(Decimal('NaN'), Decimal('2.1'))
4319 Decimal('NaN') 4318 Decimal('NaN')
4320 >>> print(c.flags[InvalidOperation]) 4319 >>> print(ExtendedContext.flags[InvalidOperation])
4321 1 4320 1
4322 >>> c.flags[InvalidOperation] = 0 4321 >>> ExtendedContext.flags[InvalidOperation] = 0
4323 >>> print(c.flags[InvalidOperation]) 4322 >>> print(ExtendedContext.flags[InvalidOperation])
4324 0 4323 0
4325 >>> c.compare_signal(Decimal('sNaN'), Decimal('2.1')) 4324 >>> ExtendedContext.compare_signal(Decimal('sNaN'), Decimal('2.1'))
4326 Decimal('NaN') 4325 Decimal('NaN')
4327 >>> print(c.flags[InvalidOperation]) 4326 >>> print(ExtendedContext.flags[InvalidOperation])
4328 1 4327 1
4329 >>> c.compare_signal(-1, 2) 4328 >>> ExtendedContext.compare_signal(-1, 2)
4330 Decimal('-1') 4329 Decimal('-1')
4331 >>> c.compare_signal(Decimal(-1), 2) 4330 >>> ExtendedContext.compare_signal(Decimal(-1), 2)
4332 Decimal('-1') 4331 Decimal('-1')
4333 >>> c.compare_signal(-1, Decimal(2)) 4332 >>> ExtendedContext.compare_signal(-1, Decimal(2))
4334 Decimal('-1') 4333 Decimal('-1')
4335 """ 4334 """
4336 a = _convert_other(a, raiseit=True) 4335 a = _convert_other(a, raiseit=True)
4337 return a.compare_signal(b, context=self) 4336 return a.compare_signal(b, context=self)
4338 4337
4339 def compare_total(self, a, b): 4338 def compare_total(self, a, b):
4340 """Compares two operands using their abstract representation rather 4339 """Compares two operands using their abstract representation rather
4341 than their numerical value. 4340 than their numerical value.
4342 4341
4343 Similar to the compare() method, but the result gives a total ordering 4342 Similar to the compare() method, but the result gives a total ordering
4344 on Decimal instances. 4343 on Decimal instances.
4345 4344
4346 Quiet and signaling NaNs are also included in the total ordering. 4345 Quiet and signaling NaNs are also included in the total ordering.
4347 The result of this function is Decimal('0') if both operands have the 4346 The result of this function is Decimal('0') if both operands have the
4348 same representation, Decimal('-1') if the first operand is lower in 4347 same representation, Decimal('-1') if the first operand is lower in
4349 the total order than the second, and Decimal('1') if the first 4348 the total order than the second, and Decimal('1') if the first
4350 operand is higher in the total order than the second operand. See 4349 operand is higher in the total order than the second operand. See
4351 the specification for details of the total order. 4350 the specification for details of the total order.
4352 4351
4353 This operation is unaffected by context and is quiet: no flags are 4352 This operation is unaffected by context and is quiet: no flags are
4354 changed and no rounding is performed. 4353 changed and no rounding is performed.
4355 4354
4356 >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9')) 4355 >>> ExtendedContext.compare_total(Decimal('12.73'), Decimal('127.9'))
4357 Decimal('-1') 4356 Decimal('-1')
4358 >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12')) 4357 >>> ExtendedContext.compare_total(Decimal('-127'), Decimal('12'))
4359 Decimal('-1') 4358 Decimal('-1')
4360 >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3')) 4359 >>> ExtendedContext.compare_total(Decimal('12.30'), Decimal('12.3'))
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
4422 Decimal('-101.5') 4421 Decimal('-101.5')
4423 >>> ExtendedContext.copy_negate(Decimal('-101.5')) 4422 >>> ExtendedContext.copy_negate(Decimal('-101.5'))
4424 Decimal('101.5') 4423 Decimal('101.5')
4425 >>> ExtendedContext.copy_negate(1) 4424 >>> ExtendedContext.copy_negate(1)
4426 Decimal('-1') 4425 Decimal('-1')
4427 """ 4426 """
4428 a = _convert_other(a, raiseit=True) 4427 a = _convert_other(a, raiseit=True)
4429 return a.copy_negate() 4428 return a.copy_negate()
4430 4429
4431 def copy_sign(self, a, b): 4430 def copy_sign(self, a, b):
4432 """Copies the second operand's sign to the first one. 4431 """Returns a copy of the first operand with the sign equal to the second
4433 4432 operand.
4434 In detail, it returns a copy of the first operand with the sign
4435 equal to the sign of the second operand.
4436 4433
4437 >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33')) 4434 >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('7.33'))
4438 Decimal('1.50') 4435 Decimal('1.50')
4439 >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33')) 4436 >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('7.33'))
4440 Decimal('1.50') 4437 Decimal('1.50')
4441 >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33')) 4438 >>> ExtendedContext.copy_sign(Decimal( '1.50'), Decimal('-7.33'))
4442 Decimal('-1.50') 4439 Decimal('-1.50')
4443 >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33')) 4440 >>> ExtendedContext.copy_sign(Decimal('-1.50'), Decimal('-7.33'))
4444 Decimal('-1.50') 4441 Decimal('-1.50')
4445 >>> ExtendedContext.copy_sign(1, -2) 4442 >>> ExtendedContext.copy_sign(1, -2)
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
4530 (Decimal('2'), Decimal('0')) 4527 (Decimal('2'), Decimal('0'))
4531 """ 4528 """
4532 a = _convert_other(a, raiseit=True) 4529 a = _convert_other(a, raiseit=True)
4533 r = a.__divmod__(b, context=self) 4530 r = a.__divmod__(b, context=self)
4534 if r is NotImplemented: 4531 if r is NotImplemented:
4535 raise TypeError("Unable to convert %s to Decimal" % b) 4532 raise TypeError("Unable to convert %s to Decimal" % b)
4536 else: 4533 else:
4537 return r 4534 return r
4538 4535
4539 def exp(self, a): 4536 def exp(self, a):
4540 """Return the value of the (natural) exponential function e ** a. 4537 """Return the value of the (natural) exponential function e ** a.
4541 The function always uses the ROUND_HALF_EVEN mode and the result 4538 The function always uses the ROUND_HALF_EVEN mode and the result
4542 is correctly rounded. 4539 is correctly rounded.
4543 4540
4544 >>> c = ExtendedContext.copy() 4541 >>> c = ExtendedContext.copy()
4545 >>> c.Emin = -999 4542 >>> c.Emin = -999
4546 >>> c.Emax = 999 4543 >>> c.Emax = 999
4547 >>> c.exp(Decimal('-Infinity')) 4544 >>> c.exp(Decimal('-Infinity'))
4548 Decimal('0') 4545 Decimal('0')
4549 >>> c.exp(Decimal('-1')) 4546 >>> c.exp(Decimal('-1'))
4550 Decimal('0.367879441') 4547 Decimal('0.367879441')
(...skipping 832 matching lines...) Expand 10 before | Expand all | Expand 10 after
5383 Decimal('1') 5380 Decimal('1')
5384 >>> ExtendedContext.quantize(Decimal(1), 2) 5381 >>> ExtendedContext.quantize(Decimal(1), 2)
5385 Decimal('1') 5382 Decimal('1')
5386 >>> ExtendedContext.quantize(1, Decimal(2)) 5383 >>> ExtendedContext.quantize(1, Decimal(2))
5387 Decimal('1') 5384 Decimal('1')
5388 """ 5385 """
5389 a = _convert_other(a, raiseit=True) 5386 a = _convert_other(a, raiseit=True)
5390 return a.quantize(b, context=self) 5387 return a.quantize(b, context=self)
5391 5388
5392 def radix(self): 5389 def radix(self):
5393 """Just returns 10, as this is Decimal, :) 5390 """Return Decimal(10), the radix (base) in which the Decimal class does
5391 all its arithmetic. Included for compatibility with the specification .
5394 5392
5395 >>> ExtendedContext.radix() 5393 >>> ExtendedContext.radix()
5396 Decimal('10') 5394 Decimal('10')
5397 """ 5395 """
5398 return Decimal(10) 5396 return Decimal(10)
5399 5397
5400 def remainder(self, a, b): 5398 def remainder(self, a, b):
5401 """Returns the remainder from integer division. 5399 """Returns the remainder from integer division.
5402 5400
5403 The result is the residue of the dividend after the operation of 5401 The result is the residue of the dividend after the operation of
(...skipping 1112 matching lines...) Expand 10 before | Expand all | Expand 10 after
6516 # Constants related to the hash implementation; hash(x) is based 6514 # Constants related to the hash implementation; hash(x) is based
6517 # on the reduction of x modulo _PyHASH_MODULUS 6515 # on the reduction of x modulo _PyHASH_MODULUS
6518 _PyHASH_MODULUS = sys.hash_info.modulus 6516 _PyHASH_MODULUS = sys.hash_info.modulus
6519 # hash values to use for positive and negative infinities, and nans 6517 # hash values to use for positive and negative infinities, and nans
6520 _PyHASH_INF = sys.hash_info.inf 6518 _PyHASH_INF = sys.hash_info.inf
6521 _PyHASH_NAN = sys.hash_info.nan 6519 _PyHASH_NAN = sys.hash_info.nan
6522 6520
6523 # _PyHASH_10INV is the inverse of 10 modulo the prime _PyHASH_MODULUS 6521 # _PyHASH_10INV is the inverse of 10 modulo the prime _PyHASH_MODULUS
6524 _PyHASH_10INV = pow(10, _PyHASH_MODULUS - 2, _PyHASH_MODULUS) 6522 _PyHASH_10INV = pow(10, _PyHASH_MODULUS - 2, _PyHASH_MODULUS)
6525 del sys 6523 del sys
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+