int16_t浮动转换怪事 [英] int16_t to float conversion weirdness

查看:629
本文介绍了int16_t浮动转换怪事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在跟这里发生了什么损失。我需要一个浮点转换为int16_t和背部。下面是语法:

I am at a loss with what is happening here. I need to convert a float to an int16_t and back. Here is the syntax:

int16_t val = (int16_t)round((float)0xFFFE/100 * angle);
//and back
float angle = ((float)100/0xFFFE * val;

当我使用的-0.093081初始角度值,将其转换回来。但是当我使用它182.241211转换回-17.764824?

When I use an initial angle value of -0.093081, it converts back. But when I use 182.241211 it converts back to -17.764824?

任何想法是怎么回事?

推荐答案

0xFFFE 几乎的最大的16位数字;和仅一个的无符号的16位数字,在那。如果除以100,然后由182乘,它肯定会溢出。

0xFFFE is almost the maximum 16-bit number; and only for an unsigned 16-bit number, at that. If you divide it by 100 and then multiply by 182, it's definitely going to overflow.

让我们完全做到在基地10清晰度(0xFFFE为65534):

Let's do it fully in base 10 for clarity (0xFFFE is 65534):

65534 / 100 *  -0.093081 =    -60.99970254
65534 / 100 * 182.241211 = 119429.95521674

您签署的16位整数的全系列几乎肯定[-32768,32767]。那最后的结果将不适合。

The full range of your signed 16-bit integer is almost certainly [-32768, 32767]. That last result won't fit.

这篇关于int16_t浮动转换怪事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆