I'm developing a library which uses _Float16
s for many of the constants to save space when passing them around. However, just testing, it seems that telling GCC to just "set it to 1" isn't working.
One would think the following code:
_Float16 f = 1.0;
would work, or at least:
_Float16 f = (_Float16)1.0;
if the two types can't be implicitly cast to each other. However, hovering in VSCode says that f
is now 2.1524e−41, and, even weirder, printing it with:
printf("%g", f);
says that it's now 7.58885e−320. All of this is contextualized with the fact that _Float16
s only support numbers down to 5.97e−8. How do I simply set it to 1?
I'm developing a library which uses _Float16
s for many of the constants to save space when passing them around. However, just testing, it seems that telling GCC to just "set it to 1" isn't working.
One would think the following code:
_Float16 f = 1.0;
would work, or at least:
_Float16 f = (_Float16)1.0;
if the two types can't be implicitly cast to each other. However, hovering in VSCode says that f
is now 2.1524e−41, and, even weirder, printing it with:
printf("%g", f);
says that it's now 7.58885e−320. All of this is contextualized with the fact that _Float16
s only support numbers down to 5.97e−8. How do I simply set it to 1?
1 Answer
Reset to default 6It looks like your problem is not that you can't initialize, or convert to, a _Float16
. It looks like your problem is simply verifying what value a _Float16
variable currently holds.
I just experimented with this code, under two different compilers (gcc and clang):
_Float16 f1 = 12.25;
_Float16 f2;
f2 = 1.125;
I have every reason to believe (details below) that f1
was properly set to 12.25, and f2
was properly set to 1.125.
I then tried
printf("%f\n", f1);
printf("%f\n", f2);
but this did not work. It printed 0.000000 for both. One compiler also warned me: "format specifies type 'double' but the argument has type '_Float16'
" — which, as we'll see, was exactly the problem.
I then tried
double d = f1;
printf("%f\n", d);
printf("%f\n", (double)f2);
And these both worked fine, printing 12.25 and 1.125.
As comments and another answer have both explained, there's a special rule for printf
and a few other functions that type float
is automatically promoted to double
, such that formats like %f
work properly with arguments of either type. But this rule does not apply to _Float16
, so you have to do the conversion yourself, explicitly.
It also sounds like there's something wrong with VSCode's hover-over functionality.
But you'll also want to figure out how to enable compiler warnings. As I mentioned, one compiler I tried gave me a perfectly informative warning without even asking. For the other, adding the compilation options -Wall
or -Wformat
caused it to do so. C can be a strict and unfiving language, but the compiler is willing to help you past some of the rough spots, but sometimes you have to ask first.
-Wpedantic
is not a warning level and does not enable-Wall
– 3CxEZiVlQ Commented Mar 21 at 2:28printf
. – 3CxEZiVlQ Commented Mar 21 at 4:081.0f16
? en.cppreference/w/cpp/types/floating-point – BoP Commented Mar 21 at 8:17%hf
as the way to print a_Float16
. – Steve Summit Commented Mar 22 at 13:51