My Fibonacci calculator works fine, but when going up to higher numbers the result comes up negative, as it would if it was an Integer over its max value.
It is working with a cache java.util.Map. Everything that goes into the Map is exactly what is expected, but when printing it out I get e.g. for 291:
-784134397488903422
2923602405716568564338475449381171413803636207598822186175234
It seems something goes wrong with my Longs, but I am not sure yet, what exactly. Could someone please point me in the right direction?
解决方案
I think you are above the maximum long value that can be stored in a signed 64 bits integer used by Java for the long type, more information about Long and thesein the Java API: http://docs.oracle.com/javase/7/docs/api/java/lang/Long.html.
The maximum positive value for a 64 bits signed integer is 2^63 -1: 9 223 372 036 854 775 807, your value seems to have reach this limit and if the highest bit of a signed integer is 1, then the signed integer become negative (see 2 complement integer for more details: http://en.wikipedia.org/wiki/Two%27s_complement).