hi! I was trying to make a prime number test and this warning came out.
I surfed the web and found no possible fixes for this.
Implicit conversion loses integer precision: 'long' to 'int'
on line: else if (mod(a,(n-1)/2,n) <= jacob_char(a, n))
Any possible fixes, pls?
int main(int argc, char* argv[])
{
long PerformCalculation(void);
long n;
long k;
long a;
double t;
int flag = 0;
cin>>n;
cin>>k;
t=1-1/pow(2.0,k);
for (int i=1; i <= k; i++)
{
a=rand()%(n-2)+2;
if (NOD(a, n) > 1)
{
flag=1;
break;
}
elseif (mod(a,(n-1)/2,n) <= jacob_char(a, n))
{
flag=2;
break;
}
}
if (flag !=0)
{
cout<<"not prime \n";
}
else
cout<<"prime with probability of = " << t << "\n";
return 0;
}
make the int a long.
it is telling you that (for example) if you have a 64 bit integer and assign that value to a 32 bit integer, it may not work because it may not fit. If the actual value fits into a 32 bit int, its just a warning you can ignore. If not, it will give incorrect results. Better to just make them all the same size, get rid of both the warning and the potential bug.
thanks, thought the main problem is that i don't realise where are the ints are, like as u can see, there are only longs in the main and in the modulus or its a jacob_char?
int jacob_char(int a, int b)
{
if (NOD(a, b) != 1)
return 0;
else
{
int r = 1;
if (a < 0)
{
a = -a;
if (b%4 == 3)
r = -r;
}
do
{
int t = 0;
while (a%2 == 0)
{
t += 1;
a /= 2;
}
if (t%2 != 0 && (b%8 == 3 || b%8 == 5))
r = -r;
if (a%4 == 3 && b%4 == 3)
r = -r;
int c = a;
a = b%c;
b = c;
}
while (a != 0);
return r;
}
}