Skip to content

Correct formatting for round_up rounding mode#834

Merged
skirpichev merged 4 commits intompmath:masterfrom
javierelpianista:correct_round_
Aug 10, 2024
Merged

Correct formatting for round_up rounding mode#834
skirpichev merged 4 commits intompmath:masterfrom
javierelpianista:correct_round_

Conversation

@javierelpianista
Copy link
Copy Markdown
Contributor

@javierelpianista javierelpianista commented Aug 4, 2024

This PR improves the code in round_digits: now the first digit in the stddigits list is excluded from the list of digits that must be rounded up when the round_up rounding mode is used. When rounding to a 0 digit, all the digits following it are looked up, and if one of them is different from 0, the 0 is rounded up.

This fixes some wrongfully formatted floats as, for example, when running the following code in the master branch

>>> from gmpy2 import mpfr
>>> from mpmath import mp
>>> print('{:.2Uf}'.format(mpfr('0.25')))
0.25
>>> print('{:.2Uf}'.format(mp.mpf('0.25')))
0.26
>>>

Copy link
Copy Markdown
Collaborator

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

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

You should include tests.

@skirpichev skirpichev self-requested a review August 4, 2024 15:30
@skirpichev skirpichev marked this pull request as draft August 5, 2024 01:30
* Made it so that when rounding up, and the digit rounded to is a 0, all of the following digits are checked.
@skirpichev skirpichev self-requested a review August 5, 2024 13:02
Copy link
Copy Markdown
Collaborator

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

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

Could you, please, adjust PR title and description?

@javierelpianista javierelpianista changed the title Corrected round_digits to always round 0 down. Correct formatting for round_up rounding mode Aug 6, 2024
@javierelpianista javierelpianista marked this pull request as ready for review August 6, 2024 12:45
@skirpichev skirpichev self-requested a review August 8, 2024 03:22
@skirpichev skirpichev merged commit cf98bcf into mpmath:master Aug 10, 2024
@javierelpianista javierelpianista deleted the correct_round_ branch August 10, 2024 12:02
@skirpichev skirpichev added this to the 1.4 milestone May 9, 2025
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.

2 participants