DeFi

Rounding Error

A rounding error is the difference between the mathematically exact result and the integer-rounded result returned by smart contract math.

The formula has to choose a whole-number answer, and the direction of that choice can decide who gains or loses value.

Rounding Error Explained in Detail

A rounding error appears when exact math produces a fractional value but the contract must return an integer. Solidity integer division rounds down. Libraries may also expose explicit rounding-up or rounding-down modes.

The audit question is not whether rounding exists. It is whether the rounding direction matches the protocol's economic invariant.

Smart contract example

function sharesForDeposit(uint256 assets, uint256 totalShares, uint256 totalAssets) external pure returns (uint256) {
    return assets * totalShares / totalAssets;
}

This rounds down. Rounding down may be acceptable for shares minted to a depositor, but dangerous if the same direction is used to undercharge debt, collateral, or liquidation penalties.

Rounding Error in Auditing

Rounding direction decides who receives the leftover value. In a vault, the protocol may want deposits to round down shares and withdrawals to round up shares burned. In lending, the safe direction may be different for borrow, repay, liquidation, and interest accrual.

Rounding errors become more serious near boundaries: one-share vaults, low-liquidity pools, dust balances, liquidation thresholds, and repeated micro-actions.

Red flags in code

  • One helper is reused for flows that need opposite rounding directions.

  • Debt, collateral, or health factor math rounds in favor of the borrower at unsafe boundaries.

  • Mint and redeem paths are not symmetric.

  • Preview functions disagree with execution functions.

  • A user can repeat tiny actions to accumulate the rounded remainder.

How to test or review it

  • Check each formula's intended beneficiary before judging the rounding direction.

  • Test values that produce a remainder of 1 after division.

  • Compare deposit, mint, withdraw, redeem, borrow, repay, and liquidation paths separately.

  • Fuzz small values and boundary values with fuzz testing.

  • Review precision loss separately because it can create or amplify rounding errors.

Sources