Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I printf a half-precision floating-point value?

I have a _Float16 half-precision variable named x in my C program, and would like to printf() it. Now, I can write: printf("%f", (double) x);, and this will work; but - can I printf x directly? Without the cast, but rather with an appropriate format specifier?

Looking for an answer regarding glibc mostly, but other C standard library implementations are also interesting.

Note: _Float16 is not automatically promoted to a double.

like image 658
einpoklum Avatar asked Sep 07 '25 15:09

einpoklum


1 Answers

can I printf x directly?

Not out of the box.

I do not think https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3184.pdf was merged.

looking for an answer regarding glibc mostly

Implement https://www.gnu.org/software/libc/manual/html_node/Customizing-Printf.html with strfromf16 .

Note that technically lowercase letters in printf format specifiers are reserved https://port70.net/~nsz/c/c11/n1570.html#7.31.11 . Consider uppercase letter, but no one cares. The C23 standard introduced upper case letters H D R K B, so I do not understand how it works.

Note: _Float16 is not promoted to double in variadic argument list.

like image 54
KamilCuk Avatar answered Sep 10 '25 14:09

KamilCuk