作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在C中将 float 转换为定点格式fix16_14
。fix16_14
表示2位整数和14位小数?考虑一个例子:-0.99633 = c03c(十六进制)(二进制补码表示)。请帮我解决这个 C 代码逻辑。
最佳答案
转换是通过乘以float
来完成的。通过 16384.0。请务必对结果进行四舍五入。此外,由于只有 2 个整数位,因此该数字必须在 -2 <= x < 2
范围内。 。否则计算会溢出。
这里是示例代码:
#include <stdio.h>
#include <inttypes.h>
#include <math.h>
int main(void)
{
float x = -0.99633;
short int y = round(x * 16384.0);
printf("%#04hx\n", (unsigned short)y);
}
代码的输出是:0xc03c
关于c - 如何将 float 转换为fix16_14?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55809019/
我是一名优秀的程序员,十分优秀!