Skip to content

Fix significant figure rounding - #390

Open
Safari77 wants to merge 1 commit into
printfn:mainfrom
Safari77:sf_rounding
Open

Fix significant figure rounding#390
Safari77 wants to merge 1 commit into
printfn:mainfrom
Safari77:sf_rounding

Conversation

@Safari77

Copy link
Copy Markdown
Contributor

The issue is in the FormattedBigUint::Display implementation in core/src/num/biguint.rs. Currently, when formatting to a specific number of significant figures (sf_limit), the code truncates the string and replaces the remaining characters with '0'.

This causes 666 to drop down to 660 without checking if the truncated portions should instead cause the value to round up (which would correctly make it 670).

To fix this, we need to round the string based on the least significant kept digit before filling the rest with zeroes.

However, there's another structural problem: FormattedBigUint currently stores base: Option, meaning that when write_base_prefix is false, the base information is lost, which would break rounding for bases other than 10 (since we wouldn't know the correct threshold for "halfway" to round up).

We will change FormattedBigUint to always remember its Base.

Checklist

  • [X ] I used LLMs or similar AI tools when creating this pull request

The issue is in the FormattedBigUint::Display implementation in
core/src/num/biguint.rs. Currently, when formatting to a specific number
of significant figures (sf_limit), the code truncates the string and
replaces the remaining characters with '0'.

This causes 666 to drop down to 660 without checking if the truncated
portions should instead cause the value to round up (which would
correctly make it 670).

To fix this, we need to round the string based on the least significant
kept digit before filling the rest with zeroes.

However, there's another structural problem: FormattedBigUint currently
stores base: Option<Base>, meaning that when write_base_prefix is false,
the base information is lost, which would break rounding for bases other
than 10 (since we wouldn't know the correct threshold for "halfway" to
round up).

We will change FormattedBigUint to always remember its Base.
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