# Multiplication after Division error leading to larger precision loss

GMX MarketUtils and PositionUtils divide before multiplying, causing precision loss in funding and PnL accounting that compounds across further multiplications.

- Payout: Contest reward
- Severity: medium
- Program: Gmx
- Credited: Breeje
- Disclosed: 2022-09-22
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2023-02-gmx-judging/issues/113
- Library page: https://bugbountylibrary.com/r/sherlock%3A2023-02-gmx%3AM-32
- Tags: domain:web3 impact:integrity kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:precision-loss

**Target:** Solidity smart contracts (MarketUtils, PositionUtils)
**Root cause:** Division is performed before subsequent multiplications (e.g. sizeOfLargerSide / FLOAT_PRECISION then multiplied), truncating precision that then propagates and amplifies through further multiplications.
**Steps:**
1. Read `MarketUtils.sol#L948`: `fundingUsd = (sizeOfLargerSide / FLOAT_PRECISION) * durationInSeconds * fundingFactorPerSecond`
2. Observe truncation from the early division by 10**30
3. Note `fundingUsd` is then multiplied again at L953-957 to derive per-collateral funding, amplifying error
4. Same pattern in `PositionUtils.sol#L217-224` for `positionPnlUsd`
5. Recommendation: multiply all numerators first, then divide by product of denominators (use unchecked/FullMath to avoid overflow)
**How found:** Code review of arithmetic ordering in funding and PnL calculations; spotted division preceding multiplication and traced how the truncated value is reused in further multiplications.
**Insight:** When auditing fixed-point math, trace whether a truncated intermediate is reused in later multiplications — error compounds. Overflow concerns cited as justification for divide-first can be addressed with unchecked blocks or FullMath/Math.mulDiv rather than sacrificing precision.
