Skip to content

Use float32 precision when scanning float32 values - #434

Open
agammann wants to merge 2 commits into
shopspring:masterfrom
agammann:fix-scan-float32-precision
Open

agammann wants to merge 2 commits into
shopspring:masterfrom
agammann:fix-scan-float32-precision

Conversation

@agammann

Copy link
Copy Markdown

Scanning a float32 currently widens it to float64 before conversion. As a result, Scan(float32(54.33)) produces 54.33000183105469, whereas NewFromFloat32 produces 54.33 from the same input. The extra digits describe the widened binary approximation rather than the shortest decimal representation at the source precision.

Use NewFromFloat32 in the existing float32 scan branch. Update the existing scanner expectation and add coverage for positive and negative fractions, zero, and the smallest and largest finite float32 values, checked against strconv.FormatFloat with a 32-bit precision argument.

Validation on Windows with Go 1.27.1:

  • The existing scanner case and four new subcases fail before the fix.
  • go test ./... passes.
  • go vet ./... passes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant