what's this???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • questions

    what's this???

    Today when I build a program,the compiler tells me there is no
    error,but when I run it ,it shows " 1.#INF00000",wh at's the
    meaning??????Th e program is as follows:
    #include<stdio. h>
    int main()
    { int x,y;
    int w=0;
    long double z=0;

    for(x=0;x<=10;x ++)

    {if(x==0)

    {w=1;}

    else

    {for(y=1;y<=x;y ++)

    {w*=y;} }

    z+=(float)1/w;}



    printf("%.9Lf", z);


    return 0;}
  • Pawel Dziepak

    #2
    Re: what's this???

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    questions wrote:
    Today when I build a program,the compiler tells me there is no
    error,but when I run it ,it shows " 1.#INF00000",wh at's the
    meaning??????Th e program is as follows:
    #include<stdio. h>
    int main()
    { int x,y;
    int w=0;
    long double z=0;
    >
    for(x=0;x<=10;x ++)
    >
    {if(x==0)
    >
    {w=1;}
    >
    else
    >
    {for(y=1;y<=x;y ++)
    >
    {w*=y;} }
    >
    z+=(float)1/w;}
    >
    >
    >
    printf("%.9Lf", z);
    >
    >
    return 0;}
    It looks like "int" is too small to fit the value you assign to w. You
    should check which type is sufficient (limits.h). According to C++
    standard the largest integer type is unsigned long int and the largest
    floating point type is long double. In fact, the second one was enough
    to made this code working on my implementation.

    I'd suggest you to format your code in an easier to read way.

    Pawel Dziepak
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.9 (GNU/Linux)
    Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

    iEYEARECAAYFAkk VezgACgkQPFW+cU iIHNpFuACdFenI7 FQvVzOB5VcK1TWf QIy8
    uOsAnAsW8j1HozM c4oaxu5JAk4fkSf tA
    =pYqQ
    -----END PGP SIGNATURE-----

    Comment

    • Rolf Magnus

      #3
      Re: what's this???

      questions wrote:
      Today when I build a program,the compiler tells me there is no
      error,but when I run it ,it shows " 1.#INF00000",wh at's the
      meaning??????
      One question mark is enough. More makes the question just look silly.
      The meaning is probably that you are dividing by zero.
      The maximum value that w would reach probably a lot bigger than int
      can handle on your platform.

      Comment

      • maverik

        #4
        Re: what's this???

        #include<stdio. h>
        z+=(float)1/w;}
        printf("%.9Lf", z);
        Should you post to the c.l.c instead of c.l.c++
        what's the meaning??????
        Looks like w is overflowed. Use long types / float, double / ...

        Comment

        • want.to.be.professer

          #5
          Re: what's this???

          On 11ÔÂ8ÈÕ, ÏÂÎç5ʱ37·Ö, questions <mengqidhu...@y ahoo.cnwrote:
          Today when I build a program,the compiler tells me there is no
          error,but when I run it ,it shows " 1.#INF00000",wh at's the
          meaning??????Th e program is as follows:
          #include<stdio. h>
          int main()
          { int x,y;
          int w=0;
          long double z=0;
          >
          for(x=0;x<=10;x ++)
          >
          {if(x==0)
          >
          {w=1;}
          >
          else
          >
          {for(y=1;y<=x;y ++)
          >
          {w*=y;} }
          >
          z+=(float)1/w;}
          >
          printf("%.9Lf", z);
          >
          return 0;}
          overflow!!!
          You can see by this:

          #include<stdio. h>
          int main()
          {
          int x,y;
          int w=0;
          long double z=0;

          for(x=0;x<=10;x ++)
          {
          if(x==0)
          {
          w=1;
          }
          else
          {
          for(y=1;y<=x;y+ +)
          {
          w*=y;
          }
          }
          printf( " w : %d \n", w );
          z+=(float)1/w;
          printf("%.9Lf\n ",z);
          }
          printf("%.9Lf\n ",z);
          return 0;
          }

          Comment

          Working...