Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format() should take into account rounding mode of the context? #503

Open
skirpichev opened this issue Aug 4, 2024 · 0 comments
Open

format() should take into account rounding mode of the context? #503

skirpichev opened this issue Aug 4, 2024 · 0 comments

Comments

@skirpichev
Copy link
Contributor

Reproducer:

>>> from gmpy2 import *
>>> format(mpfr(2.675), '.2f')
'2.67'
>>> get_context().round = RoundUp
>>> format(mpfr(2.675), '.2f')
'2.67'

I'm not sure whether it's a bug or a feature.

On one hand, changing rounding direction does affect C printf():

$ cat a.c
#include <fenv.h>
#include <stdio.h>
int main(void)
{
    double x = 2.675;
    fesetround(FE_TONEAREST);
    printf("%.2f\n", x);
    fesetround(FE_UPWARD);
    printf("%.2f\n", x);
    return 0;
}
$ gcc a.c -std=c11 -Wpedantic -lm && ./a.out
2.67
2.68

On another hand, current behaviour is consistent with CPython, which just does round to nearest (with ties to even), unless it's configured to use libc's printf (I'm not sure if this option is still available).

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

No branches or pull requests

1 participant