minato_codec_numeric (minato v0.18.6)

View Source

Codec for numeric.

Internal to minato. The supported surface is minato_codec.

Wire format

The binary representation is a header of four int16 fields followed by ndigits further int16 values:

int16  ndigits   how many base-10000 digits follow
int16  weight    base-10000 exponent of the first digit
int16  sign      0x0000 positive, 0x4000 negative,
                 0xC000 NaN, 0xD000 Infinity, 0xF000 -Infinity
int16  dscale    how many decimal digits are displayed after the point
int16  digits[]  each 0..9999, most significant first

The value is digits[0] * 10000^weight + digits[1] * 10000^(weight-1) + ..., signed, and displayed with exactly dscale digits after the decimal point. Zero is ndigits = 0 with a positive sign. The special values carry no digits.

Erlang representation

numeric decodes to a binary decimal string, never to a float: the point of the type is that it is exact, and float would throw that away. dscale is preserved as trailing zeros, so 123.4500 decodes to ~"123.4500" and not to ~"123.45".

The special values decode to the atoms nan, infinity and neg_infinity.

Encoding accepts a binary decimal string (with an optional exponent, as PostgreSQL itself accepts), an integer, a float, or one of the three atoms. A float is converted through its shortest round-tripping decimal form, so it encodes the value the float actually holds. Floats are accepted for convenience but never produced, because decoding one would defeat the type.

Negative zero normalises to positive zero, as it does on the server.

Summary

Functions

Decode Format wire bytes to a numeric value.

Encode a numeric value to Format wire bytes.

Functions

decode/4

-spec decode(numeric, binary(), minato_codec:format(), minato_codec:opts()) ->
                binary() | float() | nan | infinity | neg_infinity.

Decode Format wire bytes to a numeric value.

An exact decimal binary, unless numeric_format => float asks for a float. See minato_codec for why the exact form is the default.

encode/4

-spec encode(numeric, term(), minato_codec:format(), minato_codec:opts()) -> iodata().

Encode a numeric value to Format wire bytes.