+ 1
Result from Math.pow(x, y) % n
https://code.sololearn.com/cGY22YoD439g/?ref=app Can someone explain me why the result is 93.0? It should be 2.0. What I want to calculate is (x^y)mod n.
7 odpowiedzi
+ 10
Bittersweet in java you have the BigDecimal class used for these big numbers
+ 3
Overflow. 7.0 and 23.0 are doubles. A double has 8 bytes = 64 bit, meaning it can represent 2^64 = 18,446,744,073,709,551,616 different values (edit: not really, see my other post). 7^23 = 27,368,747,340,080,916,343. So the result of 7^23 can't be stored in a double. That's why the result is wrong.
+ 3
Yes and no. I guess my explanation wasn't 100% correct. A double has 8 bytes, but from these 8 bytes, a couple of bits are reserved for the mantissa, a couple of bits for the exponent and one bit for the sign. So you can indeed store very large numbers like 1e+308 in a double, but only up to a precision of ~15 decimal places. Everything beyond that will be lost. The double can't hold each single digit of very large numbers. Instead of the actual value 123,456,789,012,345,678(...),456,789, it will hold a value like 1.23456*10^300. So it doesn't have enough precision to perform a modulo division because that would need each single digit of the number and not just an approximate representation like 1.23456*10^300.
0
Oh, really? I read that a double could hold values up to 1.7e+308. Or am I getting this whole thing wrong?
0
So... if I want to have a full representation of a number with each single digit, the greatest number a double can hold is 2^64?
And If I don‘t need a full representation I can have doubles with a total value of 1.7e+308 (or something like that) but in that case only the first few decimal places are actually given and the rest isn‘t stored?
So that in your case of 1.23456*10^300 the first 6 decimal places are given and the 294 other are just 0 because they are just not stored so that in the end I only have the rough size of that number?
And 1.7e+308 is the greatest value a double can store when you write it like m*b^e?
Am I getting this right?
0
The only thing I‘m asking myself right now... if the number is too big to store in a double, are there any other datatypes in which I could store it?
0
Oh, alrgiht. I will try it out. Thank you :)