Package: chainfix
Welcome to ChainfixChainfix provides a way to represent numbers and perform simple math operations using fixed-point data types.Chainfix supports binary fixed-point (base-2), decimal fixed-point (base-10)
or even arbitrary (base-N) fixed-point types.Fixed-point data types are used in a wide range of computing applications.
Binary-fixed point types are commonly used in the design of specialized hardware,
FPGAs, ASICS, and signal processing applications.Decimal-fixed point types are commonly found in financial applications, blockchain,
decentralized finance, and smart contracts
(e.g. thesolidity programming language).Decimal fixed-point representationsThe real-world value π can be represented with limited precision using
two bytes indecimalfixed point format>>>fromchainfiximport*>>>frommathimportpi>>>pid=Fixd(pi,wordlength=16,precision=4)Fixd(3.1416,16,4)The.intproperty returns the stored integer value:>>>pid.int31416The resolution of the decimal fixed-point data type is10 ** -precision, which
is also the value of one least significant bit:>>>pid.lsb0.0001The range of numbers that can be represented with this precision using 16 bits is:>>>(pid.lower_bound,pid.upper_bound)(-3.2768,3.2767)The real-world value can also be displayed as an exact ratio of integers>>>pid.as_integer_ratio()(3927,1250)The.hexproperty returns the two's complement representation of the stored integer>>>pid.hex'0x7ab8'WhenFixdstores a negative number, the MSB of the stored integer is always 1:>>>Fixd(-pi,16,4).hex'0x8548'Binary fixed-point representationsLikewise, π can also be represented with limited precision using
two bytes inbinaryfixed point format>>>pib=Fixb(pi,16,12)Fixb(3.1416015625,16,12)The.intproperty returns the stored integer value:>>>pib.int12868The resolution of the decimal fixed-point data type is2 ** -precision, which
is also the value of one least significant bit:>>>pib.lsb0.000244140625The range of numbers that can be represented with this precision using 16 bits is:>>>(pib.lower_bound,pib.upper_bound)(-8.0,7.999755859375)The.hexproperty returns the two's complement representation of the stored integer>>>pib.hex'0x3244'WhenFixbis used to store a negative number, the MSB of the stored integer is always 1:>>>Fixb(-pi,16,4).hex'0xffce'ContextsChainfix provides a fixed-pointcontextto control the default behavior for new fixed-point objects.The current context can be retrieved using>>>ctx=get_decimal_context()>>>ctx.wordlength256>>>ctx.precision18forFixdandUfixdvalues or>>>ctx=get_binary_context()>>>ctx.wordlength32>>>ctx.precision16forFixbandUfixbvalues.The context can be modified to change the behavior of newly constructed values:>>>get_decimal_context().wordlength=20>>>get_decimal_context().precision=5>>>Fixd(pi)Fixd(3.14159,20,5)When passed through the constructor, the values forwordlengthandprecisionalways take precedence over the
context.>>>Fixd(pi,precision=10)Fixd(3.1415926536,256,10)Note that resulting data type has insufficinet range to represent the value pi.Future WorkSupport math operations for fixed-point types using the applicable contextContributingChainfix can be installed in developer mode after cloning the repository:$pipinstall-e.To run all tests:$pytest