# divmul

Computes floor(x * d / y), not an algebraic inverse of muldiv, with the product formed in 512 bits. Errors when y is zero or when the quotient does not fit UINT256. NULL input propagates to NULL.

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

- Kind: scalar function
- Category: [EVM utilities](/docs/category/evm-utilities.md)
- Tags: Types, EVM

Computes floor(x * d / y), not an algebraic inverse of muldiv, with the product formed in 512 bits. Errors when y is zero or when the quotient does not fit UINT256. NULL input propagates to NULL.

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

```sql
divmul(UINT256, UINT256, UINT256)
```

Forms the full 512-bit product x * d, divides it by y, and returns floor(x * d / y) as UINT256.

- 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 factor x in the 512-bit product.
- `y` (UINT256; required; positional) — Non-zero UINT256 divisor y.
- `d` (UINT256; required; positional) — UINT256 factor d in the 512-bit product.

### Returns

Floor quotient

UINT256 floor(x * d / y), or NULL when any input is NULL.

- `result` (UINT256)

Floor quotient. UINT256 floor(x * d / y), or NULL when any input is NULL.

### Examples

_Local SQL · Local_

```sql
SELECT divmul(7::UINT256, 4::UINT256, 10::UINT256)::VARCHAR AS result;
-- => [{"result":"17"}]
```

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

```sql
divmul(UINT256, UINT256, UINT256, TINYINT)
```

Computes (x * d) / y for UINT256 inputs using the supplied rounding mode. 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 factor x in the 512-bit product.
- `y` (UINT256; required; positional) — Non-zero UINT256 divisor y.
- `d` (UINT256; required; positional) — UINT256 factor d in the 512-bit product.
- `rounding` (TINYINT; required; positional) — TINYINT mode 0=floor, 1=ceil, 2=trunc, 3=expand, or 4=half-up.

### Returns

Rounded quotient

UINT256 rounded x * d / y; NULL when any input is NULL. Rounding can itself cause quotient overflow.

- `result` (UINT256)

Rounded quotient. UINT256 rounded x * d / y; NULL when any input is NULL. Rounding can itself cause quotient overflow.

### Examples

_Local SQL · Local_

```sql
SELECT divmul(1::UINT256, 2::UINT256, 1::UINT256, 1::TINYINT)::VARCHAR AS result;
-- => [{"result":"1"}]
```

## Related functions

- [fixed_mul](/docs/functions/fixed_mul.md) — See Also
- [fixed_div](/docs/functions/fixed_div.md) — See Also
- [try_mul](/docs/functions/try_mul.md) — See Also
- [evm_ceil_div](/docs/functions/evm_ceil_div.md) — See Also
