Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jackc/pgtype
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.14.1
Choose a base ref
...
head repository: jackc/pgtype
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.14.2
Choose a head ref
  • 4 commits
  • 3 files changed
  • 2 contributors

Commits on Feb 6, 2024

  1. Fix numeric to float64 conversion

    A previous optimization in 50933c0 had a logic bug when converting
    pgtype.Numeric to a float64:
    
    ```
    	if src.Exp == 1 {
    		return float64(src.Int.Int64()), nil
    	}
    ```
    
    An exponent of one means multiply by 10.
    
    #210
    jschaf authored and jackc committed Feb 6, 2024
    Copy the full SHA
    f620c0f View commit details
  2. Remove special case for exp == 1

    Generalize tests and make it exhaustive.
    jschaf authored and jackc committed Feb 6, 2024
    Copy the full SHA
    797890a View commit details
  3. Rename test case

    jschaf authored and jackc committed Feb 6, 2024
    Copy the full SHA
    d0df6f7 View commit details
  4. Release v1.14.2

    jackc committed Feb 6, 2024
    Copy the full SHA
    e9f25b5 View commit details
Showing with 31 additions and 1 deletion.
  1. +4 −0 CHANGELOG.md
  2. +1 −1 numeric.go
  3. +26 −0 numeric_test.go
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.14.2 (February 5, 2024)

* Fix numeric to float64 conversion (Joe Schafer)

# 1.14.1 (January 12, 2024)

* Backport fix numeric to string conversion for small negative values
2 changes: 1 addition & 1 deletion numeric.go
Original file line number Diff line number Diff line change
@@ -448,7 +448,7 @@ func (src *Numeric) toFloat64() (float64, error) {
return math.Inf(-1), nil
}

if src.Exp == 1 {
if src.Exp == 0 {
return float64(src.Int.Int64()), nil
}

26 changes: 26 additions & 0 deletions numeric_test.go
Original file line number Diff line number Diff line change
@@ -385,6 +385,7 @@ func TestNumericEncodeDecodeBinary(t *testing.T) {
123,
0.000012345,
1.00002345,
50.000000,
math.NaN(),
float32(math.NaN()),
math.Inf(1),
@@ -442,3 +443,28 @@ func TestNumericSmallNegativeValues(t *testing.T) {
t.Fatalf("expected %s, got %s", "-0.000123", s)
}
}

// https://github.com/jackc/pgtype/issues/210
func TestNumericFloat64Exhaustive(t *testing.T) {
for exp := -10; exp <= 10; exp++ {
for i := -100; i < 100; i++ {
n := pgtype.Numeric{
Int: big.NewInt(int64(i)),
Exp: int32(exp),
Status: pgtype.Present,
}

var got float64
err := n.AssignTo(&got)
if err != nil {
t.Fatal(err)
}

want := float64(i) * math.Pow10(exp)
delta := math.Abs(want * 0.0000000001)
if math.Abs(got-want) > delta {
t.Fatalf("expected %f from %de%d, got %f", want, i, exp, got)
}
}
}
}