为什么要在一个大的浮动上添加一个小浮动只是放下一个小浮动? [英] Why does adding a small float to a large float just drop the small one?
问题描述
float a = 3 //(gdb)p / fa = 3
float b = 299792458 //(gdb)p / fb = 299792448
然后
float sum = a + b //(gdb)p / f sum = 299792448
我想这与尾数转移有关。有人可以解释到底发生了什么事吗? 32位
32位浮点数只有24位精度。因此,一个浮动不能完全保持b - 它可以通过设置一些指数,然后尾数尽可能接近尽其最好的工作。
然后当你考虑b和a的浮点表示,并尝试添加它们,加法操作会将小数字a的尾数向下移动,以尝试匹配b的指数,直到数值(3)落在最后,因此,加法运算符最后将浮点数0加到b。
Say I have:
float a = 3 // (gdb) p/f a = 3
float b = 299792458 // (gdb) p/f b = 299792448
then
float sum = a + b // (gdb) p/f sum = 299792448
I think it has something to do with the mantissa shifting around. Can someone explain exactly what's going on? 32bit
32-bit floats only have 24 bits of precision. Thus, a float cannot hold b exactly - it does the best job it can by setting some exponent and then mantissa to get as close as possible.
When you then consider the floating point representation of b and a, and try and add them, the addition operation will shift the small number a's mantissa downwards as it tries to match b's exponent, to the point where the value (3) falls off the end and you're left with 0. Hence, the addition operator ends up adding floating point zero to b.
这篇关于为什么要在一个大的浮动上添加一个小浮动只是放下一个小浮动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!