Expand description
Represents a quantity with a non-negative value and specified precision.
Quantity is an immutable value type for representing trade sizes, order quantities,
and position amounts. It enforces non-negative values and provides fixed-point arithmetic
for deterministic calculations.
§Arithmetic behavior
Adding or subtracting two Quantity values requires matching effective fixed-point scales.
These operations panic on a scale mismatch.
Comparisons and hashes account for scale differences without rounding.
Without the defi feature, constructors restrict values to a single storage scale.
| Operation | Result | Notes |
|---|---|---|
Quantity + Quantity | Quantity | Precision is max of both operands. |
Quantity - Quantity | Quantity | Panics if result would be negative. |
Quantity * Quantity | Quantity | Precision is max of both operands. |
Quantity + Decimal | Decimal | |
Quantity - Decimal | Decimal | |
Quantity * Decimal | Decimal | |
Quantity / Decimal | Decimal | |
Quantity + f64 | f64 | |
Quantity - f64 | f64 | |
Quantity * f64 | f64 | |
Quantity / f64 | f64 |
Multiplication accepts mixed scales and truncates the result toward zero at the result scale.
It retains the maximum operand precision without rounding the raw result to that precision.
For example, 1.5 * 1.5 stores 2.25 at precision 1, so display and as_decimal return 2.2.
Reconstructing that raw result at precision 1 fails raw validation. Use
Quantity::checked_rescale to explicitly raise precision before raw reconstruction or
serialization. Alternatively, explicitly round the raw value to its declared precision.
Quantity::checked_floor_to_increment validates its operands while aligning scales, so
explicitly rescale to sufficient precision before flooring a result with finer raw digits.
Arrow encoding can preserve finer raw digits, but strict decoding rejects them.
§Immutability
Quantity is immutable. All arithmetic operations return new instances.
Structs§
- Quantity
- Represents a quantity with a non-negative value and specified precision.
Constants§
- QUANTITY_
MAX - The maximum valid quantity value that can be represented.
- QUANTITY_
MIN - The minimum valid quantity value that can be represented.
- QUANTITY_
UNDEF - The sentinel value for an unset or null quantity.
Statics§
- QUANTITY_
RAW_ MAX - The maximum raw quantity integer value.
Functions§
- check_
positive_ quantity - Checks if the quantity
valueis positive.