CPSC120A
Fundamentals of Computer Science I

Lab 21

Binary Real

Use the command line to create a new directory called lab21 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

Binary Real

In Emacs, create a text file called binary_real.txt. In that file answer the following questions on binary real numbers.

  1. What is the binary number 0. 01 in decimal?

  2. What is the the binary number 1010. 0101 in decimal?

  3. What is the decimal number 1. 5 in binary?

  4. What is the decimal number 14. 5625 in binary?

  5. What is the binary number 1. 0 × 101 in decimal?

  6. What is the binary number 0. 10 × 1010 in decimal?

  7. For each of the following numbers, specify whether it can be represented exactly with a python float value.

    1. 1 / 3

    2. 1 / 4

    3. π

    4. 910

    5. 91000

    6. 1234567890. 0

    7. 12345678901234567890. 0

  8. What is the result of evaluating the expression 0.3 == 0.1 + 0.2 in Python?

  9. What is the result of evaluating the expression round(2.675, 2)?

Base Conversion

Create a Python function called to_decimal(binary_real) in a file called binary_real.py. The function should have one parameter, binary_real, that is a string that represents a binary real number. The function should return a float that is the equivalent of the binary string. Assume that the binary_real string contains only one period character and any number of zero and one characters. Be sure to include test cases for your function.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only programs that have perfect style and flawless functionality will be accepted as complete.