Floating Point Numbers
- Floating point numbers are MUCH more complex than integers.
- Between 1 and 2 are no other integers.
- Between any two real numbers are infinitely many real numbers.
- It is easy to see a maximum integer (as we did in the last section)
- Floating points are much different.
- We represent floating point numbers much like scientific notation.
- $[+/-]d.d \times 10^{e}$ : Scientific notation
- $[+/-]1.n \times 2^{e}$ : floating point numbers.
- The standard is actually IEEE 754
- With integers we decided to two's compliment, we use sign/magnitude for floating point numbers
- sign 1.significant x 2^ exponent
- The sign is a single bit.
- The significant is between 11 and 237 bits.
- The exponent is between 5 and 19 bits.
- There are special cases for numbers that are very close to 0
- A simple example.
- This is a simplified version of ieee 754, don't worry too much about details.
- THIS IS NOT AN ACCURATE REPRESENTATION.
- THERE ARE MISSING DETAILS.
- Assume 4 exponent bits.
- Assume 3 decimal bits.
- The first bit is a sign bit.
- The next 4 bits are exponent.
- The next 3 bits are decimal
- 0 0000 000
- +, 0, 0
- + 1.0 x 2^0 or 1.0
- 1 0000 100
- -, 0, 1/2
- - 1.1 x 2^0
- -1.5
- 0 0001 100
- +, 1, 1/2
- + 1.100 x 2^1
- or 11.0 x 2 = 3.
- Our biggest number would be 0 0111 111
- + , 7, 1/2 + 1/4 + 1/8
- 1.111 x 2^7
- $11110000_2 = 240$
- Our next biggest number would be 0 0111 110
- + , 7, 1/2 + 1/4
- 1.110 x 2^7
- $11100000_2 = 224$
- Note there is a space between the biggest number and the next biggest.
- This is called the gap.
- It is a round off error problem.
- There is no way to represent numbers that fall in the gap.
- Floating point is slow.
- How do you add numbers in scientific notation?
- $1.23\times10^4 + 2.01\times10^2$
- Note that floats can be used to represent
- Numbers very close to 0: $1.0\times2^{-100}$
- Very large numbers : $1.0\times2^{300}$
- This has been a very quick look at floating point.
- Floating point has the same limitations as integer
- But a much more complex representation
- So it can be much slower.
- And an additional round off error problem.
- Always chose an int if you can.