### ====================================================================== ### Print a table of representable Fibonacci sequence values. awk integer ### values masquerade as 64-bit floating-point values, so they are limited ### by the size of the fraction (53 bits in IEEE 754 arithmetic, common on ### most modern systems). ### ### Usage: ### awk -f Fibonacci3.awk ### ### [19-Apr-2002] ### ====================================================================== BEGIN { Fibonacci(); exit(0) } function Fibonacci() { lo = hi = n = 1 golden_ratio = (1.0 + sqrt(5.0))/2.0 limit = 2^53 - 1 printf("%2d\t%21s\n", n, add_group_commas(lo)) while (hi <= limit) { n++ ratio = hi/lo printf("%2d\t%21s\t%19.15f\t%19.15f\n", \ n, add_group_commas(hi), ratio, (ratio - golden_ratio)) hi = int(int(lo) + int(hi)) lo = int(int(hi) - int(lo)) } } function add_group_commas(n, k,len_s,s,t) { s = sprintf("%.0f", n) t = "" len_s = length(s); for (k = len_s - 3; k > 0; k -= 3) t = ("," substr(s,k + 1,3) t) t = (substr(s,1,k + 3) t) return (t) }