Floating-Point Binary Fractions
Date: 07/19/99 at 15:05:21 From: Amad Mairaj Subject: Computers and Math I have a question. Since computers think in terms of "1" and "0" how do they calculate and represent fractions such as 12.93? How can you represent this number in binary? For example how would 123.99 + 12.938 be added in binary? I am confused and would appreciate clarification. Regards, Amad P.S. Love your website, best site in the WORLD!
Date: 07/19/99 at 17:12:03 From: Doctor Peterson Subject: Re: Computers and Math Hi, Amad. You can find some information about this sort of thing in our FAQ on bases; look for discussions of fractions: http://mathforum.org/dr.math/faq/faq.bases.html There are two ways to represent non-integer numbers in binary. One is called "fixed-point"; you just pretend there's a "binary point" at a certain place in the number. Then, for example, "10101" would represent 101.01 = 5.25 (1*4 + 0*2 + 1*1 + 0*1/2 + 1*1/4). More commonly today, numbers are stored in "floating point," where some of the bits are used to indicate where the binary point is. This is similar to scientific notation; 5.25 might be represented as something like "101010e0010", which would mean 1.0101 * 2^2. This is somewhat simplified, but should give you a general idea of how it works. If you'd like more details, write back! - Doctor Peterson, The Math Forum http://mathforum.org/dr.math/
Date: 07/21/99 at 16:48:28 From: (anonymous) Subject: Re: Computers and Math FPU or Floating Point Unit), hmmm... Can you clarify what 'floating-point' means, in easy-to-understand terms? And how does it work with respect to fractions? Computers without FPUs, such as early 286s, were also able to do fractions. How did they differ from CPUs with FPUs? Thanks, Amad
Date: 07/21/99 at 23:25:23 From: Doctor Peterson Subject: Re: Computers and Math Hi, Amad. Yes, the FPU is a part, in or alongside the CPU, that does floating-point computations. In early hardware, the CPU could only work with integers, so these calculations had to be done explicitly by software, which would use ordinary bit-manipulation instructions to multiply, convert to integers, and so on. Later, separate chips were made that would communicate with the CPU, accepting requests to do these calculations on their own, and eventually they were integrated into the CPU chip itself. Each step made floating-point calculations faster. Floating point is essentially the binary equivalent of scientific notation. It simply means that we store the digits of the number and the location of the decimal (or binary) point separately, so that the point "floats," allowing us to represent numbers of different sizes without having to put the same number of digits after the point in every number. In scientific notation in base ten, we first scale a number so that it is between 1 and 10, and then multiply that by the appropriate power of ten: 123.45 = 1.2345 * 10^2 = 1.2345e2 In binary, we can write a number as the product of a number between 1 and 2, and a power of two: 5.25 base 10 = 101.01 = 1.0101 * 2^2 = 1.0101e10 (The exponent 2 is 10 base 2; each power of two shifts the binary point right one place.) A computer works with such numbers the same way we work with scientific notation. To multiply, you multiply the fraction parts and add the exponent parts. To add, you have to convert them to use the same exponent, then add the fraction parts. As I indicated in general terms, a floating-point number is stored as a string of bits, say 32 of them, with each set of bits assigned a different purpose. The first bit is typically a sign bit, which is set (1) to indicate a negative number. Then several bits (8 in a single-precision number) store the exponent, with 127 added to it for reasons I won't go into. Finally, the rest of the bits (23 in single-precision) are used to store the fractional part; since this is always between 1 and 2, the ones bit is always 1 and doesn't have to be stored. So our number 5.25 looks like this: S|E E E E E E E E|F F F F F F F F F F F F F F F F F F F F F F F 0|1 0 0 0 0 0 0 1|0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 | \_____________/ \___________________________________________/ | | | positive exp 2+127 = 129 frac 1.0101 -> 0101 If you're interested in details, here are some of many sites that tell about the standard floating point format: IEEE floating point representations of real numbers - John David Stone http://www.math.grinnell.edu/~stone/courses/fundamentals/IEEE-reals.html IEEE Standard 754 Floating Point Numbers - Steve Hollasch http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html - Doctor Peterson, The Math Forum http://mathforum.org/dr.math/
Search the Dr. Math Library:
Ask Dr. MathTM
© 1994-2015 The Math Forum