Thread: Problem with code

  1. #1
    brian0918
    Guest

    Problem with code

    The problem with my code arises when I try putting in a value for quadmin and quadmax of around 60000. It starts saying all the results are negative numbers, though they can't be because of the numbers I'm using.

    Code:
    			short a;
    			short bc;
    			short cc;
    			unsigned short iterations;
    			cout << " " << endl;
    			cout << " " << endl;
    			cout << "----------------------------------------------" << endl;
    			cout << " " << endl;
    			cout << " " << endl;
    			cout << "How many quadratics? (from n=min to n=max)" << endl;
    			cout << "min?" << endl;
    			cin >> quadmin;
    			cout << "max?" << endl;
    			cin >> quadmax;
    			cout << "How many iterations? (from 0 to 65,535)" << endl;
    			cin >> iterations;
    			cout << "a?" << endl;
    			cin >> a;
    			cout << "b c?" << endl;
    			cin >> bc;
    			cout << "c c?" << endl;
    			cin >> cc;
    			cout << " " << endl;
    			cout << " " << endl;
    			cout << "-----------------------------------------" << endl;
    			cout << " " << endl;
    			cout << " " << endl;
    
    	
    			
    			for(n = quadmin; n <= quadmax; n++) 
    			{
    				//keeps track of how many primes result for this quadratic
    				int primes = 0;	
    				//keeps track of the total iterations for this quadratic
    				int total = 0;	
    				
    				//START of test for increasing values of i
    				for(unsigned short i=0;i<iterations;i++)
    				{
    		
    
    
    					short flag = 0;
    
    					__int64 num = ((a*i*i) + (((2*a*n)+bc)*i) + ((a*n*n)+(bc*n)+cc));
    		
    					
    					if(num>1)
    					{
    						if((num%2)==0)	//checks against 2
    						{		
    
    							flag = 1;
    						}
    		
    						else
    						{
    
    							// checks odds from 3 up to sqrt(n)
    							for(long m = 3;m<=((long)(sqrt(num)));m+=2)	
    							{	
    				
    								if((num%m)==0)
    								{ 
    					
    									flag = 1;
    									break;
    			
    								}
    	
    							}
    
    						}
    		
    		
    						if(flag==0)
    						{
    
    							primes = primes + 1;
    
    						}
    
    						total++;
    					}
    
    					else
    					{
    						long double r = num;
    						cout << r << " was tossed out for being <= 1" << endl;
    						cout << " " << endl;
    					}
    		
    				}
    
    									
    				cout << a << "x^2 + " << ((2*a*n)+bc) << "x + " << ((a*n*n)+(bc*n)+cc) << endl;
    				cout << primes << " out of " << total << " were prime." << endl;
    				cout << " " << endl;
    
    			}


    and here's the output (including the numbers I used)


    Code:
    --------------------------------------------
    
    
    How many quadratics? (from n=min to n=max)
    min?
    60000
    max?
    70000
    How many iterations? (from 0 to 65,535)
    81
    a?
    1
    b c?
    -1
    c c?
    41
    
    
    -----------------------------------------
    
    
    -6.95027e+008 was tossed out for being <= 1
    
    -6.94907e+008 was tossed out for being <= 1
    
    -6.94787e+008 was tossed out for being <= 1
    
    -6.94667e+008 was tossed out for being <= 1
    
    -6.94547e+008 was tossed out for being <= 1
    
    -6.94427e+008 was tossed out for being <= 1
    
    -6.94307e+008 was tossed out for being <= 1
    
    -6.94187e+008 was tossed out for being <= 1
    
    -6.94067e+008 was tossed out for being <= 1
    
    -6.93947e+008 was tossed out for being <= 1

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What are the types of n,quadmin and quadmax ?

    Because if they are short, as in your previous code, then you've wrapped your positive integers into negative

  3. #3
    brian0918
    Guest
    Oops, sorry I didn't include that part originally.

    They are all longs.

    long n;
    long quadmin;
    long quadmax;

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'd change those other variables to be long as well

    For instance
    > (a*i*i)
    This is evaluated as a short expression which suggests rounding and truncation is going on.

    The expression only gets converted to __int64 when the assignment is made, and by then it's too late

  5. #5
    brian0918
    Guest

    Unhappy

    Ok, I switched all shorts to long (except for the flag)... and it still doesn't work! The same result as before.

    I even replaced:

    __int64 num = ((a*i*i) + (((2*a*n)+bc)*i) + (anum + bnum + cnum))

    with:


    __int64 = (a*i*i);
    __int64 = (((2*a*n)+bc)*i);
    __int64 = ((a*n*n)+(bc*n)+cc);
    __int64 num = (anum + bnum + cnum);



    And it still didn't work. It keeps saying the current num is a negative, which is impossible. In my example, I go up to 80 in the iterations, and 70000 in the n... So, num is:

    1*80*80 + (2*1*70000 - 1)*80 + ((1*70000*70000)+(-1*70000)+41)

    which is 4,911,136,361

    The range for __int64 is –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

    If num was a "long", it would be expected to screw up due to overflowing numbers, but its much larger than a "long".. I don't get it!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You've still got the same problem
    1*70000*70000 does not fit into an unsigned long result

    Try
    ( (__int64 )a*n*n )
    which should force this part of the expression to be evaluated using __int64 arithmetic

  7. #7
    brian0918
    Guest

    Talking WOOHOOO

    Thanks! That did it! I had to basically put (__int64) in front of everything.

    The final version was:

    __int64 num = (((__int64)a*i*i) + (((__int64)((__int64)2*a*n)+bc)*i) + (((__int64)a*n*n)+((__int64)bc*n)+cc));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM