Package: decimal128
decimal128This package is primarily a fork of thedecimal128 supportfound in theMondoDB python driver. The motivation for this project is to provide a standalone package with no dependencies that does not need all of MongoDB's python driver.UsageTheDecimal128class behaves largely like a standard pythondecimal.Decimalclass. Most arithmetic operators are  included and the package supports a small number of additional init values to allow for conversion from binary integer decimal (BID).Decimal128uses an instance ofdecimal.Contextconfigured for IEEE-754 Decimal128 when validating parameters.Unlike MongoDB'sDecimal128, no signals are trapped and as such, exceptions are not raised fordecimal.InvalidOperation,decimal.Inexact, ordecimal.Overflow.Supported init values arestr,int,float, standarddecimal.Decimalobjects, BIDtuple, and using a classmethof for BIDbytes:>>> Decimal128("0.0625")
Decimal128('0.0625')
>>> Decimal128(Decimal("0.0625"))
Decimal128('0.0625')
>>> Decimal128(625)
Decimal128('625')
>>> Decimal128(0.0625)
Decimal128('0.0625')
>>> Decimal128((3474527112516337664,625))
Decimal128('0.0625')
>>> Decimal128.from_bid(bytes.fromhex("30380000000000000000000000000271"))
Decimal128('0.0625')decimal.DecimalcompatibilityThe helper functioncreate_decimal128_contextcreates adecimal.Contextwhich ensures that decimals creted by the standard library are identical to those created byDecimal128.>>> import decimal
>>> decimal128_ctx = create_decimal128_context()
>>> with decimal.localcontext(decimal128_ctx) as ctx:
...     Decimal128(ctx.create_decimal(".13.3"))
...
Decimal128('NaN')
>>>
>>> with decimal.localcontext(decimal128_ctx) as ctx:
...     Decimal128(ctx.create_decimal("1E-6177"))
...
Decimal128('0E-6176')
>>>
>>> with decimal.localcontext(decimal128_ctx) as ctx:
...     Decimal128(ctx.create_decimal("1E6145"))
...
Decimal128('Infinity')Theto_decimal()method returns adecimal.Decimalobject for the stored value.Propertiesbid(): →bytesThe binary integer decimal (BID) encoding of this instance. BIDs are 16 bytes with the highest bits encoded first:>>> Decimal128("999").bid().hex()
'304000000000000000000000000003e7'
>>> Decimal128("NaN").bid().hex()
'7c000000000000000000000000000000'Methodsfrom_bid(value:bytes)Create an instance ofDecimal128from binary integer decimal bytes.Parameters:value: 16 byte string (128-bit IEEE 754-2008 decimal floating point in binary integer decimal (BID) format).is_infinite() →boolReturnTrueif self is infinite; otherwise returnFalse.is_nan() →boolReturnTrueif self is a NaN; otherwise returnFalse.is_signed() →boolReturnTrueif self is negative; otherwise returnFalse.to_decimal() ->decimal.DecimalReturns an instance ofdecimal.Decimalfor thisDecimal128.to_float() →floatReturns an instance of float for thisDecimal128.PerformanceDecimal128is considerably slower thandecimal.Decimalusing native libraries as all arithmetic operations involve creating new objects to perform the operation. Expect performance to be 200-300 times slower thandecimal.Decimal.Creditsdecimal128was built byJon Connellbut started as a fork of thedecimal128 supportfound in theMondoDB python driver.Tests are adapted from the MongoDB tests for decimal128 and also fromJohn Dupuy'sNimimplementationof decimal128.LicenseThis project is licensed under theApache License 2.0.Some tests are licensed under theMIT Licensewhich can be identified in the source comments.