UnitInterval
- class cometa.common.unit_interval.UnitInterval(ptr)[source]
Bases:
objectRepresents a rational number as a ratio of two integers.
Unit intervals are used throughout Cardano to represent fractional values such as protocol parameters (e.g., treasury cut, pool pledge influence). They are serialized as Rational Numbers (CBOR Tag 30).
The value of a unit interval is the numerator divided by the denominator.
Example
>>> interval = UnitInterval(1, 4) # Represents 0.25 >>> interval.to_float() 0.25
- classmethod new(numerator, denominator)[source]
Creates a new unit interval from a numerator and denominator.
- Parameters:
numerator (int) – The numerator of the fraction.
denominator (int) – The denominator of the fraction (must not be zero).
- Returns:
A new UnitInterval instance.
- Raises:
CardanoError – If creation fails (e.g., zero denominator).
- Return type:
Example
>>> interval = UnitInterval.new(3, 4) # Represents 0.75
- classmethod from_float(value)[source]
Creates a unit interval from a floating-point value.
The float is converted to a rational approximation with appropriate numerator and denominator values.
- Parameters:
value (float) – The floating-point value to convert.
- Returns:
A new UnitInterval approximating the given value.
- Raises:
CardanoError – If creation fails.
- Return type:
Example
>>> interval = UnitInterval.from_float(0.25) >>> interval.numerator 1 >>> interval.denominator 4
- classmethod from_cbor(reader)[source]
Deserializes a UnitInterval from CBOR data.
- Parameters:
reader (CborReader) – A CborReader positioned at the unit interval data.
- Returns:
A new UnitInterval deserialized from the CBOR data.
- Raises:
CardanoError – If deserialization fails.
- Return type:
- property numerator: int
Returns the numerator of the unit interval.
- property denominator: int
Returns the denominator of the unit interval.
- to_float()[source]
Converts the unit interval to a floating-point value.
- Returns:
The decimal value of the fraction (numerator / denominator).
- Return type:
float
Example
>>> interval = UnitInterval.new(1, 4) >>> interval.to_float() 0.25
- to_cbor(writer)[source]
Serializes the unit interval to CBOR format.
- Parameters:
writer (CborWriter) – A CborWriter to write the serialized data to.
- Raises:
CardanoError – If serialization fails.
- Return type:
None
- to_cip116_json(writer)[source]
Converts this object to CIP-116 compliant JSON representation.
CIP-116 defines a standard JSON format for Cardano data structures.
- Parameters:
writer (JsonWriter) – A JsonWriter to write the serialized data to.
- Raises:
CardanoError – If conversion fails.
- Return type:
None