HangOver

/*
总结:1.float的精度是6位有效数字,取值范围是10的-38次方到10的38次方,float占用4字节空间
      double的精度是15位有效数字,取值范围是10的-308次方到10的308次方,double占用8字节空间。
      浮点数相等判断;
      const float EPSINON = 0.00001;
      if ((x >= - EPSINON) && (x <= EPSINON)
      大于小于直接比较
      2.不要将数量级相差较大的两个浮点数相加,也不可将两个相近的浮点数相减
*/
#include <stdio.h>
#include <math.h>

int main()
{
    double ans[301] =
    {
        0,0.500000,0.833333,1.083333,1.283333,1.450000,1.592857,1.717857,1.828968,1.928968,2.019877,
        2.103211,2.180134,2.251562,2.318229,2.380729,2.439553,2.495108,2.547740,2.597740,2.645359,
        2.690813,2.734292,2.775958,2.815958,2.854420,2.891457,2.927171,2.961654,2.994987,3.027245,
        3.058495,3.088798,3.118210,3.146781,3.174559,3.201586,3.227902,3.253543,3.278543,3.302933,
        3.326743,3.349999,3.372726,3.394948,3.416687,3.437964,3.458797,3.479205,3.499205,3.518813,
        3.538044,3.556912,3.575430,3.593612,3.611469,3.629013,3.646255,3.663204,3.679870,3.696264,
        3.712393,3.728266,3.743891,3.759276,3.774427,3.789352,3.804058,3.818551,3.832837,3.846921,
        3.860810,3.874509,3.888022,3.901356,3.914514,3.927501,3.940321,3.952979,3.965479,3.977825,
        3.990020,4.002068,4.013973,4.025738,4.037366,4.048860,4.060224,4.071459,4.082571,4.093560,
        4.104429,4.115182,4.125820,4.136346,4.146763,4.157072,4.167277,4.177378,4.187378,4.197279,
        4.207082,4.216791,4.226407,4.235930,4.245364,4.254710,4.263969,4.273144,4.282235,4.291244,
        4.300172,4.309022,4.317794,4.326489,4.335110,4.343657,4.352132,4.360535,4.368868,4.377133,
        4.385329,4.393460,4.401524,4.409524,4.417461,4.425335,4.433147,4.440899,4.448591,4.456225,
        4.463801,4.471319,4.478782,4.486190,4.493543,4.500842,4.508088,4.515282,4.522425,4.529517,
        4.536560,4.543553,4.550497,4.557394,4.564243,4.571046,4.577803,4.584514,4.591181,4.597803,
        4.604382,4.610918,4.617412,4.623863,4.630273,4.636643,4.642972,4.649261,4.655511,4.661722,
        4.667895,4.674030,4.680128,4.686188,4.692212,4.698201,4.704153,4.710070,4.715952,4.721800,
        4.727614,4.733395,4.739142,4.744856,4.750538,4.756188,4.761806,4.767392,4.772948,4.778473,
        4.783967,4.789432,4.794866,4.800272,4.805648,4.810996,4.816315,4.821606,4.826869,4.832105,
        4.837313,4.842494,4.847649,4.852777,4.857879,4.862955,4.868006,4.873031,4.878031,4.883006,
        4.887957,4.892883,4.897785,4.902663,4.907517,4.912348,4.917156,4.921940,4.926702,4.931442,
        4.936159,4.940853,4.945526,4.950177,4.954807,4.959415,4.964003,4.968569,4.973114,4.977639,
        4.982144,4.986628,4.991092,4.995537,4.999961,5.004367,5.008753,5.013119,5.017467,5.021796,
        5.026107,5.030399,5.034672,5.038927,5.043165,5.047384,5.051586,5.055770,5.059936,5.064086,
        5.068218,5.072333,5.076432,5.080513,5.084578,5.088627,5.092659,5.096675,5.100675,5.104659,
        5.108628,5.112580,5.116517,5.120439,5.124345,5.128236,5.132112,5.135973,5.139819,5.143651,
        5.147467,5.151270,5.155058,5.158831,5.162590,5.166336,5.170067,5.173785,5.177488,5.181178,
        5.184855,5.188518,5.192167,5.195804,5.199427,5.203037,5.206634,5.210218,5.213790,5.217349,
        5.220895,5.224428,5.227949,5.231458,5.234955,5.238439,5.241911,5.245371,5.248820,5.252256,
        5.255681,5.259094,5.262495,5.265885,5.269263,5.272630,5.275986,5.279331,5.282664,5.285986
    };
    int i;
    double j;
    while(scanf("%lf", &j), j)  //这里不应该是j-0.0 > 0.0001
    {
        int answer = 0;
        for (i = 1; i < 301; i++)
        {
            if (ans[i] >= j)    //不要将ans[i]与j做减法,double的精度高,
            {                   //用0.000001时远远不够的,直接用0比较
                answer = i;
                break;
            }
        }
        printf("%d card(s)\n", answer);
    }
    return 0;
}

  相近浮点数相减很容易出错:

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    float a =   0.00001;
    cout << setprecision(10);
    cout << a << endl;
    cout << a - 0.00001 << endl;
    if (a -     0.00001 <= 0)
    {
        cout << "less" << endl;
    }
    else
    {
        cout << "greater" << endl;
    }
    return 0;
}
/*
    9.999999747e-006
    -2.526212492e-013
    less
*/

  

posted @ 2011-09-14 21:10  SunnyDay2015  阅读(310)  评论(0编辑  收藏  举报