This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] TMS320F28379D:将 float32截断为所需的小数位

Guru**** 2551640 points


请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/c2000-microcontrollers-group/c2000/f/c2000-microcontrollers-forum/1296279/tms320f28379d-truncating-float32-to-required-decimal-places

器件型号:TMS320F28379D

您好,专家,

我想将 float32变量截断为其小数点后的2个位、目前我正在执行以下操作、可否让我知道有没有更好的方法、 我想避免任何除法运算。

#include <math.h>
//Round to 2 decimal places

//Option 1:
float32_t currentMechTheta = 0.3278;
currentMechTheta = floorf(pMotor->ptrFCL->qep.MechTheta * 100) / 100;   //0.3200

//Option 2:
float32_t currentMechTheta = 0.3278;
currentMechTheta = roundf(pMotor->ptrFCL->qep.MechTheta * 100) / 100;   //0.3300

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、Arun、

    是的、这看起来对于截断是正确的。 由于浮点变量的存储方式与整数不同、因此这可能是处理变量中单个位的最佳方式。

    此致、

    阿米尔·奥马尔