Author Topic: just for amusement, rounding error  (Read 2136 times)

0 Members and 1 Guest are viewing this topic.

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
just for amusement, rounding error
« on: November 20, 2021, 10:42:59 am »
in the book "Handbook of Floating-Point Arithmetic" by Jean-Michel Muller and others, page 10
shows an interesting example, if you google for the afore mentioned book you may find a pdf of the same
Code: QB64: [Select]
  1. Dest Console
  2.  
  3. Option Explicit
  4.  
  5. Dim As Double i, account, euler
  6. Dim As Long j, n
  7.  
  8. n = 25
  9. Dim As Double approximate_value(25)
  10. For i = 1 To 25
  11.     Read approximate_value(i)
  12. euler = 2.7182818284590452353602874713527#
  13. Print "Exact value = n! * e - int(e * n!) where e = 2.718281828459045..."
  14. Print "  n  Approximate value         computed"
  15. For j = 1 To 25
  16.     account = euler - 1#
  17.     For i = 1 To j
  18.         account = i * account - 1
  19.     Next
  20.     Print Using "###"; j;
  21.     Print approximate_value(j), account
  22. Data .7182818284590452#
  23. Data .4365636569180905#
  24. Data .3096909707542714#
  25. Data .2387638830170856#
  26. Data .1938194150854282#
  27. Data .1629164905125695#
  28. Data .1404154335879862#
  29. Data .1233234687038897#
  30. Data .1099112183350075#
  31. Data .0991121833500754#
  32. Data .0902340168508295#
  33. Data .0828082022099543#
  34. Data .0765066287294056#
  35. Data .0710928022116781#
  36. Data .0663920331751714#
  37. Data .0622725308027424#
  38. Data .0586330236466206#
  39. Data .0553944256391715#
  40. Data .0524940871442588#
  41. Data .0498817428851762#
  42. Data .0475166005887012#
  43. Data .0453652129514256#
  44. Data .0433998978827887#
  45. Data .0415975491869292#
  46. Data .0399387296732302#
  47.  
output
Code: [Select]
Exact value = n! * e - int(e * n!) where e = 2.718281828459045...
  n  Approximate value         computed
  1 .7182818284590452         .7182818284590451
  2 .4365636569180905         .4365636569180902
  3 .3096909707542714         .3096909707542705
  4 .2387638830170856         .2387638830170822
  5 .1938194150854282         .193819415085411
  6 .1629164905125695         .1629164905124654
  7 .1404154335879862         .1404154335872576
  8 .1233234687038897         .123323468698061
  9 .1099112183350075         .109911218282548
 10 9.911218335007541D-02     9.911218282547907D-02
 11 .0902340168508295         9.023401108026974D-02
 12 .0828082022099543         8.280813296323686D-02
 13 .0765066287294056         7.650572852207915D-02
 14 .0710928022116781         7.108019930910814D-02
 15 .0663920331751714         6.620298963662208D-02
 16 .0622725308027424         5.924783418595325D-02
 17 .0586330236466206         7.213181161205284D-03
 18 .0553944256391715        -.870162739098305
 19 .0524940871442588        -17.5330920428678
 20 .0498817428851762        -351.661840857356
 21 .0475166005887012        -7385.898658004473
 22 .0453652129514256        -162490.7704760984
 23 .0433998978827887        -3737288.720950264
 24 .0415975491869292        -89694930.30280632
 25 .0399387296732302        -2242373258.570158