Floating Point Representation in Computers

Jul 21, 2024

Understanding Floating Point Representation in Computers

Introduction

  • Discussion on floating point numbers and their representation in computers.
  • Common issues encountered with floating point numbers in computations.

Decimal vs Binary Numbers

Decimal Numbers Example

  • Decimal number example: 302 = 3 * 10^2 + 0 * 10^1 + 2 * 10^0 = 300 + 0 + 2

Binary Numbers Example

  • Binary number example: 11111 in binary = 1 * 2^4 + 1 * 2^3 + 1 * 2^2 + 1 * 2^1 + 1 * 2^0 = 16 + 8 + 4 + 2 + 1 = 31 in decimal.

Converting Decimal to Binary

Steps to Convert Decimal Integers

  1. Take the remainder when dividing the number by 2. This gives the least significant bit.
  2. Divide the number by 2 using integer division.
  3. Repeat the process until the number is reduced to zero.

Example: Converting 19 to Binary

  • 19 in decimal = 10011 in binary.
  • Code logic:
    • Flag negative numbers.
    • Use the absolute value for conversion.
    • Accumulate results based on remainders and divisions.

Handling Floating Point Numbers

Converting Decimal Fractions to Binary

  • Example: 0.375 in binary.
  • Steps:
    1. Multiply the fraction by a power of 2 to convert it into a whole number (e.g., 0.375 * 2^3 = 3).
    2. Convert the resulting integer to binary (3 = 11 in binary).
    3. Divide the binary integer by the same power of 2 (shift right by 3 places).

Code for Conversion

  • Code provided to convert decimal to binary fractional part.
  • Example: 0.375 (decimal) converted to binary is 0.011.
  • Example: 0.333 (decimal) converts to a more complex binary representation.

Implications and Considerations

  • Floating point numbers may not have exact representations in binary.
  • Testing equality of floating point numbers should use a tolerance method (e.g., abs(x - y) < epsilon) rather than direct comparison (x == y).
  • Python rounds floating point numbers for display purposes to provide expected results.

Conclusion

  • Understanding binary representation helps in dealing with floating point inaccuracies.
  • Careful handling of floating point comparisons is essential for precise computations.