Written by Alipha |
Integers are more than 3 times faster than Floating-Point numbers in most instances. Also integers take up 1/2 as much memory as Floating-Point numbers. And yet, they are the default data type that QBasic uses. Why?
What are Integers and Floating-Point Numbers?First, however, what they are. An integer is a whole number, that can be either negative or positive. A Floating-Point number is one that supports numbers with a decimal portion. It can also be used to approximate very large or very small numbers. Some examples would be:
4.53Note that 523.0 is the same as 523, which is an integer. Though because it is being stored in a floating-point variable, it has a different format, and also slower, than if 523 was stored in an integer data type.
How Much Slower?Well, here is a table. I compared the speed that it took 2 integers to preform an operation 10,000,000 times to the speed it took 2 floating-point numbers. The operations I preformed were addition (+), subtraction (-), multiplication (*), floating-point division (/), integer division (\), modulo (MOD), and Exponentiation (^). The difference between floating-point division and integer division is that the decimal portion is not calculated in integer division (it is truncated).
Integer (in seconds) | Floating-Point (in seconds) | What's Faster | How Many Times Faster | + | 6.26 | 24.11 | Integer | 3.85 |
- | 6.27 | 24.06 | Integer | 3.84 | * | 6.70 | 24.17 | Integer | 3.61 |
/ | 63.00 | 25.54 | F-Point | 2.47 | \ | 7.74 | 86.51 | Integer | 11.18 |
MOD | 7.80 | 86.73 | Integer | 11.12 | ^ | 120.84 | 81.29 | F-Point | 1.49 |
As you can see, Floating-Point was only faster at Floating-Point Division (of course it would be) and Exponentiation. Though Floating-Point is still quite useful. It allows us to use decimal points in our numbers and to represent very large or small numbers with Scientific Notation.
How Do I Use Integers Instead of Floating-Point?There are a few ways:
#1: You can add the % to the end of variable names, just like you add $ to the end of string variables.#2: You can use DIM:
DIM myVariable AS INTEGERNote that if you use DIM, you wouldn't, and couldn't, put a % onto the end of the variable name.
#3: There is DEFINT. DEFINT changes the default data type of all variables beginning with the letters specified to integer. If you want all variables to be integer, by default (you can change them to something else on a one by one basis with method #1 and #2), then you'd use:DEFINT A-Z
Personally, I prefer to have DEFINT A-Z at the beginning of my program, since (usually) most of my variables are integers.Other Data Types
There is basically just integers, floating-point, and string variables, but integers and floating-point can both be divided into further. Integers can be divided into integers and long-integers. The two floating-point data types are single-percision floating-point and double-percision. Here is a table pointing out their differences:
Data Type | Symbol | DEFtype | Size | Maximum | Minimum | INTEGER | % | DEFINT | 2 bytes | 32,767 | -32,768 |
LONG-integer | & | DEFLNG | 4 bytes | 2,147,483,647 | -2,147,483,648 | SINGLE-percision | ! | DEFSNG | 4 bytes | ±3.4028E+38 7 digits |
±1.4013E-45 |
DOUBLE-percision | # | DEFDBL | 8 bytes | ±1.7977D+308 15 digits |
±4.9407D-324 | STRING | $ | DEFSTR | 1 byte/char | 32,767 chars | 0 chars |