gpt4 book ai didi

c - 左移 << 浮点变量

转载 作者:太空宇宙 更新时间:2023-11-04 00:12:57 29 4
gpt4 key购买 nike

我想将 num 与 scale 参数相乘。这里 num 和 scale 是 float 变量。我打算使用左移 << 的二元运算符。PFB 为了优化目的我想修改的逻辑。这是代码的一部分。修改后会提供一些性能。

我的硬件乘法运算非常昂贵。我的代码通过高度*宽度时间的图像进行乘法运算。所以我想通过移位运算符避免这种情况

typedef float float32_t;
float32_t scale = 64.0 ;
float32_t num = 1.5f ;
num = num *scale ;

我想修改成:

//float32_t scale = 64.0 ; //Left shift for performance benefits
float32_t num = 1.5f ;
num = num << 6 ;

但我遇到以下错误:

error: invalid operands of types 'float32_t {aka float}' and 'float32_t {aka float}' to binary 'operator<<' float32_t num = num << 6  ;

有一种方法可以修改函数本身的参数类型。但我不想为此更改修改函数的原型(prototype)。谁能告诉我解决方案。

最佳答案

在 C 中 << 的操作数和 >>运算符必须是整数类型。

C11:6.5 表达式(p4):

Some operators (the unary operator ~, and the binary operators <<, >>, &, ^, and |, collectively described as bitwise operators) are required to have operands that have integer type.[...]

注意在

float32_t num = num *scale ;  

num未初始化且其值未指定,可能导致未定义的行为。

关于c - 左移 << 浮点变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29210174/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com