fix

class pyphix.fix.FixFmt(signed, int_bits, frac_bits)[source]

Fix format class

Parameters:
  • signed (bool) – indicate whether the representation is signed (True) or unsigned
  • int_bits (int) – number of bits representing the integer part
  • frac_bits (int) – number of bits representing the fractional part
mask

Return mask to limit number representation (bit_length*ones).

Ex:

>>> from pyphix import fix
>>> fmt = fix.FixFmt(True, 2, 3)
>>> bin(fmt.mask)
    '0b111111'
>>> fmt = fix.FixFmt(False, 1, 12)
>>> bin(fmt.mask)
    '0b1111111111111'
bit_length

Return the number of bits required to represent a number with current fix format.

maxvalue(fmt='float')[source]

Return max representable value by current fix format objext.

Parameters:fmt (EFormat or str) – format the value is presented, either hex, int, bin, float.
Returns:max representable value.
Return type:float or int or str
minvalue(fmt='float')[source]

Return min representable value by current fix format objext.

fixrange

Return the range representable by fix format object as tuple (min, max).

tuplefmt

Return object as a tuple.

listfmt

Return object as a list.

class pyphix.fix.FixNum(value, fmt, rnd='SymZero', over='Wrap')[source]

Fixed point number class

Round methods
SymInf positive numbers tend to +inf, negative numbers to -inf
SymZero round toward zero (DEFAULT)
NonSymPos round toward +inf
NonSymNeg round toward -inf
ConvEven round to closest even
ConvOdd round to closest odd
Floor round to largest previous
Ceil round to smallest following
Overflow methods
Sat saturate
Wrap wrap around – DEFAULT
Parameters:
  • value (np.ndarray(ndim > 0) or float) – value to represent in fix point
  • fmt (FixFmt) – fix point format
  • rnd (str) – round method
  • over (str) – overflow method
change_fix(new_fmt, new_rnd=None, new_over=None)[source]

Change fix parameters of current object.

WARNING: this action may lead to information loss due to new format and round/overflow methods.

Parameters:
  • new_fmt (FixFmt) – new format (mandatory).
  • new_rnd (str or None) – new round method, if not specified current is used.
  • new_over (str or None) – new saturation method, if not specified current is used.
Returns:

new formatted fix-point object.

Return type:

FixFmt

binfmt

Represent fix-point object in binary format.

hexfmt

Represent fix-point object in hexadecimal format.

intfmt

Represent fix-point object in integer format.

fimath

Return fix math as tuple (round method, overflow mode).

add(*args, **kwargs)[source]

Addition method.

Usage: add(other, out_fmt=None, out_rnd=”SymZero”, out_over=”Wrap”)

It allows to decide the output format. If not indicated, full-precision format will be adopted.

Parameters:
  • other (FixNum) – fix-point object.
  • out_fmt (FixFmt) – optional format operation result can be casted to.
  • out_rnd (str) – round method adopted on result (default `SymZero`).
  • out_over (str) – overflow method adopted on result (default `Wrap`).
Returns:

addition result.

Return type:

FixNum

sub(*args, **kwargs)[source]

Subtraction method.

Usage: sub(other, out_fmt=None, out_rnd=”SymZero”, out_over=”Wrap”)

It allows to decide output format. If not indicated, full-precision format will be adopted.

Parameters:
  • other (FixNum) – fix-point object.
  • out_fmt (FixFmt) – optional format operation result can be casted to.
  • out_rnd (str) – round method adopted on result (default `SymZero`).
  • out_over (str) – overflow method adopted on result (default `Wrap`).
Returns:

operation result.

Return type:

FixNum

mult(*args, **kwargs)[source]

Multiplication method.

Usage: mult(other, out_fmt=None, out_rnd=”SymZero”, out_over=”Wrap”)

It allows to decide output format. If not indicated, full-precision format will be adopted.

Parameters:
  • other (FixNum) – fix-point object.
  • out_fmt (FixFmt) – optional format operation result can be casted to.
  • out_rnd (str) – round method adopted on result (default `SymZero`).
  • out_over (str) – overflow method adopted on result (default `Wrap`).
Returns:

operation result.

Return type:

FixNum