Difference between revisions of "CSC231 Lab 8 2010"

From dftwiki3
Jump to: navigation, search
(Created page with '--~~~~ ---- =Exploring Floating-Point Numbers= *Open [http://cs.smith.edu/~thiebaut/classes/231/floatingpoint.html Werner Randelshofer's Floating Point applet] (This applet is…')
 
(Question #11)
 
Line 115: Line 115:
  
 
<br />
 
<br />
[[Category:CSC231]][[Category:Lab]]
+
[[Category:CSC231]][[Category:Labs]]

Latest revision as of 08:45, 13 December 2010

--D. Thiebaut 13:44, 13 December 2010 (UTC)



Exploring Floating-Point Numbers

Question #1

Do all decimal numbers with a non-zero non-integer part convert exactly to binary using the IEEE format? In other words, is it possible to have a number which in decimal is of the form dd.ddd, but that, when transformed to binary, requires an infinite number of bits?

Question #2

Do we have one or two different representations for 0? What are the representations of 0 and -0?

Question #3

What is the largest number of decimal digits that we can represent with this format? In other words, if the binary representation of 1.23456789 is different from the representation for 1.23456780, then we have at least 9 decimal digits of accuracy. Try different lengths of decimal digits past the decimal point.

Pi is 3.1415926535897932384626433832795... What is the closest representation of Pi with a 32-bit floating-point format?

Question #4

What is the smallest difference, in magnitude, between two consecutive FP numbers between 0 and 1? In other words, take a number such as 0.5, what is the smallest number greater than 0.5 that has a floating point representation different from that of 0.5. Then, what is the difference between these two numbers?

Question #5

What is the difference, in magnitude, between two consecutive FP numbers with the largest positive exponents?

Question #6

What is the smallest non-zero normalized number that we can represent with this format?

Question #7

What is the smallest non-zero denormalized number that we can represent with this format?

Question #8

What is the representation of NaN?

Question #9

What is the magnitude of the largest number that is not infinity?


Question #10

Using Linux, create a file called testFloat.cpp with your favorite editor, and enter the C++ code below (You do not need to enter the lines that start with // ):

      // testFloat.cpp
      //
      // A simple test of floating point numbers in C++
      // To compile and run:
      //     g++ -o testFloat testFloat.cpp
      //     ./testFloat
      //
      #include <iostream>

      using namespace std;

      main() {

        float x = 0.1;

        if ( x*10 == 1.0 )
          cout << "x is equal to 0.1\n";
        else
          cout << "x is not equal to 0.1\n";
      }

Study the program: you should be able to figure out what it does. The "cout << string" statements are statements that output strings to the screen. The "\n" symbol represents the carriage-return character to go to the next line. Predict its output

What do you predict the output to be?

Run the program. What is its output? Why?

Question #11

Try the same program in Python:

      # testfloat.py
      # to run the program:
      #
      #     python testfloat.py
      #

      x = 0.1
      if x * 10.0 == 1.0:
          print "x is equal to 0.1"
      else:
          print "x is not equal to 0.1"


Comment on the output of the program. Very good answers to this strange behavior can be found in http://docs.python.org/tut/node16.html on http://docs.python.org, and in The Perils of Floating Point, but Bruce M. Bush.