Lecture 3

Floating Point Numbers

In many calculations the range of numbers is very large. For very large or small numbers, people use scientific notation.
Instead of writing: we write: Computers can use a similar notation, this is called floating point.
In general, a number n is represented as: f is called the fraction, or mantissa, and e is called the exponent. B is the base of the number system; 10 for decimal, 2 for binary.

Standardisation is better for floating point numbers than integers and most computers use the same representation (called I.E.E.E. 754).

There are two common formats for floating point numbers, single and double precision.

Single precision numbers are stored in 32 bits as shown below.

 __________________________________
|_|________|_______________________|
 |    \               \
 Sign  Exponent       Fraction
(1 bit)(8 bits)       (23 bits)
The sign bit gives the sign of the number as a whole (1 for negative).

The exponent is stored using excess 127 notation.

The fraction is stored in 23 bits and represents a decimal number from 1.0 to nearly 2.0.
The first bit in the fraction must always be one and is not stored.


 

Double Precision numbers are stored in 64 bits as shown below.

 ____________________________________________________
|_|___________|______________________________________|
 |     \                \
 Sign   Exponent         Fraction
(1 bit) (11 bits)        (52 bits)
The sign bit gives the sign of the number as a whole.

The exponent is stored using excess 1023 notation.

The fraction is stored in 52 bits.

        Item             Single        Double
 ________________________________________________
|___Bits in sign_____|_____1______|______1_______|
|___Bits in exponent_|_____8______|______11______|
|___Bits in Fraction_|_____23_____|______52______|
|___Bits, total______|_____32_____|______64______|
|___Exponent system__|_Excess 127_|_Excess 1023__|
|___Exponent Range___|_-126..+127_|_-1022..+1023_|
|___Smallest_________|___2-126_____|_____2-1022_____|
|___Largest__________|___2+128_____|_____2+1024_____|
|___Decimal range____|_10-38..10+38_|__10-308..10+308_|
Example: 0.5 is stored as 0x03f000000 in single precision f.p. (1x2-1 sign=0 exponent=126 fraction=0)

The floating point number line for single precision:

                Positive overflow
         Positive numbers        \    
   Positive underflow    \        \         
                    0\    \        \
-------|.  . . ..|--|--|.. . .  .|-------
 \         \       \
  \         \       Negative underflow
   \         Negative Numbers
    Negative Overflow
In C, these floating point numbers are called float (single precision) and double (double precision). There are a large number of mathematical functions that perform operations on floats and doubles.

e.g. .


Floating point calculations may be performed by using integer operations on the fraction and mantissa. However, most computers now have special hardware for performing f.p. operations. When the speed of a machine is measured, the results for integer and floating point calculations are usually presented separately.

Characters

Characters are stored as a 7 bit unsigned integer. The mapping from character to integer is defined by the American Standard Code for Information Interchange (ASCII).
                             Second Hex Digit 
        0|  1|  2|  3|  4|  5|  6|  7|  8|  9|  A|  B|  C|  D|  E|  F| 
     |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| 
 F 0 |NUL|SOH|STX|ETX|EOT|ENQ|ACK|BEL| BS| HT| LF| VT| FF| CR| SO| SI| 
 i   |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| 
 r 1 |DLE|DC1|DC2|DC3|DC4|NAK|SYN|ETB|CAN| EM|SUB|ESC| FS| GS| RS| US| 
 s   |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| 
 t 2 | SP|  !|  "|  #|  $|  %|  &|  '|  (|  )|  *|  +|  ,|  -|  .|  /| 
     |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| 
 H 3 |  0|  1|  2|  3|  4|  5|  6|  7|  8|  9|  :|  ;|  <|  =|  >|  ?| 
 e   |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| 
 x 4 |  @|  A|  B|  C|  D|  E|  F|  G|  H|  I|  J|  K|  L|  M|  N|  O| 
     |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| 
 D 5 |  P|  Q|  R|  S|  T|  U|  V|  W|  X|  Y|  Z|  [|  \|  ]|  ^|  _| 
 i   |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| 
 g 6 |  `|  a|  b|  c|  d|  e|  f|  g|  h|  i|  j|  k|  l|  m|  n|  o| 
 I   |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| 
 t 7 |  p|  q|  r|  s|  t|  u|  v|  w|  x|  y|  z|  {|  ||  }|   |DEL| 
     |---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
The representation for each character consists of 7 bits, and all 127 possible bit patterns represent valid characters.

The characters in positions 00 - 0x01f and position 0x07f are special control characters that are used for transmission, printing and control. Of these, 0x0c, 0x0d, 0x0a and 0x07f are most important.

The remaining characters are all printable, and include letters, numbers, punctuation, and a space.

The digits 0-9 appear in sequence, as do the upper and lower case letters. This organization simplifies character manipulation and comparison.

In order to change the character representation of a digit into its numerical value, we can subtract 0x030 from it. In order to convert the ASCII character `5,' which is in position 0x035, into the number 5, we compute 0x035 - 0x030 = 0x05.

In order to convert an upper case letter into a lower case letter, we add 0x020. To convert the letter `H,' which is at location 0x048 in the ASCII table, into the letter `h,' which is at position 0x068, we compute 0x048 + 0x020 = 0x068.

In C, characters are always stored using 8 bits. A character can be used in a C program by using either the ASCII code or enclosing the character in single quotes.

Strings

A string is a sequence of characters. In C, strings are stored in consecutive memory locations and are always terminated by a character with a code of 0 (NUL).