Skip to content

Commit

Permalink
Merge pull request #857 from jordanlewis/float32
Browse files Browse the repository at this point in the history
encode: use 64-bit parsing for float32s
  • Loading branch information
maddyblue authored May 3, 2019
2 parents 51e2106 + afdef43 commit bc6a3c0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 6 additions & 2 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1127,10 +1127,11 @@ func TestReadFloatPrecision(t *testing.T) {
db := openTestConn(t)
defer db.Close()

row := db.QueryRow("SELECT float4 '0.10000122', float8 '35.03554004971999'")
row := db.QueryRow("SELECT float4 '0.10000122', float8 '35.03554004971999', float4 '1.2'")
var float4val float32
var float8val float64
err := row.Scan(&float4val, &float8val)
var float4val2 float64
err := row.Scan(&float4val, &float8val, &float4val2)
if err != nil {
t.Fatal(err)
}
Expand All @@ -1140,6 +1141,9 @@ func TestReadFloatPrecision(t *testing.T) {
if float8val != float64(35.03554004971999) {
t.Errorf("Expected float8 fidelity to be maintained; got no match")
}
if float4val2 != float64(1.2) {
t.Errorf("Expected float4 fidelity into a float64 to be maintained; got no match")
}
}

func TestXactMultiStmt(t *testing.T) {
Expand Down
9 changes: 4 additions & 5 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,10 @@ func textDecode(parameterStatus *parameterStatus, s []byte, typ oid.Oid) interfa
}
return i
case oid.T_float4, oid.T_float8:
bits := 64
if typ == oid.T_float4 {
bits = 32
}
f, err := strconv.ParseFloat(string(s), bits)
// We always use 64 bit parsing, regardless of whether the input text is for
// a float4 or float8, because clients expect float64s for all float datatypes
// and returning a 32-bit parsed float64 produces lossy results.
f, err := strconv.ParseFloat(string(s), 64)
if err != nil {
errorf("%s", err)
}
Expand Down

0 comments on commit bc6a3c0

Please sign in to comment.