home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / BBS / DOORS / SRE0982B.ZIP / BUGEXAMP.TXT next >
Encoding:
Text File  |  1991-09-09  |  2.9 KB  |  63 lines

  1. This file is for programmers.  If you are not a programmer, you may not
  2. understand some of the example programs, but you might want to read this
  3. anyway.
  4.  
  5. ────────────────────────────────────────────────────────────────────────
  6. To show you why some bugs are caused and not easily fixed, consider this
  7. program:
  8.  
  9. Var I, J : Integer;                │ main()
  10. Begin                              │ {    int i, j;
  11.   I := 10000 * 100 div 50;         │      i = 10000 * 100 / 50;
  12.   J := 10000 div 50 * 100;         │      j = 10000 / 50 * 100;
  13.   Writeln( I, ' = ', J );          │      printf( "%d = %d\n", i, j );
  14. End.                               │ }
  15.  
  16. The result should be that I is equal to J.
  17.  
  18. Now a similar program:
  19.  
  20. Var I, J, A, B, C : Integer;       │ main()
  21. Begin                              │ {    int i, j, a, b, c;
  22.   A := 10000;                      │      a = 10000;
  23.   B := 100;                        │      b = 100;
  24.   C := 50;                         │      c = 50;
  25.   I := A * B div C;                │      i = a * b / c;
  26.   J := A div C * B;                │      j = a / c * b;
  27.   Writeln( I, ' = ', J );          │      printf( "%d = %d\n", i, j );
  28. End.                               │ }
  29.  
  30. Now the result is that I is not equal to J.  Mathematicians usually tell
  31. computer programmers that it's crazy to have a language that does this.
  32. Yet we have to live with it.  This has been a major cause of SRE bugs;
  33. many times nothing happens until the empires are large.  (Replace 10000
  34. by 3 and the bug won't appear!)  That makes debugging slow and painful
  35. because that kind of problem isn't something that is obvious late at night.
  36. Furthermore, if you try typing in "A*B DIV C" into the debugger, it gives
  37. the right answer, but when you run it, it won't give the right answer!
  38. ────────────────────────────────────────────────────────────────────────
  39.  
  40. Conclusion:  programming languages need a "SuperInteger" or something
  41.              larger than LongInt so that SRE and other programs that
  42.              work with large numbers will work correctly.
  43.  
  44. Remember this when you write your own program and you can't figure out
  45. why in the world 1000 * 5000 / 1000 is not always 5000.  Try rearranging
  46. the formula!
  47.  
  48. Have fun!  ;)
  49.  
  50. Amit Patel, programmer at the Solar Realm
  51.  
  52. ────────────────────────────────────────────────────────────────────────
  53. P.S.  If you still haven't figured out why this happens, type in:
  54.               I := 10000 * 100;    │      i = 10000 * 100;
  55.  
  56.       and see what happens.  1 million won't fit into an integer, so
  57.       it causes strange results (or compile time errors if you use numbers
  58.       and not variables).  Integers can store up to 32767, which is NOT
  59.       enough to store how much money you have in SRE!  Using "long" integers
  60.       only delays the problem.
  61. ────────────────────────────────────────────────────────────────────────
  62.  
  63.