home *** CD-ROM | disk | FTP | other *** search
- This file is for programmers. If you are not a programmer, you may not
- understand some of the example programs, but you might want to read this
- anyway.
-
- ────────────────────────────────────────────────────────────────────────
- To show you why some bugs are caused and not easily fixed, consider this
- program:
-
- Var I, J : Integer; │ main()
- Begin │ { int i, j;
- I := 10000 * 100 div 50; │ i = 10000 * 100 / 50;
- J := 10000 div 50 * 100; │ j = 10000 / 50 * 100;
- Writeln( I, ' = ', J ); │ printf( "%d = %d\n", i, j );
- End. │ }
-
- The result should be that I is equal to J.
-
- Now a similar program:
-
- Var I, J, A, B, C : Integer; │ main()
- Begin │ { int i, j, a, b, c;
- A := 10000; │ a = 10000;
- B := 100; │ b = 100;
- C := 50; │ c = 50;
- I := A * B div C; │ i = a * b / c;
- J := A div C * B; │ j = a / c * b;
- Writeln( I, ' = ', J ); │ printf( "%d = %d\n", i, j );
- End. │ }
-
- Now the result is that I is not equal to J. Mathematicians usually tell
- computer programmers that it's crazy to have a language that does this.
- Yet we have to live with it. This has been a major cause of SRE bugs;
- many times nothing happens until the empires are large. (Replace 10000
- by 3 and the bug won't appear!) That makes debugging slow and painful
- because that kind of problem isn't something that is obvious late at night.
- Furthermore, if you try typing in "A*B DIV C" into the debugger, it gives
- the right answer, but when you run it, it won't give the right answer!
- ────────────────────────────────────────────────────────────────────────
-
- Conclusion: programming languages need a "SuperInteger" or something
- larger than LongInt so that SRE and other programs that
- work with large numbers will work correctly.
-
- Remember this when you write your own program and you can't figure out
- why in the world 1000 * 5000 / 1000 is not always 5000. Try rearranging
- the formula!
-
- Have fun! ;)
-
- Amit Patel, programmer at the Solar Realm
-
- ────────────────────────────────────────────────────────────────────────
- P.S. If you still haven't figured out why this happens, type in:
- I := 10000 * 100; │ i = 10000 * 100;
-
- and see what happens. 1 million won't fit into an integer, so
- it causes strange results (or compile time errors if you use numbers
- and not variables). Integers can store up to 32767, which is NOT
- enough to store how much money you have in SRE! Using "long" integers
- only delays the problem.
- ────────────────────────────────────────────────────────────────────────
-
-