Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 I have a problem to find the cause of "invalid operands to binary %"
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Castoris




PostPosted: Thu Jul 12, 2012 11:49 am   Post subject: I have a problem to find the cause of "invalid operands to binary %"

Hello everyone,

please tell me what cause this error "invalid operands to binary %"
in my program


# include <stdio.h>
# include <math.h>


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
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Thu Jul 12, 2012 12:20 pm   Post subject: Re: I have a problem to find the cause of "invalid operands to binary %"

Observe that
C:
double num = 7;
int i = 3;
printf ("%d", num % i);

gives the error "invalid operands to binary %", whereas
C:
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




PostPosted: Thu Jul 12, 2012 12:21 pm   Post subject: Re: I have a problem to find the cause of "invalid operands to binary %"

Sorry double posted by accident, web page wasn't responding.
Castoris




PostPosted: Thu Jul 12, 2012 4:49 pm   Post subject: RE:I have a problem to find the cause of "invalid operands to binary %"

I used double to store the big number 60085147143

is there not another way to store this number??

thank you
Dreadnought




PostPosted: Thu Jul 12, 2012 6:28 pm   Post subject: Re: I have a problem to find the cause of "invalid operands to binary %"

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




PostPosted: Fri Jul 13, 2012 4:38 pm   Post subject: Re: I have a problem to find the cause of "invalid operands to binary %"

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 <stdint.h>) 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




PostPosted: Tue Sep 04, 2012 4:31 pm   Post subject: RE:I have a problem to find the cause of "invalid operands to binary %"

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.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: