Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts

    I'm having problems comparing numbers in my code...

    Hello everybody, I'm having a few problems comparing numbers in my program which forces me to round them, which doesn't always give me the desired results...I'll quickly try to explain what I'm doing...I wrote a bunch of code that builds together a mathematical function based on user input...the function class ( called "Func" ) I wrote which contains methods for the functions derivatives etc. works just fine...it's finding a functions roots that is causing me problems. I'm a beginner so please bear with me. Here is the code I'm currently using to evaluate a functions roots:

    Code:
    // A function used for rounding
    double round( double x, double decimals )
    {
    	// Rounding one decimal
    	x = x * pow( 10, decimals );
    	x = int( x > 0.0 ? x + 0.5 : x - 0.5 );
    	x = x / pow( 10, decimals );
    	return x;
    }
    
    // Calculates a functions possible root using Newton's method
    double newtonsMethod( Func* f, double x0 )
    {
    	// Start the loop
    	for( int i = 0; i < 10; i++ )
    	{
    		x0 = x0 - ( f->func( x0 ) / f->dfunc( x0 ) );
    	}
    	return x0;
    }
    
    // Evaluate the number of roots a function has
    int evaluateNumOfRoots( Func* f )
    {
    	double x = -10;		// Start at -10
    	double dx = 0.01;	// Move in steps of 0.01
    	int numOfReqMatches = 3;	// Number of required matches
    	int numberOfRoots = 0;		// The number of roots found
    	int matchCounter = 0;		// Is used to count the number of matches in the array
    	int oldestValue = 0;		// Stores which value is to replaced because it is the oldest
    
    	// Stores the last values which are to be checked for matches
    	double* prevResults = new double[numOfReqMatches];
    	double rootsFound[1000];	// 1000 should suffice
    
    	// Start actual loop
    	while( x < 10 )
    	{
    		if( x == 10 )
    		{
    			// Fill the elements with numOfReqMatches new values only for the first time
    			for( int i = 0; i < numOfReqMatches; i++ )
    			{
    				prevResults[i] = newtonsMethod( f, x );
    				x += dx;	// Change once more for next time before exiting loop
    			}
    		}
    		else
    		{
    			// Fill the oldest one with a new value
    			prevResults[oldestValue] = newtonsMethod( f, x );
    			x += dx;	// Change for next time
    
    			if( oldestValue == 2 )
    			{
    				oldestValue = 0;		// Reset if necessary
    			}
    			else
    			{
    				oldestValue++;			// Increment if necessary
    			}
    		}
    
    		// Check to see if we have matching values within the array
    		for( int i = 1; i < numOfReqMatches; i++ )
    		{
    			if( round( prevResults[i-1], 2 ) == round( prevResults[i], 2 ) )
    			{
    				matchCounter++;
    			}
    		}
    
    		if( matchCounter == numOfReqMatches - 1 )
    		{
    			numberOfRoots++;	// Found one!!
    			matchCounter = 0;
    		}
    		else
    		{
    			matchCounter = 0;	// Didn't find one
    		}
    	}
    	// Delete the array and return the number of Roots
    	delete[] prevResults;
    	return numberOfRoots;
    }
    I'll quickly explain how the last function works:
    I'm using an array with three places to test whether or not I have found a root. A root is found, if all 3 of the elements in the array are the same. I am always replacing the oldest value in the array with the current result I get for a possible root using the newtons Method...the newtons Method will give me a possible answer (usually if the starting point is close enough). If I have the matching answers from that function, I have found a root, and the variable used for counting roots is incremented. Right now this function always increments said variable when 3 matches are found in the array, so I am of course getting way more roots than there are, because I find the same ones a lot of times. I think a look in the array will help explain what I mean. I'm calculating them for x[-10;10] in steps of dx = 0.01...the array looks as follows (note that the oldest value is always replaces each time x is increased by dx):

    Code:
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    .
    .
    .
    .
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -1.99999 | -2 | -2
    -1.99999 | -1.99317 | -2
    -1.99999 | -1.99317 | -5
    -4.00069 | -1.99317 | -5
    -4.00069 | -4 | -5
    -4.00069 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    .
    .
    .
    -4 | -4 | -4
    .
    .
    .
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    The function I am working with does infact have its roots at x = -2 and x = -4...as you can see not all arrays have either -4 or -2 as their only elements, simply because the newtons method doesn't work all the time...but it's no problem, since I strictly only want to accept those solutions of the newtonsMethod( &f, x ) which are found 3 times in a row and stored in the array.
    Now, as you can see by the rough course of this array's stored elements, almost all of these "pairs" will be evaluated as function roots...this is where problems occur...
    Here is the part which checks if we have a root:

    Code:
    // Check to see if we have matching values within the array
    		for( int i = 1; i < numOfReqMatches; i++ )
    		{
    			if( round( prevResults[i-1], 2 ) == round( prevResults[i], 2 ) )
    			{
    				matchCounter++;
    			}
    		}
    
    		if( matchCounter == numOfReqMatches - 1 )
    		{
    			numberOfRoots++;	// Found one!!
    			matchCounter = 0;
    		}
    		else
    		{
    			matchCounter = 0;	// Didn't find one
    		}
    	}
    Anyways, I have to use the round() function I wrote to get the nearly 2000 found roots (numberOfRoots) I desire (since the array shows that almost all x values for newtonsMethod result in either -2 or -4....-10 to 10 in steps of 0.01 is 2000 values).

    If I don't use this round function, I only get about 800-something matches...why? The array clearly shows that newtonsMethod() returns values that are practically always exactly -2 or -4 as it returns doubles...why do I have to round these values to get more matches (and with that, the actual amount of matches) when the values of the array I printed out aren't like -2.00012 or -4.00035 or something like that, but rather, just -2 or -4?

    I hope I explained what my question is...I simply don't understand what the cause of this is as I am still very inexperienced...I also apologize for the wall of text and my not-so-good English (I'm German).

    Anyone know what the cause of this is? Also, if you have any suggestions of how I could optimize my code, I'd be more than happy to accept them, as the function I wrote (which isn't even complete yet), is already waaaaay to long.

    Cheers,
    Chris!

  • #2
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    From only a quick read I do have one question.
    A root is found, if all 3 of the elements in the array are the same.
    My question is, following the above quote of 3 elements are the same do you consider the following a root?
    -1.99999 | -2 | -2

    EDIT: When you round the above you should get -2 | -2 | -2 which would be a match (root), correct? Without the rounding it isn't a match because -1.99999 is not equal to -2?

    Basically, I'm checking to see if I understand your problem correctly.
    Last edited by Gox; 06-05-2009 at 10:30 PM.

  • #3
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    well you are correct in that it would normally not be considered a root...since I don't want to round values...but I have waaaaaaaayy more array "compositions" which have -2 | -2| -2 that I wouldn't care to round -1.999999.

    I don't think I explained it well enough. Take a look at the whole text file showing all values that the array enholds at one point or another while going through x = -10 to x = 10.

    Code:
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -1.99999 | -2 | -2
    -1.99999 | -1.99317 | -2
    -1.99999 | -1.99317 | -5
    -4.00069 | -1.99317 | -5
    -4.00069 | -4 | -5
    -4.00069 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -4
    -4 | -4 | -5
    -2 | -4 | -5
    -2 | -2 | -5
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    .
    .
    .(only -2 | -2 | -2 ) from now on
    .
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    -2 | -2 | -2
    you don't need to go through the entire array to understand that almost all "pairs-of-3" ( a thing consisting of 3 pieces which are the same...sorry I don't know what the word is in English )are matches...well over 1900 from 2000 anyways...only pairs-of-3 of -4 or -2 would be considered as roots in an ideal case. -1.9999 | -2 | -2 wouldn't be a root if I simply compared the values... the only reason I am rounding when comparing is because I am only getting about 830 found roots if I don't, which doesn't make sense, as there are clearly more "-2|-2|-2" and "-4|-4|-4" pairs-of-3 than 830, and I don't know why...

    edit: there is a mistake in my first posts code
    this:
    Code:
    while( x < 10 )
    	{
    		if( x == 10 )
    .
    .
    .
    should be changed to
    Code:
    while( x < 10 )
    	{
    		if( x == -10 )
    .
    .
    .
    so x should be -10...but thats not really of importance for my problem...just wanted to correct that
    Last edited by BrickInTheWall; 06-06-2009 at 04:35 AM.

  • #4
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    ^stupid spam...
    I'd like to add something to my problem:
    It best becomes visible when checking for roots which are only -2 or -4 and not rounding the values received from the newtonsMethod() method...here is what I mean:

    I change this line:
    Code:
    		if( matchCounter == numOfReqMatches - 1 )
    		{
    			numberOfRoots++;	// Found one!!
    			matchCounter = 0;
    		}
    to:
    Code:
    		if( matchCounter == numOfReqMatches - 1 && newtonsMethod( f, x ) == -4 )
    		{
    			numberOfRoots++;	// Found one!!
    			matchCounter = 0;
    		}
    I do the same thing for -2 ...without using the round() function I wrote, I only get about 68 matches and for -2 I get about 1300-something when there are clearly way more....sadly I don't get exactly the right amount of matches either when using the round() function which leads to the fact that it will find a root which is practically the same as one it has already found....I don't get why, because all the values in the array (see text file above) are not rounded, but actually calculated using the newtonsMethod() function and output to a text file....not rounded

  • #5
    Gox
    Gox is offline
    Regular Coder Gox's Avatar
    Join Date
    May 2006
    Location
    Ontario, Canada
    Posts
    392
    Thanks
    2
    Thanked 20 Times in 20 Posts
    One quick question from your previous post.
    Take a look at the whole text file showing all values
    Is what you posted indeed the whole file? If so I find about 820 instances where there's a "pair-of-3" on a single line. Which is roughly how many you say you get when you don't round. This makes me think that maybe your comparison code is correct but that there is a problem elsewhere is there should be more than 820 "pairs-of-3" in the text file. Am I still confused on what is considered a "root".

    I'll have to look more closely later at you last post.
    Last edited by Gox; 06-06-2009 at 07:22 PM. Reason: Grammar

  • #6
    Regular Coder BrickInTheWall's Avatar
    Join Date
    Mar 2009
    Location
    Munich, Germany
    Posts
    139
    Thanks
    1
    Thanked 13 Times in 13 Posts
    Hi, no sadly it's not the entire file as it would have been too many characters for a post here...but it's 2000 lines and I counted the ones that aren't roots...so I should be getting over 1990 roots...
    these are roots:
    -2|-2|-2
    or
    -4|-4|-4

    these aren't roots:
    -1.99999|-2|-2
    -2|-4|-5

    the problem is...my program doesn't always recognize -2|-2|-2 or -4|-4|-4 compositions as roots even though the text file with the array values clearly shows that these stored values are whole numbers and not -2.0000001 for example. Because of this I am forced to round at a certain decimal which also leads to that -1.9999|-2|-2 will be considered as a root.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •