How is the standarized way to calculate float with integers?

后端 未结 3 1038
日久生厌
日久生厌 2021-01-24 12:14

Do any of you know how this will be calculated in C?

uint8_t samplerate = 200;
uint8_t Result;
Result = 0.5 * samplerate;

Now, the problem is t

3条回答
  •  爱一瞬间的悲伤
    2021-01-24 12:58

    Yes, of course this is controlled by the standard, there is no uncertainty here.

    Basically the integer will be promoted to double (since the type of 0.5 is double, it's not float) and the computation will happen there, then the result will be truncated back down to uint8_t. The compiler will shout at you for the loss of precision, typically. If it does not, add more warning options as required.

提交回复
热议问题