CINT/CLNG Integer Assignment Round x.5 to Nearest Even Integer

This article was previously published under Q28855
This article has been archived. It is offered "as is" and will no longer be updated.
(Note that Microsoft QuickBasic for MS-DOS, versions 3.0 and earlier have no long integer format or CLNG function)

When a numeric expression ending in .5 is assigned to an integervariable, a compiled Basic program will round the expression to thenearest even integer. For example, .5 converts to 0, 1.5 converts to2, 2.5 converts to 2, and 3.5 converts to 4.

This rounding to the nearest even integer occurs for the CINT and CLNGfunctions and for an integer division assigned to an integer or longinteger variable. This behavior is a feature of the IEEE FloatingPoint Standard.
More information
This type of integer rounding complies with the following IEEEstandard:
"If the difference between the unrounded operand and the rounded result is exactly one half, the rounded result is even" (Section 5.5 of the "IEEE Standard for Binary Floating-Point Arithmetic")
The purpose of this behavior is to prevent an average upward (ordownward) bias as various calculations are rounded. If the number isalways rounded up, there would be an upward bias in calculations.Rounding to the nearest even number averages out; therefore, norounding bias occurs.

The compilers listed above store and manipulate numbers using IEEEformat. This exclusive use of IEEE format for real numbers isnecessary to enable compiled Basic to CALL routines written inFORTRAN, Pascal, and C, all of which use IEEE format.

QB.EXE (noncoprocessor) version 3.0 and QuickBasic for MS-DOS,versions 1.0, 1.01, 1.02, 2.0, and 2.01 all store numbers in MicrosoftBinary Floating Point format (also known as MBF). Note that QB87.EXE,the coprocessor version of QuickBasic for MS-DOS, version 3.0, usesIEEE format. QuickBasic for MS-DOS, versions 1.x, 2.0, and 2.01 do notsupport IEEE (or 8087, 80287, or 80387) coprocessors.

Microsoft Binary format uses a standard different from that of IEEEfor converting between floating point and integers. In particular,numbers with "5" as the least significant digit are always rounded upto the next integer. The result does not have to be an even number.Thus, the Microsoft Binary format has an upward rounding bias.

Note that QuickBasic for MS-DOS, versions 3.0 and earlier cannot makeinterlanguage CALLs to FORTRAN, Pascal, or C.

Listed below are two examples of the above rounding behavior. Toexecute these examples in VBDOS.EXE use the following steps:

  1. From the File menu, choose New Project.
  2. Copy the code example to the Code window.
  3. Press F5 to run the program.

Example 1

The following is an example of always rounding expressions ending in.5 to an even number by integer assignment:
   DEFINT A-Z   INPUT "Type a whole number (1,2,3,4,5,6,...)",INUM   IRESULT=INUM/2   PRINT "If INUM/2 ends in .5, it rounds/truncates to even number:"   PRINT IRESULT				

Example 2

The following is an example of rounding of the CINT() function:
      a=.5      b=1.0      c=1.5      d=2.0      e=2.5      cls      print "CINT (0.5) = "; CINT(A)      PRINT "CINT (1.0) = "; CINT(B)      PRINT "CINT (1.5) = "; CINT(C)      PRINT "CINT (2.0) = "; CINT(D)      PRINT "CINT (2.5) = "; CINT(E)				
OUTPUT FROM: B.EXE 4.0 later | B.EXE 3.0, 2.01, 2.0
CINT (0.5) =           0           |         1CINT (1.0) =           1           |         1CINT (1.5) =           2           |         2CINT (2.0) =           2           |         2CINT (2.5) =           2           |         3				
VBmsdos QuickBas

Article ID: 28855 - Last Review: 10/26/2013 10:57:00 - Revision: 3.0

Microsoft Visual Basic 1.0 Standard Edition, Microsoft BASIC Professional Development System 7.0, Microsoft BASIC Professional Development System 7.1

  • kbnosurvey kbarchive KB28855