int16 函数对值进行四舍五入和钳位。所以等价物看起来像这样
int16_t int16( double d )
{
return isnan(d) ? 0 : (d > 32767.0) ?
32767 :
(d <-32768.0) ? -32768 : (int16_t)round(d)) ;
}
编辑:int16 也为 NAN 输入返回 0,所以也要处理这个。
另请注意,代码确实需要区分大小写,因为 C 中 double 到 int16_t 的转换对于 NAN 和目标整数范围之外的值是未定义的。