nautechsystems/nautilus_trader · error

{field} {value} is not exactly representable with quantity p

Error message

{field} {value} is not exactly representable with quantity precision {precision}

What it means

Domain Quantity values have fixed precision. After converting the decimal with Quantity::from_decimal_dp, the code verifies the round-trip is exact; this error fires when the provider value has more decimal places than the configured quantity precision and would be truncated or rounded.

Source

Thrown at crates/adapters/polymarket/src/execution/reconciliation.rs:285

fn validate_quantity_evidence(
    value: Decimal,
    precision: u8,
    field: &str,
    allow_zero: bool,
) -> anyhow::Result<()> {
    if allow_zero {
        anyhow::ensure!(
            value >= Decimal::ZERO,
            "{field} {value} must be non-negative"
        );
    } else {
        anyhow::ensure!(value > Decimal::ZERO, "{field} {value} must be positive");
    }

    let quantity = Quantity::from_decimal_dp(value, precision).with_context(|| {
        format!("failed to represent {field} {value} with quantity precision {precision}")
    })?;
    anyhow::ensure!(
        quantity.as_decimal() == value,
        "{field} {value} is not exactly representable with quantity precision {precision}",
    );
    Ok(())
}

fn validate_price_evidence(value: Decimal, precision: u8, field: &str) -> anyhow::Result<()> {
    anyhow::ensure!(
        value > Decimal::ZERO && value < Decimal::ONE,
        "{field} {value} must be greater than zero and less than one",
    );
    let price = Price::from_decimal_dp(value, precision).with_context(|| {
        format!("failed to represent {field} {value} with price precision {precision}")
    })?;
    anyhow::ensure!(
        price.as_decimal() == value,
        "{field} {value} is not exactly representable with price precision {precision}",
    );

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Check the instrument's configured quantity precision and raise it to cover the provider's decimals if the venue truly supports them.
  2. Verify the provider value against the market's size tick; sizes not matching the tick are suspect data.
  3. Round only with an explicit, logged policy after confirming the venue accepts the rounded value — never silently.
  4. Compare with the raw JSON string from the provider to confirm the extra decimals are real and not a parsing artifact.

Example fix

// before
let precision = instrument.precision.quantity;
validate_quantity_evidence(trade.size, precision, "size", false)?;
// after
let precision = instrument.precision.quantity;
if trade.size.fract().scale() > precision as u32 {
    warn!("provider size {} exceeds instrument quantity precision {}; skipping", trade.size, precision);
    return Ok(());
}
validate_quantity_evidence(trade.size, precision, "size", false)?;
Defensive patterns

Strategy: validation

Validate before calling

fn fits_precision(v: Decimal, precision: u8) -> bool {
    v.fract().scale() <= precision as u32
}

Type guard

fn exactly_representable_quantity(v: Decimal, precision: u8) -> Option<Quantity> {
    Quantity::from_decimal_dp(v, precision).ok().filter(|q| q.as_decimal() == v)
}

Try / catch

match validate_quantity_evidence(size, precision, "size", false) {
    Ok(()) => (),
    Err(e) if e.to_string().contains("representable") => warn!("size {} exceeds precision {}; needs review", size, precision),
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: validate_quantity_evidence (via validate_order_row_values or validate_trade_values) receives a Decimal like 12.3456789 while the instrument's quantity precision is, say, 2 — quantity.as_decimal() != value.

Common situations: Polymarket sizes with more decimals than the instrument's tick/precision config; instrument precision configured too coarsely in the venue config; provider changing the number of decimals in size fields.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/06fa8b16ee8e2db6. Report an issue: GitHub.