Suite101

Types of integers, floating point

Numerical data types in computer programming

© Guy Lecky-Thompson

Abacus, sxc.hu
A discussion of the common numerical data types available to computer programmers for manipulating data, and some of the common constraints on their use.

Introduction

One of the most common computer programming tasks usually invloves calculations or numerical manipulation of some kind. Numbers also provide an insight into data storage and representation issues that computer programmers often come up against. They can also cause some confusion.

So, we shall be looking at three main areas:

  • Numerical data types
  • Representing numerical data types
  • Storing numerical data

This article is also designed to be language-independent, but assumes some basic knowledge of computer programming. As an introduction to programming principles in general, we recommend : Computer Programming 101

Integers

An integer is a whole number. 3 is an integer, as is -1042. Generally speaking, programming languages allow for integers that are:

  • Reasonably small (0 - 255)
  • Moderately Large (0 - 64k)
  • Large (64k and above)

Integers can also be signed (be positive or negative) or unsigned (can only be positive). A language such as C assumes that all numbers are signed unless otherwise specified, using the 'unsigned' keyword.

Due to the representation of an integer, there are several influences on the range of numerical values that can be stored in the numerical data type. For example, an SQL TINYINT will be stored as a single byte, rather like the C 'short int'.

One byte can contain a number from 0 to 255, using the hexadecimal format that the reader should be familiar with. However, if a signed value is required, this is reduced to -127 to +127, with the high bit being used to store the sign, thus removing the possibility to represent value greater than 127 or less than -127.

To store larger numbers, more bytes need to be allocated. The exact number of bytes available will depend on the language and/or architecture. Commonly, however, 2 bytes are used for the normal range of integers (0 to 64k, or -32k to +32k), and 4 bytes for anything larger.

Floating Point Numbers

Floating point numbers are more complex to store, and as such, the exact details are beyond the scope of this discussion. However, it is important to remember that the end data will be a combination of 0 or 1 bit values, essentially integer storage. So, each floating point value will be stored in pieces using a specific encoding regimen.

The most common requires that each value is stored in three parts:

  • The mantissa - fractional part
  • The scale - where to put the decimal point
  • The sign - positive or negative

Typically, 32 bits are used in total, with 23 bits being allocated to the mantissa (otherwise known as the significand), 8 for the scale (or exponent), and the most significant bit being used to indicate a negative value.

To store a value, it need to be normalized. This means that we need to take the value (say 142.45), and calculate the exponant by moving the decimal point to the left until a single digit remains. So, we end up with 1.4245 and an exponent of 2. These values are easily stored, using teh scheme mentioned above.

So, a floating point value is usually represented by 32 bits (4 bytes) or 64 bits (8 bytes). These are known as single and double precision values.

Programming Considerations for Storing Numbers

To store numbers (in files, or memory, for example), the programmer sometimes needs to make a desicion based on available space and the ranges of number needed to be stored. To store a large number of very small integers, it might not matter whether they are stored as characters or binary data.

For a large number of very imprecise or very small floating point numbers (each one less than 4 bytes wide : 12.34 for example) it might be more efficient to store them as character strings.

It is generally accepted, however, that storing numbers as their native binary is the most efficient approach, and that when using a programming language such as C which allows for choice, binary storage is more effective.

Further Reading

The following articles may also be of interest:


The copyright of the article Types of integers, floating point in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Types of integers, floating point in print or online must be granted by the author in writing.



Post Your Comment
2500 characters left
NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
What is 0+3?


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo