From 73bd33b21562f483c420c5f8389bba8a46dacb1f Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Mon, 21 Dec 2015 13:12:11 -0600 Subject: [PATCH] Do not encode Go float64 to a PostgreSQL float4 The automatic conversion of float64 to float32 could cause loss of precision. --- CHANGELOG.md | 1 + values.go | 14 +++----------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dad72ebf..88c0a74d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Tip +* Go float64 can no longer be encoded to a PostgreSQL float4 * Add ConnPool.Reset method * []byte skips encoding/decoding * Rows.Scan errors now include which argument caused error diff --git a/values.go b/values.go index d4c2b390..abde1d0c 100644 --- a/values.go +++ b/values.go @@ -895,17 +895,9 @@ func decodeFloat4(vr *ValueReader) float32 { } func encodeFloat4(w *WriteBuf, value interface{}) error { - var v float32 - switch value := value.(type) { - case float32: - v = float32(value) - case float64: - if value > math.MaxFloat32 { - return fmt.Errorf("%T %f is larger than max float32 %f", value, math.MaxFloat32) - } - v = float32(value) - default: - return fmt.Errorf("Expected float representable in float32, received %T %v", value, value) + v, ok := value.(float32) + if !ok { + return fmt.Errorf("Expected float32, received %T", value) } w.WriteInt32(4)