java中编译后显示负数,Java Longs中的Fibonacci计算显示为负数

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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值