MCPcopy Index your code
hub / github.com/RustPython/RustPython / Decimal

Class Decimal

Lib/_pydecimal.py:449–3806  ·  view source on GitHub ↗

Floating-point class for decimal arithmetic.

Source from the content-addressed store, hash-verified

447# numbers.py for more detail.
448
449class Decimal(object):
450 """Floating-point class for decimal arithmetic."""
451
452 __slots__ = ('_exp','_int','_sign', '_is_special')
453 # Generally, the value of the Decimal instance is given by
454 # (-1)**_sign * _int * 10**_exp
455 # Special values are signified by _is_special == True
456
457 # We're immutable, so use __new__ not __init__
458 def __new__(cls, value="0", context=None):
459 """Create a decimal point instance.
460
461 >>> Decimal('3.14') # string input
462 Decimal('3.14')
463 >>> Decimal((0, (3, 1, 4), -2)) # tuple (sign, digit_tuple, exponent)
464 Decimal('3.14')
465 >>> Decimal(314) # int
466 Decimal('314')
467 >>> Decimal(Decimal(314)) # another decimal instance
468 Decimal('314')
469 >>> Decimal(' 3.14 \\n') # leading and trailing whitespace okay
470 Decimal('3.14')
471 """
472
473 # Note that the coefficient, self._int, is actually stored as
474 # a string rather than as a tuple of digits. This speeds up
475 # the "digits to integer" and "integer to digits" conversions
476 # that are used in almost every arithmetic operation on
477 # Decimals. This is an internal detail: the as_tuple function
478 # and the Decimal constructor still deal with tuples of
479 # digits.
480
481 self = object.__new__(cls)
482
483 # From a string
484 # REs insist on real strings, so we can too.
485 if isinstance(value, str):
486 m = _parser(value.strip().replace("_", ""))
487 if m is None:
488 if context is None:
489 context = getcontext()
490 return context._raise_error(ConversionSyntax,
491 "Invalid literal for Decimal: %r" % value)
492
493 if m.group('sign') == "-":
494 self._sign = 1
495 else:
496 self._sign = 0
497 intpart = m.group('int')
498 if intpart is not None:
499 # finite number
500 fracpart = m.group('frac') or ''
501 exp = int(m.group('exp') or '0')
502 self._int = str(int(intpart+fracpart))
503 self._exp = exp - len(fracpart)
504 self._is_special = False
505 else:
506 diag = m.group('diag')

Callers 15

_decimal_sqrt_of_fracFunction · 0.90
from_decimalMethod · 0.90
testInitFromDecimalMethod · 0.90
testFromDecimalMethod · 0.90
testFromNumberMethod · 0.90
testMixingWithDecimalMethod · 0.90
test_decistmtMethod · 0.90
test_numbersMethod · 0.90
test_localizeMethod · 0.90

Calls

no outgoing calls

Tested by 15

testInitFromDecimalMethod · 0.72
testFromDecimalMethod · 0.72
testFromNumberMethod · 0.72
testMixingWithDecimalMethod · 0.72
test_decistmtMethod · 0.72
test_numbersMethod · 0.72
test_localizeMethod · 0.72