最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - How do I convert a `float` to a `_Float16`, or even initialize a `_Float16`? (Andor print with printf?) - Stack Overflow

programmeradmin5浏览0评论

I'm developing a library which uses _Float16s 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 _Float16s only support numbers down to 5.97e−8. How do I simply set it to 1?

I'm developing a library which uses _Float16s 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 _Float16s only support numbers down to 5.97e−8. How do I simply set it to 1?

Share Improve this question edited Apr 4 at 16:11 Peter Cordes 367k49 gold badges717 silver badges981 bronze badges asked Mar 21 at 2:10 Coarse RosinflowerCoarse Rosinflower 1471 silver badge11 bronze badges 30
  • 3 -Wpedantic is not a warning level and does not enable -Wall – 3CxEZiVlQ Commented Mar 21 at 2:28
  • 1 I will raise this as a bug in VSCode, since that's what this seems to be. This only a bug in the shown code. – 3CxEZiVlQ Commented Mar 21 at 2:30
  • 3 @Steve The assignments work well. The bug is in the using printf. – 3CxEZiVlQ Commented Mar 21 at 4:08
  • 1 Have you tried 1.0f16? en.cppreference/w/cpp/types/floating-point – BoP Commented Mar 21 at 8:17
  • 1 Somebody should define %hf as the way to print a _Float16. – Steve Summit Commented Mar 22 at 13:51
 |  Show 25 more comments

1 Answer 1

Reset to default 6

It 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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论