========================== Spring 06 CS61C Homework 5 ========================== -------------------------------- Floating and Fixed Point Numbers -------------------------------- Update ------ * 2/28/06: In Question 2, treat the number as unsigned fixed number. * 2/28/06: In Question 5, the input ``num`` can be negative. The output type should therefore be ``int`` instead of ``unsigned``. The code has been updated to reflect the change. Corresponding Reading --------------------- * P&H 3.5, Lecture Notes on Fixed Point Numbers In this homeworkd, we use the notation ``fixed`` to denote a ``w`` bits fixed point number with binary point at position ``b`` from the least significant bit (counting from 0). For example, ``fixed<32,3>`` denotes a 32-bit fixed point number, of which 3 right most bits are fractional. Problems -------- Question 1 ~~~~~~~~~~ P&H Exercise 3.30. Question 2 ~~~~~~~~~~ What does the bit pattern from Question 1 represents for a ``fixed<32,3>``? What does the bit pattern from Question 1 represents for a ``fixed<32,32>``? What does the bit pattern from Question 1 represents for a ``fixed<32,0>``? Question 3 ~~~~~~~~~~ Show the IEEE-754 binary representation for the following decimal floating-point numbers: (a) 20.5 (b) -2.625 (c) 3 * 2\ :sup:`-145` (d) -0 (e) +0 Question 4 ~~~~~~~~~~ Show the binary representation for the following fixed-point ``fixed<8,3>`` numbers. Truncate bits that are not representable. (a) 20.5\ :sub:`ten` (b) 2.33\ :sub:`ten` (c) 0.6875\ :sub:`ten` OPTIONAL: What is the result if "even rounding" instead of truncation is used. Question 5 ~~~~~~~~~~ Complete the following C-function that converts a floating point number ``num`` into a fixed point number of type ``fixed<32,bp>``. You may truncate all bits that are not representable.: :: int Float2Fixed(float num, int bp) { int result; /* your code goes here */ return result; } For example, ``Float2Fixed(5.5, 2)`` should return the integer 22 with bit pattern: :: 0000000000000000000000000000000000010110 Submission ---------- Your solution to Question 1 to 4 should be saved in a file ``hw5.txt``. Your solution to Question 5 should be saved in a file named ``hw5q5.c``. From the directory that contains these two files, run: :: submit hw5