muldiv

x * y / z with a 512-bit product: no overflow before division. UINT256 rounds down, INT256 truncates toward zero, unless a rounding mode is given. Errors when z is zero or result does not fit its type. NULL propagates. Native integer arguments fit both forms: cast them to UINT256 or INT256.

Scalar function TypesEVM

Example

Runs locally

SELECT muldiv(7::UINT256, 6::UINT256, 4::UINT256)::VARCHAR AS result;
-- => [{"result":"10"}]

API reference

muldiv(UINT256, UINT256, UINT256) #

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

Inputs

Name Type Use
x UINT256

UINT256, or INT256 for a signed result, in the 512-bit product.

required positional
y UINT256

Same type as x, in the 512-bit product.

required positional
z UINT256

Non-zero, same type as x.

required positional

Returns

Name Type
result UINT256

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

muldiv(UINT256, UINT256, UINT256, TINYINT) #

Computes (x * y) / z 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.

Inputs

Name Type Use
x UINT256

UINT256, or INT256 for a signed result, in the 512-bit product.

required positional
y UINT256

Same type as x, in the 512-bit product.

required positional
z UINT256

Non-zero, same type as x.

required positional
rounding TINYINT

TINYINT mode 0=floor, 1=ceil, 2=trunc, 3=expand, or 4=half-up.

required positional

Returns

Name Type
result UINT256

Rounded quotient. UINT256 rounded quotient; NULL when any input is NULL. Rounding can itself cause quotient overflow.

Overload examples

Runs locally

SELECT muldiv(5::UINT256, 1::UINT256, 2::UINT256, 4::TINYINT)::VARCHAR AS result;
-- => [{"result":"3"}]
muldiv(INT256, INT256, INT256) #

512-bit product of INT256 magnitudes over |z|, sign carried separately, truncated toward zero as INT256 `/`.

Inputs

Name Type Use
x INT256

UINT256, or INT256 for a signed result, in the 512-bit product.

required positional
y INT256

Same type as x, in the 512-bit product.

required positional
z INT256

Non-zero, same type as x.

required positional

Returns

Name Type
result INT256

Signed quotient. INT256 x * y / z truncated toward zero; NULL when any input is NULL. Quotient outside INT256 errors.

Overload examples

Runs locally

-- A signed difference in basis points with two decimals: (a - b) * 1e6 / b
SELECT format_units(muldiv((2400::INT256 - 2410::INT256), 1000000::INT256, 2410::INT256), 2) AS bps;
-- => [{"bps":"-41.49"}]
muldiv(INT256, INT256, INT256, TINYINT) #

INT256 x * y / z, exact, rounded by mode. Rounding modes: 0=floor (toward -inf), 1=ceil (toward +inf), 2=trunc (toward zero), 3=expand (away from zero), 4=half-up (nearest, ties toward +inf). Other modes error.

Inputs

Name Type Use
x INT256

UINT256, or INT256 for a signed result, in the 512-bit product.

required positional
y INT256

Same type as x, in the 512-bit product.

required positional
z INT256

Non-zero, same type as x.

required positional
rounding TINYINT

TINYINT mode 0=floor, 1=ceil, 2=trunc, 3=expand, or 4=half-up (ties toward positive infinity).

required positional

Returns

Name Type
result INT256

Rounded signed quotient. INT256 rounded quotient; NULL when any input is NULL. Quotient outside INT256 or unknown mode errors.

Overload examples

Runs locally

SELECT muldiv((-7)::INT256, 1::INT256, 2::INT256, 0::TINYINT)::VARCHAR AS floor_half, muldiv((-7)::INT256, 1::INT256, 2::INT256)::VARCHAR AS trunc_half;
-- => [{"floor_half":"-4","trunc_half":"-3"}]