左移 浮点型

发布于 2025-01-06 06:38:09 字数 165 浏览 2 评论 0原文

尝试执行此操作时出现编译器错误

float_val=float_val<<1;

它给出错误“错误 C2296: '<<' : 非法,左操作数的类型为“float””

v 不能左移浮点变量吗?为什么会这样呢?

get a complier error while trying to do

float_val=float_val<<1;

It gives out a error saying "error C2296: '<<' : illegal, left operand has type 'float '"

Can't v left shift float vars? Why is this so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

千紇 2025-01-13 06:38:09

你不能左移 float 变量,因为 (a) 你的 FPU 通常不会有一个桶形移位器暴露给你,所以物理上无法生成代码来做到这一点,以及 (b) 这意味着什么?底层的位表示由多个具有不同含义的字段组成,您真的希望这些位相互渗透吗?

如果您想将该变量中保存的数字乘以 2,则应该这样做。

如果您想将浮点重新解释为某种左移有意义的类型(例如,适当大的无符号整数类型),以应对像卡马克平方根这样的可怕的位黑客,那么您也可以这样做,但在现代硬件上,它是高度您不太可能真正需要:认真考虑是否有更好的方法来做您想做的事。

You can't left shift float variables, because (a) your FPU generally will not have a barrel shifter exposed to you so physically cannot generate code to do that, and (b) what would it even mean? The underlying bit representation consists of multiple fields with different meanings, do you really want those bits bleeding into each other?

If you want to multiply the number held in that variable by two, you should just do that instead.

If you want to reinterpret the float as some type that left shift makes sense on (e.g. a suitably large unsigned integer type) for some horrible bit hack like Carmack's square root, well, you can do that too, but on modern hardware it is highly unlikely that you really need to: seriously consider if there is a better way to do what you want.

时光瘦了 2025-01-13 06:38:09

如果您想快速将浮点数乘以或除以 2 的幂,请查看标准函数 ldexpf。显然有点晦涩:-)。

https://linux.die.net/man/3/ldexpf

Check out the standard function ldexpf if you want to quickly multiply or divide a float by a power of 2. A bit obscure obviously :-).

https://linux.die.net/man/3/ldexpf

掌心的温暖 2025-01-13 06:38:09

移动浮点数没有意义,因为它表示为符号位、指数和尾数的串联。由于移位操作是关于移位位,因此这意味着将位从尾数移位到指数和/或符号位。

Shifting floats makes no sense since it's represented as the concatenation of a sign bit, an exponent and a mantissa. Since shifting operation is about shifting bits, it would imply shifting bits from mantissa to exponent and / or to sign bit.

静赏你的温柔 2025-01-13 06:38:09

浮点数没有值表示级别的位,这就是为什么不能对其应用按位运算的原因。

有关详细信息,请参阅此答案

Floating point numbers don't have bits at the level of value-representation, which is why you can't apply bitwise operations to them.

See this answer for more information.

笑脸一如从前 2025-01-13 06:38:09

由于左移运算符被定义为乘以 2 的幂,因此它对于浮点类型非常有意义。但是,C 语言没有定义其用途,因此您必须使用 scalbn 函数或类似函数。

Since the left shift operator is defined as multiplication by a power of 2, it makes perfect sense for floating point types. However, the C language does not define its use, so instead you have to use the scalbn function or similar.

二手情话 2025-01-13 06:38:09

您不能左移 float 类型的对象。

C 规定按位移位运算符的操作数应为整数类型。

You cannot left shift objects of float type.

C says the operands of the bitwise shift operators shall have integer type.

客…行舟 2025-01-13 06:38:09

您必须首先将浮点数转换为其他东西。如:

float f = 128;
f = (float) (((int) f) << 1 );

而上式中,f应为256.0。

现在显然,如果您从 128.4 开始,这是有问题的,因为演员会放弃 0.4。您可能不想首先使用浮动。

You'd have to transform the float to something else first. Such as:

float f = 128;
f = (float) (((int) f) << 1 );

And in the above, f should be 256.0.

Now obviously this is problematic if you start with 128.4 as the cast will drop the .4. You may not want to be using a float in first place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文