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
I/O module: Meaning of %.6
varies depending on what type it is applied to
#18497
Comments
This is the kind of question that makes me want to ask "What do C and Python do?" (where I suspect that C has it easier because AFAIK, it doesn't support type-neutral format specifiers like Chapel's |
From some brief experimentation, it looks like Python:
>>> x = 3.2637
>>> y = 2
>>> print(f'{x:.3f}')
3.264
>>> print(f'{y:.3f}')
2.000
>>> z = 4.1
>>> print(f'{z:.3f}')
4.100
>>> y = 2
>>> print(f'{y:.3d}')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Precision not allowed in integer format specifier
>>> print(f'{y:.3n}')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Precision not allowed in integer format specifier
>>> x = 3.2637
>>> z = 4.1
>>> print(f'{z:.3n}')
4.1
>>> print(f'{x:.3n}')
3.26 |
lydia-duncan
changed the title
[Library Stabilization] Meaning of
I/O module: Meaning of Mar 23, 2022
%.6
varies depending on what type it is applied to%.6
varies depending on what type it is applied to
#include <stdio.h>
int main() {
printf("%.3i\n", 2); // prints `002`
printf("%.3f\n", 3.2637); // prints `3.264`
printf("%.3f\n", 4.1); // prints `4.100`
}
|
In our ad-hoc sub-team meeting today, we decided to:
|
2 tasks
jeremiah-corrado
added a commit
that referenced
this issue
Aug 9, 2023
Makes a couple of changes to handling of precision arguments in formatted IO per discussion here: #18497 (comment) - `%i` and `%u` both emit a warning when a precision argument is provided. This is not treated as a deprecation because the spec (to my knowledge) doesn't indicate that it is a supported feature - `%r` and `%n` both include precision digits for integer values. E.g., `writef("%.5r", 1)` prints `1.00000`. This is a bug fix. Note: The `compopts` files for a few formatted IO C-tests are also updated to include `-lm`. This flag ensures that the C standard libraries math header gets linked with the program, as `floorf` is now used in `qio_formatted.c`. Testing: - [x] local paratest - [x] gasnet paratest [ reviewed by @lydia-duncan ] - thanks!
Resolved by #22924 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The number after the decimal point in formatted string specification varies depending on the type of the argument it is applied to. For integers, it means "insert a decimal point and pad to the specified number of zeroes", while for reals, we only print out the number of decimals that were already provided. This is especially confusing when the format string written doesn't appear to have changed otherwise, such as when using the generic
n
to indicate the type required:Such differences are reflected when explicitly specifying the type:
While it may make sense in an individual type situation, in the larger picture it seems confusing and could cause problems when copying formatting lines for adjustment later in your program.
Should we change this behavior? How?
The text was updated successfully, but these errors were encountered: