I am creating a program to calculate Mersenne primes in Java. While doing so, I noticed that the modulo %
operator doesn't function properly when dealing with large numbers.
Here is my code:
double a = 2305843009213693951d; //2^61 -1
double b = 2;
double m = a % b; //this should be 1.0
System.out.println(m); //prints 0.0
While one would expect the result to be 1, since the original number isn't even, it actually returns 0.0
.
Why is this, and how can I fix it?