c++ - C2296:“|”:非法,左操作数的类型为“float”
问题描述
我试图以一种干净和快速的方式(通过逻辑)比较三个变量是否为 0,它们都是浮点数,IEEE 754。
float x;
float y;
float z;
if(((x | y | z) == 0.0f)) { ... }
但是,我不断得到:
main.cpp(15): error C2296: '|': illegal, left operand has type 'float'
main.cpp(15): error C2297: '|': illegal, right operand has type 'float'
main.cpp(18): error C2296: '|': illegal, left operand has type 'float'
main.cpp(18): error C2297: '|': illegal, right operand has type 'float'
我读过一些关于,
std::numeric_limits<T>::is_iec559
std::numeric_limits<float>::epsilon()
std::fabs()
和厄普西隆10^−7
解决方案
位运算符只为整数类型定义。Afloat
不是整数类型。
不要像这样进行微优化。写清楚你的意图,把优化策略留给编译器。
推荐阅读
- reactjs - React-leaflet 自定义组件 - 未传递上下文?
- python - 我如何在网络 x 上与我在 Twitter 上的关注者创建图表?
- c# - 如何在复杂的 JSON 对象中搜索 Cosmos DB
- symfony - 使用 Symfony 控制台在后台启动服务器时出现问题
- laravel - 通过ajax formData请求在laravel中上传.pdf文件时仅保存.temp扩展文件
- laravel - 没有收到特定老师发布的作业
- wso2 - WSO2 apimcli 环境在完全分布式环境中为 API 管理器添加并使用 wso2 身份服务器作为密钥管理器
- highcharts - 从轴 Hightcharts 设置刻度间隔一分钟
- snowflake-cloud-data-platform - 数据库中所有表的聚合
- r - 多次产生相同的输出