# fixed_div

Computes fixed-point division as x * scale / y with a full 512-bit intermediate product. The caller owns the units and scale: this function does not require decimal, WAD, or Q-format inputs. It is pure and deterministic despite remaining registered VOLATILE for compatibility. NULL input propagates to NULL. A zero y or a rounded quotient above UINT256 raises an error.

Canonical HTML: <https://determica.com/docs/functions/fixed_div>

- Kind: scalar function
- Category: [Fixed-point and AMM math](/docs/category/fixed-point-and-amm-math.md)
- Tags: Fixed Point Math, Deterministic, Offline, Metadata Defect:volatile Registration, Product Semantics:unsupported Rounding Parity, Types

Computes fixed-point division as x * scale / y with a full 512-bit intermediate product. The caller owns the units and scale: this function does not require decimal, WAD, or Q-format inputs. It is pure and deterministic despite remaining registered VOLATILE for compatibility. NULL input propagates to NULL. A zero y or a rounded quotient above UINT256 raises an error.

## Overload 1: fixed_div(UINT256, UINT256, UINT256)

```sql
fixed_div(UINT256, UINT256, UINT256)
```

Returns floor(x * scale / y). The 512-bit product cannot overflow; the final UINT256 quotient can.

- Kind: scalar
- Execution context: Offline
- Behavior: Transform
- Execution modes: Offline
- Input shape: Scalar Arguments
- Planning contract: Runtime
- Cardinality: One Per Input
- Determinism: Deterministic
- Return shape: Scalar
- Schema stability: Fixed
- Stability: Stable
- Side effects: None documented

### Inputs

- `x` (UINT256; required; positional) — UINT256 value expressed in caller-selected units.
- `y` (UINT256; required; positional) — Non-zero UINT256 divisor expressed in units compatible with x.
- `scale` (UINT256; required; positional) — UINT256 factor applied before division to select the result scale.

### Returns

Floor-scaled quotient

UINT256 floor(x * scale / y), in the caller-selected result scale; NULL when any argument is NULL.

- `result` (UINT256)

Floor-scaled quotient. UINT256 floor(x * scale / y), in the caller-selected result scale; NULL when any argument is NULL.

### Examples

_Local SQL · Local_

```sql
SELECT format_units(fixed_div(8000000000000000000000::UINT256, 3200000000000000000000::UINT256, 1000000000000000000::UINT256), 18) AS weth_amount;
-- => [{"weth_amount":"2.5"}]
```

## Overload 2: fixed_div(UINT256, UINT256, UINT256, TINYINT)

```sql
fixed_div(UINT256, UINT256, UINT256, TINYINT)
```

Rounds the exact 512-bit ratio x * scale / y according to rounding. Rounding modes: 0=floor, 1=ceil, 2=trunc, 3=expand, 4=half-up. Because UINT256 is unsigned, floor and trunc both round down, ceil and expand both round up, and half-up rounds to nearest with ties upward. Compatibility behavior for unsupported TINYINT values is parity-based: odd values round up and even values round down.

- Kind: scalar
- Execution context: Offline
- Behavior: Transform
- Execution modes: Offline
- Input shape: Scalar Arguments
- Planning contract: Runtime
- Cardinality: One Per Input
- Determinism: Deterministic
- Return shape: Scalar
- Schema stability: Fixed
- Stability: Stable
- Side effects: None documented

### Inputs

- `x` (UINT256; required; positional) — UINT256 value expressed in caller-selected units.
- `y` (UINT256; required; positional) — Non-zero UINT256 divisor expressed in units compatible with x.
- `scale` (UINT256; required; positional) — UINT256 factor applied before division to select the result scale.
- `rounding` (TINYINT; required; positional) — TINYINT 0=floor, 1=ceil, 2=trunc, 3=expand, or 4=half-up; unsupported values retain parity behavior.

### Returns

Rounded scaled quotient

UINT256 rounded x * scale / y in the caller-selected scale; NULL when any argument is NULL. Rounding up can overflow an otherwise representable floor quotient.

- `result` (UINT256)

Rounded scaled quotient. UINT256 rounded x * scale / y in the caller-selected scale; NULL when any argument is NULL. Rounding up can overflow an otherwise representable floor quotient.

### Examples

_Local SQL · Local_

```sql
SELECT fixed_div(1000000000000000000::UINT256, 6::UINT256, 1::UINT256, 4::TINYINT)::VARCHAR AS share_wad;
-- => [{"share_wad":"166666666666666667"}]
```

## Related functions

- [muldiv](/docs/functions/muldiv.md) — See Also
- [divmul](/docs/functions/divmul.md) — See Also
- [format_units](/docs/functions/format_units.md) — Usually After
