Mucking around with numbers
So, I was reading up on the Fibonacci sequence the other day, and wondered how long it would take CF to calculate the sequence to a couple of thousand steps, and also how long those numbers ended up becoming.
Consider the following loop:
<cfset n1 =0> <cfset n2 =1> <table> <thead> <tr><th>Row</th><th>No.</th><th>Length</th> </thead> <tbody> <cfloop from="1" to="1000" index="i"> <cfset x = (n1 + n2)> <cfoutput> <tr><td>#i#</td><td>#len(numberformat(x))#</td><td>#numberformat(x)#</td></tr> </cfoutput> <cfset n1 = n2> <cfset n2 = x> </cfloop> </tbody> </table>
I immediately hit a few issues. One was scientific notation, which was solved by numberformat(), but the other issue I found was that if I tried to increase the number of loop iterations beyond 1475, I’d get an error simply stating ‘For input string: “I”‘
Removing the numberformat mask revealed 1.30698922376E+308 as the largest generated number in the sequence, with the remaining rows represented as ‘1.#INF’.
So, out of curiosity, does anyone know why? Is it simply that Java can’t handle larger integers?