

If you don't already know, I guess I'll be the first to tell you:
integers in computers aren't the same as integers in mathematics and floating point
numbers in computers aren't the same as real numbers in mathematics. Our computers can
only handle so much at a time and mathematicians are far more facile with infinities
and/or immense finite sets and fields.
Integers
As computers inherently like to see it, integers have a very limited
range of values. 16-bit computers usually limit their natural view to about 65,536
different integer values in all. Sometimes, they are designated to all be positive,
spanning from 0 to 65535. Sometimes they are designated to a somewhat balanced
spread from -32768 to +32767. In Quick BASIC, for example, it's the more balanced spanning
that is chosen, by design. Other languages often allow you a choice in this matter. As you
might imagine, computers with different basic natures, such as 32-bit computers and 64-bit
computers, will allow their integers a broader span and 8-bit computers and even narrower
span.
Often enough, computer languages will seek to extend this natural
size a bit. So you'll find that Quick BASIC, for example, adds the LONG variable type to
its INTEGER type. Being an unnatural type for the computer running in 16-bit mode, the
LONG variable is simulated under Quick BASIC and takes more time to use. As always, there
are trade-offs to be understood and accepted.
Floating-point is another one of those trade-offs.
Floating point
Integers in most computer languages have one obvious limitation,
almost by design: they don't handle fractional values. Handling US dollars is an
easy example, where keeping track of pennies means needing a fractional part. Of course,
you could always use integers for that by simply saying that you are tracking pennies, not
dollars. And that works, too. But there are many more cases which push you right back into
needing those fractional values, again.
There is another, perhaps somewhat less obvious limitation: the dynamic
range of integers is very limited. Even if all you wanted were integers, what if you
needed integer values starting at a trillion? Or spanning a range of values from
micrometers to kiloparsecs? Even if you could live without fractional values, you might be
hampered by the fact that integers are usually pinned near zero and spread only a short
distance around it.
Although both these problems can be handled with combinations of
integers, writing the code for that and maintaining it and extending it and documenting it
is a nightmare. Worse, nearly every programmer will need to solve this problem for their
work and chances are that with a million programmers you will get a million different
implementations.
Making floating point an integral part of a computer language was
vital. It had to be done.
What is it, then?
Floating point mimics scientific notation, basically.
I f you recall, scientific notation uses two parts to a value: some
number between 1 and 10, excluding 10 itself, multiplied by some power of 10, such as
4.5*10^5 or -1.101*10^(-2). The base of the power, 10, is kind of a fixture and is always
present. Scientific notation solves both problems mentioned before, fractional values and
a wide dynamic range. Using scientific notation, it's as easy to write the size of an
amoeba in parsecs as it is to write the size of a galaxy in nanometers. It's a wonderful
system.
Floating point notation in computers seeks to borrow from this
experience. It also uses two types of values in a special combination that mirrors how
scientific notation does it's magic. And the result is a very handy system. In fact, so
handy that most folks never need look any deeper.
Floating point also has its own dark side, as well. It is often
slower, it frequently takes up more memory, and it can produce very confusing results.
Some of the confusing results are legend: two variables holding what appear to be exactly
the same value do not compare as equal in an IF statement, for example. See Clarifying Floating
Point, for example. There are many other special circumstances that are well beyond
the scope here. But integers often don't have these problems. Using floating point is not
a panacea for all the problems you may encounter. But it is a very good tool to use for a
broad range of problems.
Last updated: Friday, July 09, 2004 13:19