Skip to content

fix issues 411,Reason: NewFromFloat returns the shortest decimal for round-trip.#418

Open
8xmx8 wants to merge 1 commit into
shopspring:masterfrom
8xmx8:master
Open

fix issues 411,Reason: NewFromFloat returns the shortest decimal for round-trip.#418
8xmx8 wants to merge 1 commit into
shopspring:masterfrom
8xmx8:master

Conversation

@8xmx8

@8xmx8 8xmx8 commented Apr 13, 2026

Copy link
Copy Markdown

Perform an exact decimal expansion strictly based on the true binary floating-point value (rather than the shortest round-trip representation).,see:#411

…floating-point value (rather than the shortest round-trip representation).
@8xmx8 8xmx8 changed the title Perform an exact decimal expansion strictly based on the true binary … fix issues 411,Reason: NewFromFloat returns the shortest decimal for round-trip. Apr 13, 2026
@juanjoseluisgarcia

Copy link
Copy Markdown

why this PR has not been merged yet?

@mwoss mwoss left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening and PR and helping to fix #411.

It looks great, however, this changes the public contract of NewFromFloat / NewFromFloat32 from "shortest round-trip representation" to "exact binary value," which is a breaking behavioral change.

Examples:

On master:

NewFromFloat(0.1).String() # "0.1"
NewFromFloat(0.3).String() # "0.3"

On this branch:

NewFromFloat(0.1).String() # "0.1000000000000000055511151231257827021181583404541015625"
NewFromFloat(0.3).String() # "0.299999999999999988897769753748434595763683319091796875"

Anyone comparing .String() output, persisting, etc, these values will be affected. The "shortest" behavior is the long-standing documented contract and is what most callers may rely on.

So maybe we could fix that narrower case from #411? I meant exact expansion only when the float has no fractional part while preserving shortest round-trip for fractional values?

If we would like to fix both, I would implement new public method like NewFromFloatExact

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.

4 participants