pow( , ) not working unless i step over the whole thing

I'm trying to write a code that calculates Newton's method, not an assignment, just for myself.
I'm having trouble with the function I put together to estimate values that will bring the polynomial close to 0.
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
int estimate(float fx[][2], int s)
{

	int i=0;
	float v1, v2=0, est1=0, est2=0;
	
	do{
	
		v1=0;
		est1=rand()%5;
		for(i=0;i<s;i++)
			v1+=(fx[i][0])*(pow(est1, fx[i][1]));
	
	}while((v1>5)||(v1<0));
	
	do{
		v2=0;
		est2=rand()%5;
		for(i=0;i<s;i++)
			v2+=(fx[i][0])*(pow(est2, fx[i][1]));
		if((v2>-5)&&(v2<0))
			break;

	}while(1);
	
	printf("est1=%.0f v1=%.0f\nest2=%.0f v2=%.0f", est1, v1, est2, v2);
	return 0;
}


when I step into it while debugging, it gets stuck on v1+=(fx[i][0])*(pow(est1, fx[i][1])); and sends me to the math.h library.
However, when I debug it and step OVER the entire sequence, the the code goes on as expected and delivers proper results.
If I try to run without debugging, the output window stops at the point where the function is called, not even ending, just getting stuck.
Any ideas?

Also, I'm not really sure what you'd need to know to help, so if you want to see the whole thing, I'll post it, or explain the variables or whatever:)
Only thing I can think of that would cause that is some kind of memory corruption.

When calling this function, are you sure 'fx' is large enough (has 's' elements)?
are you sure 'fx' is large enough (has 's' elements)?

yes because I use it in other parts of the code with the same 's' all the way through, and there's no problem.
memory corruption?
> when I step into it while debugging, it gets stuck on v1+=(fx[i][0])*(pow(est1, fx[i][1]));
> and sends me to the math.h library.
That's what `step' is supposed to do. It goes to the next line,
if there is a function call it would go into that function

> If I try to run without debugging, the output window stops at the point where the function is called,
> not even ending, just getting stuck.
An infinite loop.
Please explain what you are trying to do with those loops.
It seems that you are evaluating a polynomial with a natural number.
Don't understand your break condition.
Please explain what you are trying to do with those loops.

The loops are not the problem, I assume, because when I step over the whole thing line by line, the loops don't interfere.
What I'm trying to do is get the loop, along with rand, to produce two outputs of the function. I want one to be positive and one to be negative, and both close to zero.
Infinite loop is probably right, since, especially for the second do..while, its possible that a function could be entered that would never have a negative result, no matter what the value of x
> especially for the second do..while
There is nothing especial for the second loop. If you can have f(x), you can easily have g(x) = -f(x)

> I want one to be positive and one to be negative, and both close to zero.
you want abs(f(x)) < 5
Look the values of x that you are testing with.
est1=rand()%5; can only be {0,1,2,3,4}

Put a limit in the number of iterations
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
K=0;
do{
	++K;

	v2=0;
	est2=rand()%5;
	for(i=0;i<s;i++)
		v2+=(fx[i][0])*(pow(est2, fx[i][1]));
	if((v2>-5)&&(v2<0))
		break;

	if(K>42){
		std::cerr << "iteration limit exceeded\n";
		exit(1);
	}
}while(1);
Last edited on
now it only prints iteration limit exceeded. i removed the %5 from rand().
I don't understand what std::cerr does.
if I make K># a larger number each time, will it eventually work?
std::cerr is the output for error, just like std::cout it prints messages to the screen.
std::cerr is the output for error, just like std::cout it prints messages to the screen.


i don't fully understand, but it seems like its used when you already know when and where the error is, what's the point of that?
cerr and cout use different output channels, so error messages can be filtered
cerr doesn't get buffered, that helps if your programm crashes
> if I make K># a larger number each time, will it eventually work?
No. Suppose an even order polynomial, it may not have real roots.

You may throw real numbers in a range, increasing the range once in a while.
Eventually you should just give up.
Eventually you should just give up.


yea I just changed it completely. i'm thinking that making something like this work is a little beyond my level.
Maybe I worded it badly.
I mean that the program should fail.
Topic archived. No new replies allowed.