FLOATING POINT ERROR: OVERFLOW

CAN SOMEONE BE KIND ENOUGH TO TELL ME WHY I HAVE A
FLOATING POINT ERROR: OVERFLOW
ABNORMAL PROGRAM TERMINATION
IN MY CODE?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<stdio.h>
#include <math.h>
#include<conio.h>
#define A 0
#define B 1
float F(float x0, float y0)
{
return (0.2*x0)+(y0*y0);
       }
float Rung4(float x0, float y0, float h)
    {
	float k1 = F(x0,y0);
	float k2 = F(x0+h/2,y0+k1/2);
	float k3 = F(x0+h/2,y0+k2/2);
	float k4 = F(x0+h/2,y0+k2/2);
	float y1 = y0 + ( k1 + 2*k2 + 2*k3 + k4)/6;
    /*	printf("\n\n  k1 = %.4lf  ",k1);
	printf("\n\n  k2 = %.4lf ",k2);
	printf("\n\n  k3 = %.4lf ",k3);
	printf("\n\n  k4 = %.4lf ",k4);
	printf("\n\n  y(%.4lf) = %.3lf ",x0+h,y1);*/

 return y1;
    }

float Maks(float *y, float *y2, float n)
{
   int i;
   float Maks=fabs(y[0]-y2[0]);
	for(i=1; i<n; i++)
	 if (fabs(y[i]-y2[2*i])>Maks)
      Maks= fabs(y[i]- y2[2*i]);
return Maks;
}

void Rk4()
{
  int n=10, i;
  float h=(float)(B-A)/n, *x, *y, *x1, *y2, esp=0.001;
    do
      {
	 x=new float [n+1];
	 y=new float [n+1];
	 x1=new float [n*2+1];
	 y2=new float [n*2+1];
	      for (i=0; i<n; i++)
	       {
		   y[0]=0.1;
		   x[n]=B;
		   x[i]=A+i*h;
		   y[i+1]=Rung4(x[i], y[i], h);
		   }
	 n=2*n;
	 h=(float)(B-A)/n;
	 for (i=0; i<n; i++)
	  {
	     y2[0]=0.1;
	     x1[n]=B;
	     x1[i]=A+i*h;
	     y2[i+1]=Rung4(x1[i], y2[i], h);
	     }
      }
  while ((Maks(y, y2, n/2)/15)>=esp);
  puts("Runge- Kutta 4th Order");
    for (i=0; i<n; i++)
printf("x[%d]=%.2f       y[%d]=%6f\n", i, x[i], i, y[i]);

}
int main ()
{
// clrscr ();

  Rk4();
  getch ();
  return 0;
}
DEBUG YOUR PROGRAM AND TELL US WHERE THE ERRORS OCCUR SO THAT WE CAN HELP YOU.

YOU CAN DO THAT SIMPLY BY PLACING A std::cout<<"Hello"<<std::endl; AFTER A LINE, AND THEN MOVING IT THROUGH THE PROGRAM TILL YOU FIND THE POINT WHERE THE PROGRAM CRASHES.

DON'T FORGET TO INCLUDE #include <iostream>

HAVE A NICE DAY!
^ +1
TheDestroyer,
Thanks, i can now locate my error.
You're welcome. Mark the article as solved if it's solved :)
rather than riddle your code with debug print statements (ie: putting cout lines everywhere), learn to use your debugger.

Breakpoints > cout lines.
@Disch:

I don't fully agree. When there's a lot of things happening, it gets annoying to debug every single part. [Granted, if "lots of things are happening" you probably should have divided up the work and checked module per module.]

Often, I use prints to generate some data and just watch until I see something fishy, or it crashes. For example, I'll use a "Starting phase X" and "Ending phase X" print around the major parts of the an algorithm. If it crashes (or an error message appears), it's easy to see where it happened by just checking the last "starting" message that doesn't have an "ending" message yet.

Similarly, I'll often print an "expected" value versus an "actual" value. (Or compare the two and print an error message if they aren't the same).

Prints are a rather elegant way of "top-level debugging", to be honest, if used sparsely. Having a constantly moving wall of text in front of your eyes is useless, but summarized data between two steps is definitely handy.

Cleaning up is often the most annoying part, which is why I try to avoid checks (or wrap them all into a single check function). Prints are easy to spot because of the colour of string literals (they don't appear in my code otherwise), or I simply search for printf(), as I generally don't use them. (Official output goes to files, intermediate notifications/progress logs go through cout).
Yeah, I agree in such cases as you describe print logging can be useful. I don't really think this particular problem qualifies as such an instance, though.
@Disch: When I started writing C++ programs, I didn't know anything about debugging. Debugging was intemidating enough for me that I couldn't even learn it, where it was a big deal to learn compared to the simple programs I was creating. So I "invented" this printing method for fast debugging. Definintely, the guy posting the question doesn't know what breakpoints mean or what the difference is between an opimised compiled program and a debug-version compiled program. So I posed this method as a simple solution to this for him to "stop" depending on others when he can solve his own problem with the least efforts. I'm pretty sure, if he had known this method, he wouldn't have posted his 35142135 lines code in the forum and asked people to fix his code for him.

@Gaminic: There's a very simple solution I follow for clean up. Simply, don't type cout<<... directly in your code; instead, you could define a macro with a name debug(str)... in some special form that could be found easily with your IDE's find function. How about that? ;-)
Last edited on
Topic archived. No new replies allowed.