• Post Reply Bookmark Topic Watch Topic
  • New Topic

sum is infinity  RSS feed

 
Muhammad Usman Siddiqui
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

i googled a lot but could not find the answer so posting here

i believe my variable cant hold this value.
any suggestions?

thanks in advance.

 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags when posting code. I've added them for you this time. See how much better that looks?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a quick search says that Fib(75) is approx. 2,111,485,077,978,049 You are trying to compute Fib(4,000,000). I'm not surprised it won't fit in a Float.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhammad Usman Siddiqui wrote:
i believe my variable cant hold this value.
any suggestions?


The IEEE 754 specification, used by Java for floating point, specifies "infinity" as the largest possible positive value. Basically, when you overflow the double variable on the plus side, the variable becomes "infinity". This is different from integers, which use the twos complement representation, and wraps around to negative values on overflow.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:a quick search says that Fib(75) is approx. 2,111,485,077,978,049 You are trying to compute Fib(4,000,000). I'm not surprised it won't fit in a Float.


I am, however, surprised that the OP had the patience to wait for the program to finish...

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
with no IO except the final result, it runs pretty dang fast...a few seconds, if that.
 
Muhammad Usman Siddiqui
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:Please use code tags when posting code. I've added them for you this time. See how much better that looks?


thanks. i will.
 
Muhammad Usman Siddiqui
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Muhammad Usman Siddiqui wrote:
i believe my variable cant hold this value.
any suggestions?


The IEEE 754 specification, used by Java for floating point, specifies "infinity" as the largest possible positive value. Basically, when you overflow the double variable on the plus side, the variable becomes "infinity". This is different from integers, which use the twos complement representation, and wraps around to negative values on overflow.

Henry


how can i fix it? can you point me to some where?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't use a float. your solution is already there - us a BigInteger instead.

And quite honestly, you shouldn't be using a float anyway. You are working with integers, so you should use some kind of integer type.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:a quick search says that Fib(75) is approx. 2,111,485,077,978,049 You are trying to compute Fib(4,000,000). I'm not surprised it won't fit in a Float.

And, since one side of the sum of the numerator is less than 1, an approximation for F(4,000,000) is (1.618^4,000,000)/√5, which will have ≈ 832,000 digits. So: good luck with that!

Winston
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought that the JiT compiler would optimise away the multiplications:
I have 3999817 additions of ∞ to a positive number. Well, that's ∞. Pheew!
And welcome to the Ranch
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!