flexfloat.FlexFloat
- class flexfloat.FlexFloat(sign=False, exponent=None, fraction=None)[source]
A class to represent a floating-point number with growable exponent and fixed-size fraction. This class is designed to handle very large or very small numbers by adjusting the exponent dynamically. While keeping the mantissa (fraction) fixed in size.
This class follows the IEEE 754 double-precision floating-point format, but extends it to allow for a growable exponent and a fixed-size fraction.
- exponent
A growable bit array representing the exponent (uses off-set binary representation).
- Type:
- classmethod set_bitarray_implementation(implementation)[source]
Set the BitArray implementation to use for all FlexFloat instances.
- Parameters:
implementation (
Type[BitArray]
) – The BitArray implementation class to use.
- __init__(sign=False, exponent=None, fraction=None)[source]
Initializes a FlexFloat instance.
BitArrays are expected to be LSB-first (least significant bit at index 0, increasing to MSB).
- Parameters:
sign (
bool
, optional) – The sign of the number (True for negative, False for positive). Defaults to False.exponent (
BitArray | None
, optional) – The exponent bit array. If None, represents 0. Defaults to None.fraction (
BitArray | None
, optional) – The fraction bit array. If None, represents 0. Defaults to None.
- exponent: BitArray
A growable bit array representing the exponent (uses off-set binary representation).
- classmethod from_float(value)[source]
Creates a FlexFloat instance from a number.
- Parameters:
value (
Number
) – The number to convert to FlexFloat.- Returns:
A new FlexFloat instance representing the number.
- Return type:
- classmethod from_int(value)[source]
Creates a FlexFloat instance from an integer without overflow or underflow.
This method can handle arbitrarily large integers by dynamically growing the exponent size as needed. The fraction length can be customized to control precision.
- to_float()[source]
Converts the FlexFloat instance back to a 64-bit float.
If float is bigger than 64 bits, it will truncate the value to fit.
- Returns:
The floating-point number represented by the FlexFloat instance.
- Return type:
- Raises:
ValueError – If the FlexFloat does not have standard 64-bit exponent and fraction.
- to_int()[source]
Converts the FlexFloat instance to an integer.
The conversion truncates towards zero, similar to Python’s int(float) behavior. For example: int(3.7) = 3, int(-3.7) = -3.
- Returns:
The integer representation of the FlexFloat instance.
- Return type:
- Raises:
ArithmeticError – If the FlexFloat represents infinity or NaN.
- __float__()[source]
Converts the FlexFloat instance to a float.
This method is provided for compatibility with Python’s float type. It uses the to_float method to perform the conversion.
- Returns:
The floating-point representation of the FlexFloat instance.
- Return type:
- __int__()[source]
Converts the FlexFloat instance to an integer.
This method is provided for compatibility with Python’s int type. It uses the to_int method to perform the conversion.
- Returns:
The integer representation of the FlexFloat instance.
- Return type:
- __repr__()[source]
Returns a string representation of the FlexFloat instance.
- Returns:
A string representation of the FlexFloat instance.
- Return type:
- pretty()[source]
Returns an easier to read string representation of the FlexFloat instance. Mainly converts the exponent and fraction to integers for readability.
- Returns:
A pretty string representation of the FlexFloat instance.
- Return type:
- classmethod nan()[source]
Creates a FlexFloat instance representing NaN (Not a Number).
- Returns:
A new FlexFloat instance representing NaN.
- Return type:
- is_nan()[source]
Checks if the FlexFloat instance represents NaN (Not a Number).
- Returns:
True if the FlexFloat instance is NaN, False otherwise.
- Return type:
- is_infinity()[source]
Checks if the FlexFloat instance represents Infinity.
- Returns:
True if the FlexFloat instance is Infinity, False otherwise.
- Return type:
- is_zero()[source]
Checks if the FlexFloat instance represents zero.
- Returns:
True if the FlexFloat instance is zero, False otherwise.
- Return type:
- copy()[source]
Creates a copy of the FlexFloat instance.
- Returns:
A new FlexFloat instance with the same data as the original.
- Return type:
- __str__()[source]
Returns a float representation of the FlexFloat using a generic algorithm.
Currently, it only operates in one format: scientific notation with 5 decimal places.
- Returns:
The string representation in scientific notation.
- Return type:
- __neg__()[source]
Negates the FlexFloat instance.
- Returns:
A new FlexFloat instance with the sign flipped.
- Return type:
- __radd__(other)[source]
Right addition operator for FlexFloat instances.
This method allows the FlexFloat instance to be added to another FlexFloat or numeric type on the right side of the addition operator.
- Parameters:
other (
FlexFloat | Number
) – The other FlexFloat instance or numeric type to add.- Returns:
A new FlexFloat instance representing the sum.
- Return type:
- __rsub__(other)[source]
Right-hand subtraction for FlexFloat instances.
This method allows the FlexFloat instance to be subtracted from another FlexFloat or numeric type on the right side of the subtraction operator.
- Parameters:
other (
FlexFloat | Number
) – The other FlexFloat instance or numeric type to subtract from.- Returns:
A new FlexFloat instance representing the difference.
- Return type:
- __rmul__(other)[source]
Right-hand multiplication for Number types.
- Parameters:
other (
Number
) – The number to multiply with this FlexFloat.- Returns:
A new FlexFloat instance representing the product.
- Return type:
- __rtruediv__(other)[source]
Right-hand division for Number types.
- Parameters:
other (
Number
) – The number to divide by this FlexFloat.- Returns:
A new FlexFloat instance representing the quotient.
- Return type:
- __abs__()[source]
Returns the absolute value of the FlexFloat instance.
- Returns:
- A new FlexFloat instance with the same exponent and fraction, but
with the sign set to False (positive).
- Return type:
- abs()[source]
Calculates the absolute value of the FlexFloat instance.
- Returns:
- A new FlexFloat instance with the same exponent and fraction, but
with the sign set to False (positive).
- Return type:
- __rpow__(other)[source]
Right-hand power operation for Number types.
- Parameters:
other (
Number
) – The base to raise to the power of this FlexFloat.- Returns:
A new FlexFloat instance representing the power.
- Return type:
- __lt__(other)[source]
Check if this FlexFloat is less than another value.
- Parameters:
other (
FlexFloat | Number
) – The value to compare with.- Returns:
True if this FlexFloat is less than other, False otherwise.
- Return type:
- __le__(other)[source]
Check if this FlexFloat is less than or equal to another value.
- Parameters:
other (
FlexFloat | Number
) – The value to compare with.- Returns:
- True if this FlexFloat is less than or equal to other, False
otherwise.
- Return type: