Skip to content

docs: explain NewFromFloatWithExponent rounding - #482

Open
ZXLyyds100 wants to merge 1 commit into
shopspring:masterfrom
ZXLyyds100:docs/explain-float-exponent-276
Open

ZXLyyds100 wants to merge 1 commit into
shopspring:masterfrom
ZXLyyds100:docs/explain-float-exponent-276

Conversation

@ZXLyyds100

Copy link
Copy Markdown

Closes #276.

Summary

Clarify what exp means in NewFromFloatWithExponent:

  • The result is rounded to the nearest multiple of 10^exp, with halfway values rounded away from zero.
  • Negative, zero, and positive exponents select fractional places, the nearest integer, and places to the left of the decimal point respectively.
  • Rounding uses the actual binary float64 value, which can differ from the decimal literal.

The GoDoc now shows 123.456 with exponents -2, 0, and 2, producing 123.46, 123, and 100. An executable example checks those outputs and the exact halfway inputs 150 and -150 at exponent 2.

This is a documentation clarification, not a runtime fix. The existing implementation already produces these results. Only the function comment and an example test change; the algorithm, dependencies, minimum Go version, and CI configuration are unchanged.

Validation

Locally on Go 1.25.4, macOS arm64:

  • Compared go doc NewFromFloatWithExponent before and after: the original comment only demonstrated a negative exponent; the updated comment explains all three cases.
  • Ran the five example inputs against the original implementation and confirmed the same results.
  • go test -run '^(TestNewFromFloatWithExponent|ExampleNewFromFloatWithExponent)$' -count=1 -v
  • go test -count=1 ./...
  • go test -race -count=1 ./...
  • go vet ./...
  • go build ./...
  • golangci-lint run --timeout=10m ./... (v2.14.0; zero issues)
  • gofmt and git diff --check

Other Go versions and operating systems were not run locally. Hosted CI and maintainer review are separate from these local results.

AI assistance: the patch and this text were prepared with OpenAI Codex. The changes received an agent self-review; no independent human review is claimed.

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.

More detailed explanation for NewFromFloatWithExponent

1 participant