From 001b3166b9b675c48aae339a0d8d78f52a599056 Mon Sep 17 00:00:00 2001 From: Jim Tsao Date: Sun, 31 Oct 2021 13:32:23 +0100 Subject: [PATCH] Add infinity support for Numeric AssignTo --- numeric.go | 4 ++++ numeric_test.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/numeric.go b/numeric.go index 3f2dc9ae..72e59b69 100644 --- a/numeric.go +++ b/numeric.go @@ -403,6 +403,10 @@ func (dst *Numeric) toBigInt() (*big.Int, error) { func (src *Numeric) toFloat64() (float64, error) { if src.NaN { return math.NaN(), nil + } else if src.InfinityModifier == Infinity { + return math.Inf(1), nil + } else if src.InfinityModifier == NegativeInfinity { + return math.Inf(-1), nil } buf := make([]byte, 0, 32) diff --git a/numeric_test.go b/numeric_test.go index f14cf960..ecd2d95e 100644 --- a/numeric_test.go +++ b/numeric_test.go @@ -287,6 +287,10 @@ func TestNumericAssignTo(t *testing.T) { {src: &pgtype.Numeric{Int: big.NewInt(1006), Exp: -2, Status: pgtype.Present}, dst: &f64, expected: float64(10.06)}, // https://github.com/jackc/pgtype/issues/27 {src: &pgtype.Numeric{Status: pgtype.Present, NaN: true}, dst: &f64, expected: math.NaN()}, {src: &pgtype.Numeric{Status: pgtype.Present, NaN: true}, dst: &f32, expected: float32(math.NaN())}, + {src: &pgtype.Numeric{Status: pgtype.Present, InfinityModifier: pgtype.Infinity}, dst: &f64, expected: math.Inf(1)}, + {src: &pgtype.Numeric{Status: pgtype.Present, InfinityModifier: pgtype.Infinity}, dst: &f32, expected: float32(math.Inf(1))}, + {src: &pgtype.Numeric{Status: pgtype.Present, InfinityModifier: pgtype.NegativeInfinity}, dst: &f64, expected: math.Inf(-1)}, + {src: &pgtype.Numeric{Status: pgtype.Present, InfinityModifier: pgtype.NegativeInfinity}, dst: &f32, expected: float32(math.Inf(-1))}, } for i, tt := range simpleTests {