The format specifier %a for printf() in C
Asked Answered
P

3

29

I am reading a C language book, it said %f, %e, %g, %a were printf conversion specifications used for float and double data types. Currently I can understand %f, %e, %g completely.

When do I need use %a to print float and double type data ?

Can you please show me an example.

Privative answered 28/1, 2011 at 9:53 Comment(0)
R
48

The %a formatting specifier is new in C99. It prints the floating-point number in hexadecimal form. This is not something you would use to present numbers to users, but it's very handy for under-the-hood/technical use cases.

As an example, this code:

printf("pi=%a\n", 3.14);

prints:

pi=0x1.91eb86p+1

The excellent article linked in the comments explains that this should be read "1.91EB8616 * 21" (that is, the p is for power-of-two the floating-point number is raised to). In this case, "1.91EB8616" is "1.570000052452087410". Multiply this by the "21", and you get "3.14000010490417510".

Note that this also has the useful property of preserving all bits of precision, and presenting them in a robust way. For instance you could use this to serialize floating point numbers as text, and not have to worry about repeating/infinite decimals.

Also note that strtod() can convert floating point numbers in hexadecimal form back to actual numbers. Not 100% sure about sscanf() and friends, the documentation was not very clear and I've never used that.

Rubescent answered 28/1, 2011 at 9:56 Comment(1)
You can read more about hexadecimal floating-point constants here: exploringbinary.com/hexadecimal-floating-point-constants .Rhiana
K
8

As far as an example of why you would want to use the hex representation, you may want to use %a to precisely represent a floating point value being sent to another machine for processing.

We're using this currently for unit testing an embedded controller by sending data from a simulated plant model that is emulating sensors and actuators to the embedded processor via a UART where the embedded processor does it's control processing and returns feedback (again, float represented as %a) back to the plant model to close the loop.

Kipp answered 12/4, 2012 at 17:32 Comment(1)
Just a heads up on embedded system, at least with the Atmel SAME70 %a and %A don't workTrebuchet
S
2

When do I need use %a to print float and double type data ?

In addition to guaranteed exact representation, printing with %a is also much faster. When you are printing only a few numbers, that is not much of an issue, but if you are forced to format-print many floating-point numbers, this may well speed your work up. Switching to base 10 and handling rounding each take a not-insignificant amount of work.

Caveat: The I/O may take even more time than the formatting of characters, which may eat away at these benefits; and you might, instead, get away with just printing the raw bytes with no formatting.

Supplicatory answered 18/9, 2021 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.