Thread: floating exception problems

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    3

    floating exception problems

    hello! I am fairly new to C and am trying to write a code that solves ODEs and graphs them. I seem to have run into a "floating exception". I have been using gdb but I don't think I fully understand what it is telling me/how to go about fixing it. Any help would be great!

    My code is below thanks!

    Code:
    #include <stdio.h> 
    #include <math.h>
    #include "cpgplot.h"
    
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    //DEFINE DIFFERENTIAL EQUATIONS
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    float dSdt(float B, float N, float I, float S)
    {
         return (-((B/N)*I)*S);
    }
    float dIdt(float y, float B, float N, float I, float S)
    {
         return ((B/N)*I*S)-y*I;
    }
    float dRdt(float y, float I)
    {
         return y*I;
    }
    
    int main (){
    
    //~~~~~~~~~~~~~~~~~~~~~
    //RUNGE KUTTA
    //~~~~~~~~~~~~~~~~~~~~~~
      
      int DAYS = 15;
      int i;
      float t[i];
      t[0]=0.;
      int n = 100;
    
    // step height h 
        float h = (float)DAYS/(float)n; 
     
    //rk   
        float Sk1,Ik1,Rk1;
        float Sk2,Ik2,Rk2;
        float Sk3,Ik3,Rk3;
        float Sk4,Ik4,Rk4;
    
    //define arrays i need to graph at end
        float S[n];
        float I[n];
        float R[n];
    
    //define and set parameters
      float u=0.02;
      float y=36.;
      float B=10.;
    
    const float N = 100;
    
    //set initial conditions
        S[0]=30.;
        I[0]=30.;
        R[0]=40.;
    
    // Apply Runge Kutta
        for (i=1; i<=n; i++) 
        { 
    
    //k1
            Sk1 = h*dSdt(N,B,S[i-1],I[i-1]); 
            Ik1 = h*dIdt(y,B,N,I[i-1],S[i-1]);
            Rk1 = h*dRdt(y,I[i-1]);
    
    //k2 
            Sk2 = h*dSdt(N,B,I[i-1] + 0.5*Ik1, S[i-1] + 0.5*Sk1); 
            Ik2 = h*dIdt(y,B,N,I[i-1]+ 0.5*Ik1,S[i-1] + 0.5*Sk1);
            Rk2 = h*dRdt(y,I[i-1]+ 0.5*Ik1);
    
    //k3 
            Sk3 = h*dSdt(N,B,S[i-1]+ 0.5*Sk2,I[i-1]+ 0.5*Ik2); 
            Ik3 = h*dIdt(y,B,N,I[i-1]+ 0.5*Ik2,S[i-1]+ 0.5*Sk2);
            Rk3 = h*dRdt(y,I[i-1]+ 0.5*Ik2);
    
    //k4
            Sk4 = h*dSdt(N,B,S[i-1]+ 0.5*Sk3,I[i-1]+ 0.5*Ik3);
            Ik4 = h*dIdt(y,B,N,I[i-1]+ 0.5*Ik3,S[i-1]+ 0.5*Sk3);
            Rk4 = h*dRdt(y,I[i-1]+ 0.5*Ik3);
             
    // Update next value of funtions
            S[i] = S[i-1] + (1.0/6.0)*(Sk1 + 2.*Sk2 + 2.*Sk3 + Sk4);
            I[i] = I[i-1] + (1.0/6.0)*(Ik1 + 2.*Ik2 + 2.*Ik3 + Ik4);
            R[i] = R[i-1] + (1.0/6.0)*(Rk1 + 2.*Rk2 + 2.*Rk3 + Rk4);
      
    // Update next value of t
            t[i] = t[i-1] + h; 
       } 
     
    //~~~~~~~~~~~~~~~~~~~
    //PLOT GRAPHS
    //~~~~~~~~~~~~~~~~~~~
    
    // Open a plot window
      if (!cpgopen("/XWINDOW")) return 1;
    //set up plot axes
      cpgenv(0.,1000.,0.,100.,0,1);
    // Label axes
      cpglab("Time (days)", "Fraction of people", "SIR Model");
    
      // Change plot colour to colour 4 (BLUE)
      cpgsci(4);
      // Plot S
      cpgline(n+1,t,S);
    
      //change colour of derivative
       cpgsci(7);
      // Plot I
      cpgline(n+1,t,I);
    
      //change colour of derivative
       cpgsci(5);
      // Plot R
      cpgline(n+1,t,R);
    
      cpgclos();
    
      return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You just need to answer this simple question:
    What do you think is the size of array "t"?

    If you had warnings turned on in your compiler you would see why I'm asking this.

    By the way, you should learn how to use more descriptive names for your variables.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    3
    Thanks for your answer! Sorry about any confusion with the variables, will defienitely keep that in mind for next time! So t should be of some size to do with n right? I was thinking [n+1] but it didn't seem to help (I also tried changing the array sizes of S,I and R to the same but I am still a little bit stuck...)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Having fixed your array sizes, the most likely cause of floating point exception is going to be division by zero.

    Your arrays quickly mushroom into infinities and nans.
    Code:
    $ gcc -Wall -Wextra -g foo.c
    foo.c: In function ‘main’:
    foo.c:48:9: warning: unused variable ‘u’ [-Wunused-variable]
       float u=0.02;
             ^
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) b 91
    Breakpoint 1 at 0x400ffd: file foo.c, line 91.
    (gdb) run
    Starting program: /home/sc/Documents/a.out 
    
    Breakpoint 1, main () at foo.c:91
    91	  return 0;
    (gdb) p t
    $1 = {0, 0.150000006, 0.300000012, 0.450000018, 0.600000024, 0.75, 0.899999976, 1.04999995, 1.19999993, 1.3499999, 1.49999988, 1.64999986, 1.79999983, 1.94999981, 2.0999999, 2.25, 2.4000001, 2.55000019, 
      2.70000029, 2.85000038, 3.00000048, 3.15000057, 3.30000067, 3.45000076, 3.60000086, 3.75000095, 3.90000105, 4.05000114, 4.20000124, 4.35000134, 4.50000143, 4.65000153, 4.80000162, 4.95000172, 5.10000181, 
      5.25000191, 5.400002, 5.5500021, 5.70000219, 5.85000229, 6.00000238, 6.15000248, 6.30000257, 6.45000267, 6.60000277, 6.75000286, 6.90000296, 7.05000305, 7.20000315, 7.35000324, 7.50000334, 7.65000343, 
      7.80000353, 7.95000362, 8.10000324, 8.25000286, 8.40000248, 8.5500021, 8.70000172, 8.85000134, 9.00000095, 9.15000057, 9.30000019, 9.44999981, 9.59999943, 9.74999905, 9.89999866, 10.0499983, 10.1999979, 
      10.3499975, 10.4999971, 10.6499968, 10.7999964, 10.949996, 11.0999956, 11.2499952, 11.3999949, 11.5499945, 11.6999941, 11.8499937, 11.9999933, 12.1499929, 12.2999926, 12.4499922, 12.5999918, 12.7499914, 
      12.899991, 13.0499907, 13.1999903, 13.3499899, 13.4999895, 13.6499891, 13.7999887, 13.9499884, 14.099988, 14.2499876, 14.3999872, 14.5499868, 14.6999865, 14.8499861, 14.9999857}
    (gdb) p S
    $2 = {30, 8.63095767e+10, inf, nan(0x400000) <repeats 98 times>}
    (gdb) p I
    $3 = {30, -863043072, -nan(0x400000) <repeats 99 times>}
    (gdb) p R
    $4 = {40, -52668.1797, -inf, -nan(0x400000) <repeats 98 times>}
    (gdb)
    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.

  5. #5
    Registered User
    Join Date
    Nov 2018
    Posts
    3
    I thought that might be happening! Thanks so much! I did some research and got some better initial conditions and the program runs!

  6. #6
    Registered User
    Join Date
    Nov 2018
    Posts
    1
    please fix the size of array always.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point exception
    By christakissgeo in forum C Programming
    Replies: 1
    Last Post: 03-18-2015, 12:55 PM
  2. Floating point exception?
    By JM1082 in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2011, 08:13 PM
  3. floating point exception?
    By snowball in forum C Programming
    Replies: 2
    Last Post: 03-04-2011, 10:45 AM
  4. Getting a floating point exception
    By SnertyStan in forum C Programming
    Replies: 13
    Last Post: 03-25-2008, 11:00 AM
  5. Floating point exception ????
    By wuzzo87 in forum C Programming
    Replies: 3
    Last Post: 04-25-2007, 02:38 AM

Tags for this Thread