解释K&放这个code,R 2-1 [英] Explain this code in K&R 2-1

查看:103
本文介绍了解释K&放这个code,R 2-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确定各种浮点类型的范围。当我读到这个code:

I'm trying to determine range of the various floating-point types. When I read this code:

#include <stdio.h>

main()
{
    float fl, fltest, last;
    double dbl, dbltest, dblast;

    fl = 0.0;
    fltest = 0.0;
    while (fl == 0.0) {
        last = fltest;
        fltest = fltest + 1111e28;
        fl = (fl + fltest) - fltest;
    }
    printf("Maximum range of float variable: %e\n", last);

    dbl = 0.0;
    dbltest = 0.0;
    while (dbl == 0.0) {
        dblast = dbltest;
        dbltest = dbltest + 1111e297;
        dbl = (dbl + dbltest) - dbltest;
    }
    printf("Maximum range of double variable: %e\n", dblast);
    return 0;
}

我不明白为什么笔者加入 1111e28 fltest 变量?

推荐答案

,循环终止时, fltest 达到 +天道酬勤,因为在这一点上 FL =(FL + fltest) - fltest 变成 NaN的,这不等于 0.0 末页包含时加入 1111e28 价值产生 +天道酬勤等接近浮动

The loop terminates when fltest reaches +Inf, as at that point fl = (fl + fltest) - fltest becomes NaN, which is unequal to 0.0. last contains a value which when added to 1111e28 produces +Inf and so is close to the upper limit of float.

1111e28 选择达到 +天道酬勤快速合理;它也必须足够大,使得当加入到大的值的循环不断进步,即它至少作为最大和第二大的非无限值。

1111e28 is chosen to reach +Inf reasonably quickly; it also needs to be large enough that when added to large values the loop continues to progress i.e. it is at least as large as the gap between the largest and second-largest non-infinite float values.

这篇关于解释K&放这个code,R 2-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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