Using my TS2068 and VU-CALC, I was setting up a spread-sheet format in which one row of cells was to calculate and display the integer value of a percentage of another number that might be either positive and negative.
I soon found out that my idea of the integer value of a negative number was different to what a computer will produce.
To check this out, I wrote the following program and input several values. The result from two of those inputs is printed after the program listing.
When using integer values, we usually want the integer value that is ‘less than’ the number and its decimal appendage, but in the sense that the integer value is closer to zero. Hence we expect the integer of x.1 to be x, and the integer of-x.1 to be -x.
When you examine the printed results of my test program, you’ll see that the computer isn’t progammed to think like us in this regard. It sees the integer value of a number with a decimal appendage as the next SMALLER whole number. Hence it will give you x as the integer of x.1, but will give you -(x+1) as the integer of -x.1, i.e. the integer of -2.1 is -3.
Technically the computer is correct. -3 is the next whole number that is smaller than -2.1.
Generally, we will also want a number and its decimal appendage to be rounded off from .5 up to the next whole number that is FURTHER from zero, and from less than .5 down to the whole number that is CLOSER to zero.
Note that the formats shown in Lines 130 and 150 does accomplish this, although when handling negative numbers, not in the same manner as we think.
I hope all this will be of some use to you when working with the integer and/or rounded-off values of negative numbers.
10 INPUT A
20 PRINT "THE VALUE ENTERED WAS ";A
30 PRINT
40 PRINT "INT (A)= ";INT (A)
50 PRINT "INT -(A)= ";INT -(A)
60 PRINT "INT (-A)= ";INT (-A)
70 PRINT "-INT (A)= ";-INT (A)
80 PRINT "-INT -(A)= ";-INT -(A)
90 PRINT "-INT (-A)= ";-INT (-A)
100 PRINT
110 PRINT "ROUNDING OFF TO NEAREST WHOLE NUMBER"
120 PRINT
130 PRINT "INT (A+.5)= ";INT (A+.5)
140 PRINT "INT -(A+.5)= ";INT -(A+.5)
150 PRINT "INT (-A+.5)= ";INT (-A+.5)
160 PRINT "-INT (A+.5)= ";-INT (A+.5)
170 PRINT "-INT -(A+.5)= ";-INT -(A+.5)
180 PRINT "-INT (-A+.5)= ";-INT (-A+.5)
THE VALUE ENTERED WAS 1.4 INT (A)= 1 INT -(A)= -2 INT (-A)= -2 -INT (A)= -1 -INT -(A)= 2 -INT (-A)= 2 ROUNDING OFF TO NEAREST WHOLE NUMBER INT (A+.5)= 1 INT -(A+.5)= -2 INT (-A+.5)= -1 -INT (A+.5)= -1 -INT -(A+.5)= 2 -INT (-A+.5)= 1 THE VALUE ENTERED WAS 1.6 INT (A)= 1 INT -(A)= -2 INT (-A)= -2 -INT (A)= -1 -INT -(A)= 2 -INT (-A)= 2 ROUNDING OFF TO NEAREST WHOLE NUMBER INT (A+.5)= 2 INT -(A+.5)= -3 INT (-A+.5)= -2 -INT (A+.5)= -2 -INT -(A+.5)= 3 -INT (-A+.5)= 2
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.