APyCFloat
¶
- class apytypes.APyCFloat(*args, **kwargs)¶
Class for configurable complex-valued scalar floating-point formats.
Added in version 0.4.
Note
For real-valued floating-point formats, see
APyFloat
andAPyFloatArray
.- Attributes:
Methods
Change format of the complex-valued floating-point number.
copy
Create a copy of the object.
Test if two
APyCFloat
objects are identical.Get the bit-representation of an
APyCFloat
.Constructor¶
- __init__(self, sign: bool | int, exp: int, man: int, exp_bits: int, man_bits: int, bias: int | None = None) None ¶
- __init__(self, sign: tuple[bool | int, bool | int], exp: tuple[int, int], man: tuple[int, int], exp_bits: int, man_bits: int, bias: int | None = None) None
Overloaded function.
__init__(self, sign: bool | int, exp: int, man: int, exp_bits: int, man_bits: int, bias: int | None = None) -> None
__init__(self, sign: tuple[bool | int, bool | int], exp: tuple[int, int], man: tuple[int, int], exp_bits: int, man_bits: int, bias: int | None = None) -> None
Create an
APyCFloat
object and initialize both real and imaginary parts.
- Parameters:
- sign
tuple
ofint
orbool
Sign of real/imaginary parts. True/non-zero equals negative.
- exp
tuple
ofint
Exponent of real/imaginary parts as stored, i.e., actual value + bias.
- man
tuple
ofint
Mantissa of the real/imaginary parts as stored, i.e., without a hidden one.
- exp_bits
int
Number of exponent bits.
- man_bits
int
Number of mantissa bits.
- bias
int
, optional Exponent bias. If not provided, bias is
2**exp_bits - 1
.
- sign
- Returns:
Creation from other types¶
- from_complex(value: object, exp_bits: int, man_bits: int, bias: int | None = None) APyCFloat ¶
Create an
APyCFloat
object from anint
,float
, orcomplex
.The initialize floating-point value is the one closest to value. Ties are rounded using
QuantizationMode.TIES_EVEN
.- Parameters:
- Returns:
See also
Examples
>>> import apytypes as apy >>> a = apy.APyCFloat.from_complex(1.375, exp_bits=10, man_bits=15) >>> a APyCFloat(sign=(0, 0), exp=(511, 0), man=(12288, 0), exp_bits=10, man_bits=15) >>> str(a) '(1.375+0j)'
- from_float(value: object, exp_bits: int, man_bits: int, bias: int | None = None) APyCFloat ¶
Create an
APyCFloat
object from anint
,float
, orcomplex
.The initialize floating-point value is the one closest to value. Ties are rounded using
QuantizationMode.TIES_EVEN
.- Parameters:
- valueint, float, complex
Value to initialize from.
- exp_bitsint
Number of exponent bits.
- man_bitsint
Number of mantissa bits.
- biasint, optional
Exponent bias. If not provided, bias is
2**exp_bits - 1
.
- Returns:
See also
Examples
>>> import apytypes as apy >>> a = apy.APyCFloat.from_float(1.25, exp_bits=10, man_bits=15) >>> a APyCFloat(sign=(0, 0), exp=(511, 0), man=(8192, 0), exp_bits=10, man_bits=15) >>> str(a) '(1.25+0j)'
- from_bits(bits: tuple, exp_bits: int, man_bits: int, bias: int | None = None) APyCFloat ¶
Create an
APyCFloat
object from a bit-representation. For convenience, leaving out the second item in the tuple initializes the imaginary part to 0.- Parameters:
- Returns:
See also
Examples
Create a complex-valued floating-point number a of value -5.75 + 2j from its bit pattern (real, imag).
>>> import apytypes as apy >>> >>> a = apy.APyCFloat.from_bits((791, 256), exp_bits=5, man_bits=4) >>> a APyCFloat(sign=(1, 0), exp=(17, 16), man=(7, 0), exp_bits=5, man_bits=4)
Create a floating-point number b of value 1.0 + 0j from its bit pattern. Initializing from (240,) is equivalent to initializing from (240, 0).
>>> b = apy.APyCFloat.from_bits((240, ), exp_bits=5, man_bits=4) >>> b APyCFloat(sign=(0, 0), exp=(15, 0), man=(0, 0), exp_bits=5, man_bits=4)
Change word length¶
- cast(self, exp_bits: int | None = None, man_bits: int | None = None, bias: int | None = None, quantization: QuantizationMode | None = None) APyCFloat ¶
Change format of the complex-valued floating-point number.
This is the primary method for performing quantization when dealing with APyTypes floating-point numbers.
- Parameters:
- exp_bits
int
, optional Number of exponent bits in the result.
- man_bits
int
, optional Number of mantissa bits in the result.
- bias
int
, optional Exponent bias. If not provided, bias is
2**exp_bits - 1
.- quantization
QuantizationMode
, optional. Quantization mode to use in this cast. If None, use the global quantization mode.
- exp_bits
- Returns:
Get bit representation¶
- to_bits(self) tuple[int, int] ¶
Get the bit-representation of an
APyCFloat
.See also
Examples
Create complex-valued floating-point number a of value -5.75 + 2j and show its bit pattern (real, imag).
>>> import apytypes as apy >>> >>> a = apy.fp(-5.75 + 2j, exp_bits=5, man_bits=4) >>> a.to_bits() (791, 256)
Comparison¶
Properties¶
Word length¶