# Floating Point Numbers

In many calculations the range of numbers is very large. For very large or small numbers, people use scientific notation.
591020000000000000
we write:
5.9102 x 1017
Computers can use a similar notation, this is called floating point.
In general, a number n is represented as:
f x Be
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.

```#include <stdio.h>

void main() {
float f;
unsigned *u;

f=0.5;
u=(unsigned *)&f;    // here we cunningly convert a float
printf("%x \n",*u); // into an integer to see how it is 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.

```#include <stdio.h>

void main() {
double f;
unsigned long long *u;

f=0.5;
u=(unsigned long long *)&f; // here we cunningly convert a double
printf("%Lx \n",*u);       // into a long long to see how it is stored

} ```
```        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. .

```#include <stdio.h>
#include <math.h>  // always remember to include this
// or strange things may happen
void main() {
double theta,r,length;

theta=0.462;
r=27.48;
length= r * sin( theta );
printf("The length is %f\n",length);

} ```

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).
`             ASCII Character codes`
```                             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.

0x0c: Form feed (Clear screen on a terminal)
0x0d: Carriage Return
0x0a: Line Feed
0x07f: Delete
The remaining characters are all printable, and include letters, numbers, punctuation, and a space.
```#include <stdio.h>

void main() {
int i;

for(i=32;i<128;i++) // print some characters
printf("character %d is '%c'\n",i,i);

} ```

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).
```#include <stdio.h>

int stringtono(char str[]) { // turn a string into a number
int i,no=0;

for(i=0;str[i]!=0;i++)             // for every char in the string
if(str[i]>='0' && str[i]<='9')  // if it is a digit
no=no*10+str[i]-'0';         // add it to the end of the number
return no;
}

void main() {
char phone[]="4418169"; // this is an array of chars
char *paper="a59102";   // this is also an array of chars

printf("My phone number is %d\n",stringtono(phone));
printf("My office is %d\n",stringtono("SC1.40"));
printf("This paper is %d\n",stringtono(paper));
} ```