int32_t 和 float 之间的数据保存保证?

Guarantees of data preservation between int32_t and float?

我正在阅读提供的演示板代码(特别是 DC21561A)并找到了这段代码:

int32_t min_current_threshold_code;                                                                                                                                                                                                          
min_current_threshold_code = (min_current_threshold / LTC2946_DELTA_SENSE_lsb) * resistor;                                                                                                                                                   
ack |= LTC2946_write_16_bits(LTC2946_I2C_ADDRESS, LTC2946_MIN_DELTA_SENSE_THRESHOLD_MSB_REG, (min_current_threshold_code << 4));

在这里,第一个赋值的 RHS 上的所有内容都是浮点数。据我所知,并且已经测试过,LHS int32_t 和 RHS 浮点数的赋值将被丢弃,浮点数的十进制位将被丢弃,只剩下整数;即“1.5 * 3.5 = 5”。

以上数据通过I2C写入寄存器。我假设浮点数用于更准确地估计阈值。但是,我想知道在将浮点数分配给 int32_t 时这种截断是否是 C(或 C++)标准或特定于编译器的要求?

编辑** 有些人要求提供更多代码。虽然我的问题已得到解答,但为了完整起见,下面是其余部分。

文件顶部有

float min_current_threshold = read_float();

const float LTC2946_DELTA_SENSE_lsb = 2.5006105E-05;

const float resistor = .02;

6.3.1.4 状态:

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined. 61)

然后有一个非规范的脚注解释上面的文字:

61) The remaindering operation performed when a value of integer type is converted to unsigned type need not be performed when a value of real floating type is converted to unsigned type. Thus, the range of portable real floating values is (−1, Utype_MAX+1).

是的,标准要求截断,例如在 C99 第 6.3.1.4 章第 1 段中:

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero).