Integer Arithmetic
The computer relies on arithmetic. Here we test a variety of arithmetic
operations expressed as 32 bit two's complement binary numbers (Java's
int).
| eg.ArithmeticFixture |
| x |
y |
+ |
- |
* |
/ |
|
| 0 |
0 |
0 |
0 |
0 |
error |
Integer divide exception is expected |
| 1 |
2 |
3 |
-1 |
2 |
0.5 |
The divide non-compare is expected |
| 1 |
-1 |
0 |
2 |
-1 |
-1 |
|
| 10000 |
10000 |
20000 |
0 |
100000000 |
1 |
|
| 100000 |
100000 |
200000 |
0 |
10000000000 |
1 |
|
| 1000000 |
1000000 |
2000000 |
0 |
1000000000000 |
1 |
|
The divide by zero exception and the error just under it are
expected. The stack trace is probably excessive, though. The error
under it is caused by using integer arithmetic: the quotient
will always be an integer, so it cannot compare equal to 0.5.
Also, notice that some of the numbers are larger than you
would expect from 32 bit integer arithmetic. That's because
I changed them for the Python version; Python has unlimited
precision integers, and there was no way to duplicate the
hash Java makes of integer overflows.
Now we try something similar using automatic type conversion offered by ColumnFixtures.
| eg.ArithmeticColumnFixture |
| x |
y |
plus() |
times() |
divide() |
floating() |
|
| 2 |
3 |
5 |
6 |
0 |
0.666667 |
|
| 0 |
0 |
0 |
0 |
error |
error |
|
| 0 |
0 |
0 |
0 |
|
|
|
| 200 |
300 |
500 |
60000 |
0 |
0.666667 |
|
| 2 |
3 |
10 |
10 |
10 |
|
Expected Errors |
| 200 |
3 |
5 |
6 |
0 |
0.666667 |
Expected Errors |
| 2 |
-3 |
-1 |
-6 |
-1 |
-0.666667 |
The expected result for the integer divide
was changed from 0 to -1 because of Python's
use of "floor division." |
Several of the errors in this one are planned; the
incorrect values were there originally to show off
the nice red background for an error.
"error" is a value that will compare to an exception;
that's what the green background with the "error" message
is in the third row. What the ones with the white background
are is another question: I'd expect an exception since there
wasn't any value there originally, but it seems to put
"error" in there anyway.
Document prepaired by Ward Cunningham
First Version July 11, 2002
Last Update August 17, 2002
Notes added by John Roth April 22, 2004