Implicit conversion loses integer precision: 'long' to 'int'

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  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;
        }
        else if (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;
}
Last edited on
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
    }
}
Last edited on
I will help you to see it:

long n;
long a;

..CHOP
else if (mod(a,(n-1)/2,n) <= jacob_char(a, n))

vs:
int jacob_char(int a, int b)

the act of calling jacob_char with a long and stuffing it into an int is at least one place for this warning/complaint.
Last edited on
Topic archived. No new replies allowed.