Skip to content

Commit

Permalink
Issue #1020 Infinity numeric support (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
krokoziabla authored Aug 17, 2023
1 parent c26f179 commit debae2b
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions codecs/numeric.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ DEF MAX_DSCALE = 0x3FFF
DEF NUMERIC_POS = 0x0000
DEF NUMERIC_NEG = 0x4000
DEF NUMERIC_NAN = 0xC000
DEF NUMERIC_PINF = 0xD000
DEF NUMERIC_NINF = 0xF000

_Dec = decimal.Decimal

Expand Down Expand Up @@ -51,15 +53,22 @@ cdef numeric_encode_binary(CodecContext settings, WriteBuffer buf, obj):
dec = _Dec(obj)

dt = dec.as_tuple()
if dt.exponent == 'F':
raise ValueError('numeric type does not support infinite values')

if dt.exponent == 'n' or dt.exponent == 'N':
# NaN
sign = NUMERIC_NAN
num_pgdigits = 0
weight = 0
dscale = 0
elif dt.exponent == 'F':
# Infinity
if dt.sign:
sign = NUMERIC_NINF
else:
sign = NUMERIC_PINF
num_pgdigits = 0
weight = 0
dscale = 0
else:
exponent = dt.exponent
if exponent < 0 and -exponent > MAX_DSCALE:
Expand Down Expand Up @@ -160,6 +169,12 @@ cdef numeric_decode_binary_ex(
if sign == NUMERIC_NAN:
# Not-a-number
return _Dec('NaN')
elif sign == NUMERIC_PINF:
# +Infinity
return _Dec('Infinity')
elif sign == NUMERIC_NINF:
# -Infinity
return _Dec('-Infinity')

if num_pgdigits == 0:
# Zero
Expand Down

0 comments on commit debae2b

Please sign in to comment.