Thread: The 'nan' output.

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    2

    The 'nan' output.

    The following is a very simple code to generate the points in the graph of a function. However, I'm getting 'nan' as the output of 'ret' (marked in red).




    insert
    Code:
    #include<iostream>
    #include<math.h>
    
    
    using namespace std;
    
    
    
    
    float fun_1(float z)
    {
    
    
       int k;
    
    
       float ret1=0;
    
    
       for(k=1; k<=1000; k++)
         {
    
    
           int c, d;
           int pk=1;
           int factk=1;
    
    
           for(c=1; c<=k; c++)
              {
                 pk= pk*(3*c-2);
              }
    
    
    
    
            for(d=1; d<=3*k; d++)
              {
                 factk=factk*d;
              }
    
    
    
    
           ret1= ret1+((pk*pow(z,3*k))/(factk));
    
    
         }
    
    
    return(ret1);
    
    
    }
    
    
    
    
    float fun_2(float z)
    {
    
    
       int k;
    
    
       float ret2=0;
    
    
       for(k=1; k<=1000; k++)
         {
    
    
           int c, d;
           int qk=1;
           int factk=1;
    
    
           for(c=1; c<=k; c++)
              {
                 qk= qk*(3*c-1);
              }
    
    
    
    
            for(d=1; d<=3*k+1; d++)
              {
                 factk=factk*d;
              }
    
    
    
    
           ret2= ret2+((qk*pow(z,(3*k+1)))/(factk));
    
    
         }
    
    
    return(ret2);
    
    
    }
    
    
    
    
    
    
    
    
    
    
    float fun_main(float z, float c1, float c2)
    {
    
    
      float ret1, ret2, ret;
      ret1=fun_1(z);
      ret2=fun_2(z);
      ret= c1*(1+ret1)-c2*(z+ret2);
    
    
      return(ret);
    
    
    }
    
    
    
    
    int main()
    
    
    {
    
    
      float c1= 0.355028053887817;
      float c2= 0.258819403792807;
    
    
      float z;
    
    
    
    
      float fun_main(float z, float c1, float c2);
      float fun_1(float z);
      float fun_2(float z);
    
    
      for(z=-10; z<=10;)
    
    
      {
        float ret;
        ret= fun_main(z, c1, c2);
    
    
        cout<<z<<" "<<" "<<ret<<endl;
    
    
        z=z+0.01;
      }
    
    
    return 0;
    }

    Kindly help me sort this problem out.

    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use a debugger
    Code:
    $ g++ -Wall -g bar.cpp
    $ gdb -q ./a.out 
    Reading symbols from /home/sc/Documents/a.out...done.
    (gdb) b fun_main
    Breakpoint 1 at 0x400a89: file bar.cpp, line 53.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    
    Breakpoint 1, fun_main (z=-10, c1=0.355028063, c2=0.258819401) at bar.cpp:53
    53        ret1=fun_1(z);
    (gdb) n
    54        ret2=fun_2(z);
    (gdb) p ret1
    $1 = -nan(0x400000)
    (gdb) n
    55        ret= c1*(1+ret1)-c2*(z+ret2);
    (gdb) p ret2
    $2 = -nan(0x400000)
    OK, so now we know where all the nan's are coming from.

    So delete the first breakpoint and set one at the start of fun_1 instead, and let's analyse it's behaviour.
    Code:
    (gdb) delete 1
    (gdb) b fun_1
    Breakpoint 2 at 0x400881: file bar.cpp, line 9.
    (gdb) c
    Continuing.
    -10  -nan
    
    Breakpoint 2, fun_1 (z=-9.98999977) at bar.cpp:9
    9         float ret1=0;
    (gdb) list
    4       using namespace std;
    5
    6       float fun_1(float z)
    7       {
    8         int k;
    9         float ret1=0;
    10        for(k=1; k<=1000; k++)
    11        {
    12          int c, d;
    13          int pk=1;
    (gdb) 
    14          int factk=1;
    15          for(c=1; c<=k; c++)
    16          {
    17            pk= pk*(3*c-2);
    18          }
    19          for(d=1; d<=3*k; d++)
    20          {
    21            factk=factk*d;
    22          }
    23          ret1= ret1+((pk*pow(z,3*k))/(factk));
    (gdb) b 23
    Breakpoint 3 at 0x4008fc: file bar.cpp, line 23.
    (gdb) c
    Continuing.
    
    Breakpoint 3, fun_1 (z=-9.98999977) at bar.cpp:23
    23          ret1= ret1+((pk*pow(z,3*k))/(factk));
    (gdb) printf "%d %d %d\n", k, pk, factk
    1 1 6
    (gdb) c
    Continuing.
    
    Breakpoint 3, fun_1 (z=-9.98999977) at bar.cpp:23
    23          ret1= ret1+((pk*pow(z,3*k))/(factk));
    (gdb) printf "%d %d %d\n", k, pk, factk
    2 4 720
    (gdb) c
    Continuing.
    
    Breakpoint 3, fun_1 (z=-9.98999977) at bar.cpp:23
    23          ret1= ret1+((pk*pow(z,3*k))/(factk));
    (gdb) printf "%d %d %d\n", k, pk, factk
    3 28 362880
    (gdb) c
    Continuing.
    
    Breakpoint 3, fun_1 (z=-9.98999977) at bar.cpp:23
    23          ret1= ret1+((pk*pow(z,3*k))/(factk));
    (gdb) printf "%d %d %d\n", k, pk, factk
    4 280 479001600
    Do you see just how quickly factk is growing.
    It's only iteration 4 out of 1000 and it's already approaching the numeric limit for an integer.

    You have a serious numeric overflow issue (or possibly several).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    2
    Thanks for pointing this out. I should have checked this out by myself.

    Regards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-14-2016, 08:27 AM
  2. C++ overlapping output and adding extensions to output files
    By lordmorgul in forum Linux Programming
    Replies: 9
    Last Post: 05-11-2010, 08:26 AM
  3. How to edit output in struct and call for the output
    By andrewkho in forum C Programming
    Replies: 4
    Last Post: 03-16-2010, 10:28 PM
  4. terminal output not showing output properly
    By stanlvw in forum C Programming
    Replies: 13
    Last Post: 11-19-2007, 10:46 PM
  5. Replies: 3
    Last Post: 02-19-2003, 08:34 PM

Tags for this Thread