I’ve been playing with some bigger values with Java, and I’m running across something I don’t understand. For some reason, Java seems to like giving me bum data (although, it’s far more likely I’m telling it to give me bum data)
Here’s a snippet, edited for clarity:
System.out.println(
"2 == " + (Math.pow(51, 13) % (77))
);
Which, according to both Wolfram Alpha (See link below), and the rest of my algorithm is wrong.
(Output:)
2 == 70.0
http://www.wolframalpha.com/input/?i=51^13+mod+77
Any ideas?
I believe it’s because of a precision issue.
doubles are only precise up to 15 or so digits.Math.pow(51,13)is a huge number (~20 digits) so when you try modding it by 77, you’re going to have numerical errors.For arbitrary precision arithmetic, take a look at BigInteger and BigDecimal.