查找无法表示为IEEE-754 32位浮点数的最小整数 [英] Finding the smallest integer that can not be represented as an IEEE-754 32 bit float

查看:123
本文介绍了查找无法表示为IEEE-754 32位浮点数的最小整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:

首先,这是一个作业问题,只是为了立即解决这个问题.我当然不是在寻找汤匙喂食的解决方案,也许只是指向正确方向的一点指针.

Firstly, this IS a homework question, just to clear this up immediately. I'm not looking for a spoon fed solution of course, just maybe a little pointer to the right direction.

因此,我的任务是找到无法表示为IEEE-754浮点数(32位)的最小正整数.我知道对"5 == 5.00000000001"之类的相等性进行测试将失败,因此我认为我将简单地遍历所有数字并以这种方式对其进行测试:

So, my task is to find the smallest positive integer that can not be represented as an IEEE-754 float (32 bit). I know that testing for equality on something like "5 == 5.00000000001" will fail, so I thought I'd simply loop over all the numbers and test for that in this fashion:

int main(int argc, char **argv)
{
    unsigned int i; /* Loop counter. No need to inizialize here. */

    /* Header output */
    printf("IEEE floating point rounding failure detection\n\n");

    /* Main program processing */
    /* Loop over every integer number */
    for (i = 0;; ++i)
    {
        float result = (float)i;

        /* TODO: Break condition for integer wrapping */

        /* Test integer representation against the IEEE-754 representation */
        if (result != i)
            break; /* Break the loop here */
    }

    /* Result output */
    printf("The smallest integer that can not be precisely represented as IEEE-754"
           " is:\n\t%d", i);


    return 0;
}

此操作失败.然后,我尝试从浮点结果"中减去整数"i",即"i",希望获得可以尝试检测的"0.000000002",但同样也失败了.

This failed. Then I tried to subtract the integer "i" from the floating point "result" that is "i" hoping to achieve something of a "0.000000002" that I could try and detect, which failed, too.

有人可以指出我可以用来获得所需中断条件的浮点属性吗?

Can someone point me out a property of floating points that I can rely on to get the desired break condition?

--------------------更新如下---------------

-------------------- Update below ---------------

感谢您对此的帮助!我在这里学到了很多东西:

Thanks for help on this one! I learned multiple things here:

  1. 我最初的想法确实是正确的,并确定了要在其上运行的计算机(Solaris 10,32位)上的结果,但是在我的Linux系统(64位和32位)上无法运行.

  1. My original thought was indeed correct and determined the result on the machine it was intended to be run on (Solaris 10, 32 bit), yet failed to work on my Linux systems (64 bit and 32 bit).

Hans Passant添加的更改使该程序也可以在我的系统上使用,这里似乎发生了一些意料不到的平台差异,

The changes that Hans Passant added made the program also work with my systems, there seem to be some platform differences going on here that I didn't expect,

谢谢大家!

推荐答案

问题是您的相等性测试是浮点测试. i 变量将首先转换为float,当然会产生相同的float.将float转换回int以获得整数相等性测试:

The problem is that your equality test is a float point test. The i variable will be converted to float first and that of course produces the same float. Convert the float back to int to get an integer equality test:

float result = (float)i;
int truncated = (int)result;
if (truncated != i) break;

如果以数字16开头,那么您找到了正确的数字.将其转换为十六进制,并解释为什么那是一个未能获得等级奖金的原因.

If it starts with the digits 16 then you found the right one. Convert it to hex and explain why that was the one that failed for a grade bonus.

这篇关于查找无法表示为IEEE-754 32位浮点数的最小整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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