简体   繁体   English

如何将浮点值转换为引擎下的整数

[英]How is floating-point value converted into integer under the hood

First of all, I am quite unaware of a low-level machine representation of data (so, please, be kind if I misinterpret/misunderstand some things - an advice/correction is always welcome though) 首先,我完全没有意识到数据的低级机器表示(所以,如果我误解/误解某些事情,请善意 - 尽管欢迎提出建议/更正)

All the data is, obviously, presented as sequences of 0's and 1's. 显然,所有数据都表示为0和1的序列。

Integer numbers are just plain bits of information that can be converted to any numeral system (from the binary one). 整数只是简单的信息位,可以转换为任何数字系统 (来自二进制数字 )。

Floating-point numbers are, however, represented as sign + exponent + fraction (let's speak in terms of IEEE 754 Floating Point Standard) which are indeed just the same old bits (0's and 1's) and that are indeed can be converted (differently, of course) to any numeral system. 然而,浮点数表示为sign + exponent + fraction (让我们说的是IEEE 754浮点标准),它们确实只是相同的旧位(0和1)并且确实可以转换(不同,当然)到任何数字系统。


How does it "magically" happen that when you do a simple casting operation (see the example below), you actually get the correct result?: 当你进行简单的施法操作(参见下面的例子)时,你是如何“神奇地”发生的,你实际上得到了正确的结果?:

double a = 5.12354e3; // 5123.54
int b = int(a); // 5123

What is the logic inside the computing machine that converts sign + exponent + fraction to sign + value ? 计算机内部将sign + exponent + fraction转换为sign + value的逻辑是什么? It does not seem to be just a "plain" cast (you had 4/8 bytes before - you get 4 bytes after), right? 它似乎不仅仅是一个“普通”的演员阵容(之前你有4/8字节 - 后面有4个字节),对吗?

PS: If I am just not getting a very basic, obvious thing, sorry. PS:如果我没有得到一个非常基本的,显而易见的事情,抱歉。 Anyway, please, explain. 无论如何,请解释。

It's a simple matter of inserting a convert double to integer instruction that every processor with floating point has. 将每个具有浮点的处理器插入转换双精度到整数指令是一件简单的事情。

double a = 5.1e3; // 5123.54
MOVFD #5123.54, A(SP)
int b = int(a); // 5123
CVTDL  A(SP), B(SP)

As far as the mechanics of that all you have to do is insert a 1 bit in front of the mantissa (floating points are normally stored with an implicit one); 至于所有你需要做的机制是在尾数前插入1位(浮点通常与隐式存储一起存储); bit shift by the exponent; 由指数移位; then correct for the sign. 然后纠正标志。

You already did it for us 5123.54 can be represented as 5.12354 * 10^3 right, we learned that in grade school, so if I want to convert that to fixed point I take 5.12354 and shift it left three times and chop off the fraction 5123.54 then chop off to the right of the decimal point, and get 5123. No magic that is how it happens in hardware 1.11011 * 2^3 I shift it left three times 1110.11 and chop off the fraction. 你已经为我们做了5123.54可以表示为5.12354 * 10 ^ 3对,我们在小学就读了,所以如果我想将它转换为定点我拿5.12354并将其左移三次并切掉分数5123.54然后切断到小数点的右边,并得到5123.没有魔法就是它在硬件中发生的情况1.11011 * 2 ^ 3我将它左移三次1110.11然后切掉分数。 giving 1110. 给1110。

The floating point formats will vary but are still sign and mantissa times 2 to the power something. 浮点格式会有所不同,但仍然是符号和尾数乘以2的幂。 the term is floating point but in the binary representation the point is at a known place just like with scientific notation but more strict, you have a 1.something for non-zero values and then the times 2 to the power something, so if the power is negative the answer is zero, simple if the power is positive then you figure out how many mantissa bits to chop off... 这个术语是浮点数,但是在二进制表示中,点就在一个已知的位置,就像科学记数法一样,但是更严格,你有一个非零值的东西,然后是权力的两倍,所以如果功率是负的,答案是零,如果功率是正的那么简单就可以找出要切断多少个尾数位...

So for example the above 1.11011 * 2^3 lets say the mantissa of my floating point format is 6 bits 111011 I know where the decimal point is and know the power of two so I am really not shifting left three I am in this case shifting right 2. giving 1110. just depends on how you want to look at it. 所以例如上面的1.11011 * 2 ^ 3让我说我的浮点格式的尾数是6位111011我知道小数点在哪里并且知道2的幂,所以我真的没有向左移动三我在这种情况下移位正确2.给出1110.只取决于你想看它的方式。

then there is the sign extension for negative numbers you simply tack those on the top of the mantissa. 然后有负数的符号扩展你只需要在尾数的顶部添加。

Again floating point formats vary in subtle ways, but at the end of the day all of the floating point math (as well as fixed point) is no different than what you learned in grade school, it is just simpler since it is base two not base ten... 浮点格式再次以微妙的方式变化,但在一天结束时,所有的浮点数学(以及定点)与您在小学时学到的没有什么不同,它只是更简单,因为它是基础二而不是基地十...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM