Skip to content

printf: %a not supported (but partially implemented) #2776

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

Open
jfinkels opened this issue Dec 16, 2021 · 7 comments
Open

printf: %a not supported (but partially implemented) #2776

jfinkels opened this issue Dec 16, 2021 · 7 comments

Comments

@jfinkels
Copy link
Collaborator

$ printf "%a\n" 255
0xf.fp+4
$ ./target/debug/coreutils printf "%a\n" 255
printf: %a: invalid conversion specification

Here is the description of the floating-point conversions: https://www.gnu.org/software/libc/manual/html_node/Floating_002dPoint-Conversions.html

This seems to be called CninetyNineHexFloatf in the code:

but it seems to be only partially implemented and not exposed to the user:
// 'a', 'A', //c99 hex float implementation not yet complete

@stale
Copy link

stale bot commented Jan 13, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jan 13, 2023
@tertsdiepraam
Copy link
Member

tertsdiepraam commented Feb 7, 2024

There's been progress on this in #5128, but it's not quite correct yet. The current output of you example is

0x1.fe00000000000p+7

Looks like my implementation isn't quite right.

@jfinkels
Copy link
Collaborator Author

jfinkels commented Jan 6, 2025

Some observations from experimenting with printf %a for various integer values.

  • It looks like any rendered with %a must have 8, 9, a, b, c, d, e, or f on the left of the decimal point (hexadecimal point?). The digits 0 through 7 are unused. For example, printf "%a 31 is 0xf.8p+1 and printf %a 32 is 0x8p+2.
  • If no fractional digits are needed, there is no decimal point. For example, printf %a 18 is 0x9p+1.
  • The number of digits after the decimal point is the minimum possible, increasing as needed. For example, printf %a 257 is 0x8.08p+5 but printf %a 258 is 0x8.1p+5.

@jfinkels
Copy link
Collaborator Author

It looks like the function to update now is format_float_hexadecimal() in src/uucore/src/lib/features/format/num_format.rs. No idea what the right algorithm is though!

@drinkcat
Copy link
Contributor

Oh, sorry, I didn't notice this issue. I dug into this in #7364 as well. Behaviour is platform-dependent, and we can't get the same precision as x86(-64) by just using Rust's f64.

@jtracey
Copy link
Contributor

jtracey commented May 3, 2025

I think this was fixed in #7631?

@drinkcat
Copy link
Contributor

drinkcat commented May 3, 2025

Yeah, to the extend this can be "fixed". See #7641 for the documentation of the current behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants