When Computers Don't Compute and Other Fun with Numbers

  • Upload
    pde1d

  • View
    218

  • Download
    0

Embed Size (px)

DESCRIPTION

Presents some basic reasons computers do not give results that you would expect. Also, include some miscellaneous topics including Friday 13th, Fibonacci, Golden Ratio, Chaos, Founding Fathers and Math, Statistics, Music, Magic Squares.

Citation preview

  • 1. When Computers Don't Compute And Otherpde1d.blogspot.com

2. Have you ever gotten a wrong answer from a computer ? Finite precision numbers Chaos Limited number of numbers Insufficient resolution / memorySoftware errors Branch cuts User errors / poor documentation pde1d.blogspot.com 3. There are 10 types of people, those that understand binary numbers, and those that don't. 4s / sign bit2s bit1s bitunsignedsigned0 0 0 0 1 1 1 10 0 1 1 0 0 1 10 1 0 1 0 1 0 10 1 2 3 4 5 6 70 1 2 3 -4 -3 -2 -13 + 1 -47+105 = -32/3 0 pde1d.blogspot.com 4. Factorial !!!nn! =k= 1234...(n1)nk=1n ! = n(n1)! 8 Byte signed integers20! = 2432902008176640000 21! = -4249290049419214848 64! = -9223372036854775808 65! = -9223372036854775808 66! = 0 pde1d.blogspot.com 5. 70! = Googol + C,C++, Bash, Csh, Fortran, Java 0 HandCalculator Error LibraOffice,Calligra Sheets, Gnumeric 1.19785716699699E+100 Python,Lisp, dc, bc, GMP, BigInteger 119785716699698917960727837216890 987364589381425464258575553628646 280095827898453196800000000000000 00 pde1d.blogspot.com 6. Spreadsheets Googol + LibraOffice 1197857166996990000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000Calligra Sheets 1197857166996989221259474668628791114472893603304820 9921351258923353917776673784237595759172044980224Gnumeric 1197857166996989026992585446055884022526702920952930 327894441987121439652486137449869147396683648204870! = 119785716699698917960727837216890987364589 381425464258575553628646280095827898453196 80000000000000000 pde1d.blogspot.com 7. pde1d.blogspot.com 8. dc & bc dc Desktop calculator Stack based calculator for Unixes dc was bc ( Before C ) and before bc Arbitrary Precision Integer arithmetic with decimal point shiftbc basic/bench calculator C like syntax Originally piped through dc 'bc -l' adds math library and scale=20 digits Sine, Cosine, Arctan, ln, exp, Bessel fcn pde1d.blogspot.com 9. High Precision Libraries GMP Gnu Multiple Precision MPFR Gnu Multiple Precision floating-point with rounding MPC Complex numbersDD, QD 32 and 64 digit accuracy PARI/GP Algebra C library + shell pde1d.blogspot.com 10. Floating Point Numbers EPSILON Smallest number such that1.0 + != 1.0 or, if < 1.0 + = 1.0 Floating Point numbers IEEE 754 - 2008 FLT_EPSILON = 1.19209e-07 DBL_EPSILON = 2.22045e-16CPU registers may use higher precision Detailed definitions vary by factor of ~2 pde1d.blogspot.com 11. Precision Loss Rapidly varying functions oarccos( 0.99999999984769129011 ) = 0.001 o arccos( 0.99999999939076516049 ) = 0.002 thChange in 10 place doubles result Subtraction of nearly equal numbers 3.14159292035xxx 3.14159265359xxx 0.00000026676xxx 12 place accuracy reduced to 5 places pde1d.blogspot.com 12. Quadratic Formula b b 4 a c X= 2a 22a X +b X +c=0 2What ifb 4acor22b 4 a c or b 4 a c1 EPSILON Loss of digits 2 pde1d.blogspot.com 13. Quadratic Equation Examples Traditional and alternate solution formulations*2Enter a,b,c: 1 -2 0.9999999999999996 X = 1.00000002107342 , 0.999999978926576 Err: 0 , -1.11022e-16 Alt. sol. X = 1.00000002107342 , 0.999999978926576 Err: 0 , 02Enter a,b,c: 1 -2000000 1 X = 1999999.9999995 , 5.00003807246685e-07 Err: -0.000240445 , -7.61449e-06 Alt. sol. X = 1999999.9999995 , 5.00000000000125e-07 Err: -0.000240445 , 0b 4ac b 4ac2b 4acEnter a,b,c: 10000 1 -10000 X = 0.99995000125 , -1.00005000125 Err: -1.81899e-12 , 0 Alt. sol. X = -1.00005000125 , 0.99995000125 Err: 0 , 0*Acton, Forman S., REAL Computing Made Real,pp 5-6, Dover, 2005/ Princeton Press 1996pde1d.blogspot.com 14. It's About Time Not the 1966 TV show The value of time is inversely proportional to the amount remaining Example, 50 times slower 1 millisecond computation 4 day computationSome times for 500X500 grid Python ~30 min, numpy ~30sec, f2py ~2 sec Numerical Computing with SAGE, pg 18, Aug 15, 2013 pde1d.blogspot.com 15. Time Cost of Precision Double(15) baseline gotoBLAS DD(32) 11 to 312 X QD(64) 109 to 3060 X GMP(77) 132 to 3700 X GMP(154) 197 to 5900 X MPFR(154) 405 to 11500 X Times/speeds from MPACKhttp://www.slideshare.net/NakataMaho/the-mpack-multiple-precision-version-of-blas-and-lapackpde1d.blogspot.com 16. Pick 3 Digits Calculation Pick 3 ordered digits ( 1 2 3 ) Enter twice ( 123123 ) Divide by 11 Divide by 7 Divide by original 3 digit number - Result >bc -l 123123 last/11 last/7 last/123>dc 123123 11 / 7/ 123 / p pde1d.blogspot.com 17. Paraskevidekatriaphobia thFear of Friday the 13 Today is the first Friday the Thirteenth of 2013 In Thirteen weeks the next Friday th the 13 Triskaidekaphobia Fear of the number 13pde1d.blogspot.com 18. 13 A prime number Is the smallest emirp A happy number 12 + 32 = 10 12 + 02 = 1Wilson prime ( 3 known 5, 13, 563 ) 31 and 13 are both prime( (13-1)! + 1 ) % 132 = 0A Fibonacci number 1 , 1, 2, 3, 5, 8, 13, 21, 34, pde1d.blogspot.com 19. What is the smartest mammal ? Rabbits *they can multiply very fastFibonacci Leonardo Pisano Bigollo of Pisa Illustrated rabbit breeding in 1202 Liber Abaci F(n+1) = Fn + F(n-1) , F1 = 1 , F2 = 1 F(n+1) = number of possible arrangements of long and short items n* http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibnat.html pde1d.blogspot.com 20. Fibonacci Spiral8 Chris 73 / Wikimedia Commons 13 121 5Appears in Nature - Sunflower seed spirals Spiral with equal packing - Sea Shells ( not csh )3pde1d.blogspot.com 21. Fibonacci & Golden Ratio aF n+1 F n nbb a+b = = a b b aBeautiful Proportion, Phi symbol after Phidias Greek sculpture used and studiedpde1d.blogspot.com 22. Golden Ratio Uses Penrose Tiles Non-repeating pattern Phi ratio of tiles 112011 Nobel Prize Quasicrystals 3D Non-repeatingSir Roger Penrose Solorflare100 Wikimedia Commons1 2 o = 2 cos( 5 ) = 2 cos(72 ) = pde1d.blogspot.com 23. Calculating Golden Ratio 1 = 1+ 1 1+ 1 1+ 1+. . . =Golden Mea n Divin e P ro portio n Gold en Se c ti o n1+ 1+ 1+ 1+ 1+...1+ 5 = 2Do you like stand-up comedians? pde1d.blogspot.com 24. One Liners for Gloden Ratio Continued fraction Continued squareroot echo 67k 1[dsb1r/1+pdlb!=c]dscxq | dc 163 iterations dc -e 67k1[dsb1+vpdlb!=c]dscxq 133 iterationsFibonacci echo "67k1sa1d[sclardsa+dla/pdlc!=b]dsbx" | dc 163 iterationspde1d.blogspot.com 25. Golden Ratio by dc Equation - 67k5v1+2/p Actions67 Push '67'Stack 67kPop set precision5Push '5'5vPop Sqrt Push2.236...1Push '1'1 ; 2.236...+Pop 2 add Push3.236...2Push '2'2 ; 3.236.../Pop 2 divide Push1.618...pPop print Push1.618... pde1d.blogspot.com 26. More dc echo 67k5v1+2/dd*rd1r/[0]Ppsapsapq | dc 0.6180339887498948482045868343656381177203091798057628621354486227052 1.6180339887498948482045868343656381177203091798057628621354486227052 2.61803398874989484820458683436563811772030917980576286213544862270500.816033...6227052 1.816033...6227052 2.816033...6227050 1 = = 12 = +1 pde1d.blogspot.com 27. Python Decimal Numbers > python >>> 1.1+2.2 3.3000000000000003 >>> from decimal import * >>> getcontext().prec=67 >>> phi = ( Decimal('5').sqrt() + 1 ) / 2 >>> print 1/phi, 'n', phi, 'n', phi*phi 0.618033988749894848...21354486227050 1.618033988749894848...2135448622706 2.618033988749894848...2135448622708 pde1d.blogspot.com 28. Irrational Number Power Can an irrational number to an irrational power give a rational result ?(2) (2)(( 2) )= 2> python >>> from decimal import * >>> getcontext().prec=67 >>> s = Decimal(2).sqrt() >>> print (s**s), "**", s 1.6325269194381528447734... ** 1.4142135623730950488016... >>> print (s**s)**s 2.000000000000000000000000000000000000000000000000000 000000000000002 pde1d.blogspot.com 29. Chaos Maxwell Smart's arch nemesis Lorenz's Strange Attractor Wikimedia commonsButterfly effect KAOSSmall initial difference yield a large impactNon-periodic behavior Example recurrence relations Logistics equationx n+1 = r x n (1x n ) , 3.82843 < r 4 , 0 < x < 1or 2 nx n+1=x 2, 2 < x < 2 pde1d.blogspot.com 30. 2 nX0 = 0.5 vs 0.49...x n+1=x 22 1.5 1 0.5 Xn0 -0.5 -1 -1.5 -2 -2.5 02468101214161820Iteration nX_0=0.52.5Twelve NinesX_0=0.4999999999999Rain2 1.5 1Xn0.5 0 -0.5 -1 -1.5 -2 -2.5 80828486889092949698100Iteration nX_0=0.5X_0=0.4999999999999pde1d.blogspot.com 31. 2 nRecurrence Relation x n+1 = x 2 An infinite number of initial values end with x = 2 x 0 = 2 , x 1=0 , x 2 =2 , x 3=2 , x 4 =2 , . . . 2 nx n+1 = x 2 x=0 for( i=1; i