在C / C ++中,混合类型整数数学需要的最小类型转换是什么? [英] In C/C++, what's the minimum type up-casting required for mixed-type integer math?

查看:179
本文介绍了在C / C ++中,混合类型整数数学需要的最小类型转换是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个依赖于数据的代码,它是uint16_t,int32_t / uint32_t和int64_t值的混合。它还包括一些较大的位移位常数(例如,1 <23,甚至1 <33)。

在计算int64_t值时,如果我仔细转换每个子部分(例如,向上转换uint16_t值到int64_t)它的工作原理 - 如果我不,计算往往走错了。



代码如下:

  int64_t sensDT =(int64_t)sensD2-(int64_t)promV [PROM_C5] *(int64_t) 1 << 8); 
temperatureC =(double)((2000 + sensDT *(int64_t)promV [PROM_C6] /(1 <23))/ 100.0)

我想知道,如果我在这里的类型转换太混乱太慷ous。我不确定1<< 8要求投票(虽然没有一个,1 << 23不会导致错误的计算),但也许他们也。对于这样的计算,为了计算这样的值,有多少是多少?



编辑:所以很清楚,我问什么是最小适量铸造是正确的功能的必要条件(为了清楚起见,可以添加更多的铸件或修饰符,但从编译器的角度来看,为确保正确的计算是必要的)



Edit2:I使用C风格的cast,因为这是一个来自Arduino类型的嵌入式代码库(它本身使用的风格的casts)。

解决方案

一般来说,你可以使用现有的编码风格。依靠整数促销来给你正确的操作,只要每个操作符的操作数之一具有正确的大小, 。所以你的第一个例子可以简化:

  int64_t sensDT = sensD2-(int64_t)promV [PROM_C5] *(1 < 8); 

小心考虑优先规则,以了解运算符的应用顺序!



如果你混合相同大小的有符号和无符号类型,你可能会遇到麻烦,虽然应该提升到更大的签名类型。



你需要小心使用常量,因为没有任何装饰,这些将是默认的整数大小和签名。 1<< 8 不会是一个问题,但 1<< 35 你需要 1LL <35



如果有疑问,一些额外的演员或括号不会伤害。


I have code that depends on data that is a mixture of uint16_t, int32_t / uint32_t and int64_t values. It also includes some larger bit shifted constants (e.g., 1<<23, even 1<<33).

In calculation of a int64_t value, if I carefully cast each sub-part (e.g., up-casting uint16_t values to int64_t) it works - if I don't, the calculations often go awry.

I end up with code that looks like this:

int64_t sensDT = (int64_t)sensD2-(int64_t)promV[PROM_C5]*(int64_t)(1<<8);
temperatureC = (double)((2000+sensDT*(int64_t)promV[PROM_C6]/(1<<23))/100.0);

I wonder, though, if my sprinkling of type casts here is too cluttered and too generous. I'm not sure the 1<<8 requires the cast (while despite not having one, 1<<23 doesn't lead to erroneous calculations) but perhaps they do too. How much is too much when it comes to up-casting values for a calculation like this?

Edit: So it's clear, I'm asking what the minimum proper amount of casting is - what's necessary for correct functionality (one can add more casts or modifiers for clarity, but from the compiler's perspective what's necessary to ensure correct calculations?)

Edit2: I'm using C-style casts as this is from an Arduino-type embedded code base (which itself used that style of casts already). From the perspective of having the desired effect they appear to be equivalent, thus I used the existing coding style.

解决方案

Generally you can rely on the integer promotions to give you the correct operation, as long as one of the operands for each operator have the correct size. So your first example could be simplified:

int64_t sensDT = sensD2-(int64_t)promV[PROM_C5]*(1<<8);

Be careful to consider the precedence rules to know what order the operators will be applied!

You might run into trouble if you're mixing signed and unsigned types of the same size, although either should promote to a larger signed type.

You need to be careful with constants, because without any decoration those will be the default integer size and signed. 1<<8 won't be a problem, but 1<<35 probably will; you need 1LL<<35.

When in doubt, a few extra casts or parentheses won't hurt.

这篇关于在C / C ++中,混合类型整数数学需要的最小类型转换是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆