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.

Example

Local

1
SELECT format_units(fixed_div(8000000000000000000000::UINT256, 3200000000000000000000::UINT256, 1000000000000000000::UINT256), 18) AS weth_amount;
2
-- => [{"weth_amount":"2.5"}]
Notebook ready in readonly mode.

API reference

Exact signatures with descriptions, requirements, inputs, returns, and examples.

fixed_div(UINT256, UINT256, UINT256) #

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

Inputs

Name Type Use
x UINT256

UINT256 value expressed in caller-selected units.

required positional
y UINT256

Non-zero UINT256 divisor expressed in units compatible with x.

required positional
scale UINT256

UINT256 factor applied before division to select the result scale.

required positional

Returns

Name Type
result UINT256

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

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.

Inputs

Name Type Use
x UINT256

UINT256 value expressed in caller-selected units.

required positional
y UINT256

Non-zero UINT256 divisor expressed in units compatible with x.

required positional
scale UINT256

UINT256 factor applied before division to select the result scale.

required positional
rounding TINYINT

TINYINT 0=floor, 1=ceil, 2=trunc, 3=expand, or 4=half-up; unsupported values retain parity behavior.

required positional

Returns

Name Type
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.

Overload examples

Local SQL

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

Related functions

Category and tags

Tag
Offline
Tag
Types