
-----------------------------------
Castoris
Thu Jul 12, 2012 11:49 am

I have a problem to find the cause of &quot;invalid operands to binary %&quot;
-----------------------------------
Hello everyone,

please tell me what cause this error "invalid operands to binary %"
in my program


# include 
# include 


int main (void)
{
	int i, j = 0, k;
	double number = 60085147143;
	int gcd_prime_results[50];// Here we will store the result of the common prime divisors

	printf("%f", number);
	
	for ( i = 2; i < (sqrt(number)/2); i++)// we break down "number" because we know that we do not have to get very far
	 {	
	 	if(number % i == 0)// we check if i is a divisor of the number
	 	{
	 		for( k = 2; k < sqrt(i); k++)// first we verify if it is a prime number
			{ 
				if( i % k != 0)// if it is a prime number
				{
					if((k + 1) > sqrt(i))//if it is the last number to verify,  because we need to verify it with every possible number before we procede to the next step
					{						
	 					gcd_prime_results[j] = i;// store the divisor into an array
	 					number / i;// the new number to look for its divisor
						j++;// it goes ahead into the array
					}
				}
				else
					break;// if we found that the number is not prime, let's quit the loop and do not try the other numbers because we already know that the number is not prime 
			}
	 	}	
	 }	

	 for(k = 0; k < j; k++)
	 	printf("%d   ", gcd_prime_results[k]);
	 	
 	return 0;
}



Hope you understand it, and thank you

-----------------------------------
Dreadnought
Thu Jul 12, 2012 12:20 pm

Re: I have a problem to find the cause of &quot;invalid operands to binary %&quot;
-----------------------------------
Observe that double num = 7;
int i = 3;
printf ("%d", num % i);
gives the error "invalid operands to binary %", whereas
int num = 7;
int i = 3;
printf ("%d", num % i);
does not.

The reason is that the remainder is only defined for integer by integer division.

-----------------------------------
Dreadnought
Thu Jul 12, 2012 12:21 pm

Re: I have a problem to find the cause of &quot;invalid operands to binary %&quot;
-----------------------------------
Sorry double posted by accident, web page wasn't responding.

-----------------------------------
Castoris
Thu Jul 12, 2012 4:49 pm

RE:I have a problem to find the cause of &quot;invalid operands to binary %&quot;
-----------------------------------
I used double to store the big number 60085147143

is there not another way to store this number??

thank you

-----------------------------------
Dreadnought
Thu Jul 12, 2012 6:28 pm

Re: I have a problem to find the cause of &quot;invalid operands to binary %&quot;
-----------------------------------
Well, you can always try larger integer sizes like long and long long.
Alternately, if you don't mind writing the remainder function yourself, you can break the number up into pieces and find the remainder using the remainders of the individual pieces. For example, you could use a list and store 4 digits per node (4 digits is nice, since 9999*9999 does not overflow for 32-bit integers).

-----------------------------------
bl0ckeduser
Fri Jul 13, 2012 4:38 pm

Re: I have a problem to find the cause of &quot;invalid operands to binary %&quot;
-----------------------------------
To do % on doubles you can use c = fmod(a, b), and for floats you can use c = fmodf(a, b).

If you have a recent compiler, uint64_t (from ) is big enough to hold 60085147143.
Otherwise, long long might work.

If you want, you can store big numbers as arbitrarily-long strings and then write custom arithmetic operators supporting these (it's not so hard -- for example, once I wrote a 67-line C program to calculate big factorials this way). EDIT: Or Dreadnought's suggestion, which sounds even better.

-----------------------------------
QuantumPhysics
Tue Sep 04, 2012 4:31 pm

RE:I have a problem to find the cause of &quot;invalid operands to binary %&quot;
-----------------------------------
double variable type is usually used to store numbers with decimal points, that would be a number great than 32767, so you would need to use either unsigned int before the variable number or long.
