Difference between revisions of "CSC231 Lab 8"

From dftwiki3
Jump to: navigation, search
(Exploring Floating-Point Numbers)
(Exploring Floating-Point Numbers)
 
(9 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
=Exploring Floating-Point Numbers=
 
=Exploring Floating-Point Numbers=
  
*Open [http://cs.smith.edu/~thiebaut/classes/231/floatingpoint.html Werner Randelshofer's Floating Point applet] (This applet is © 2001 Werner Randelshofer)
+
*Open [http://cs.smith.edu/~dthiebaut/classes/231/floatingpoint.html Werner Randelshofer's Floating Point applet] (This applet is © 2001 Werner Randelshofer)
  
* 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 #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: 2.What is the representation of 0? How about -0?
+
==Question #2==
  
* 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.
+
Do we have one or two different representations for 0?  What are the representations of 0 and -0?
  
* Question #4: What is the difference, in magnitude, between two consecutive FP numbers between 0 and 1?
+
==Question #3==
  
* Question #5: What is the difference, in magnitude, between two consecutive FP numbers with the largest positive exponents?
+
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.
  
* Question #6: What is the smallest non-zero normalized number that we can represent with this format?
+
Pi is 3.1415926535897932384626433832795...  What is the closest representation of Pi with a 32-bit floating-point format?
  
* Question #7: What is the smallest non-zero denormalized number that we can represent with this format?
+
==Question #4==
  
* Question #8: What is the representation of NaN?
+
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 #9: What is the magnitude of the largest number that is not infinity?
+
==Question #5==
  
* Question #10: What is the magnitude of the one before the largest number? What is the difference between the largest and next largest numbers, in absolute value?
+
What is the difference, in magnitude, between two consecutive FP numbers with the largest positive exponents?
  
* Question #11: 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 // ):
+
==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 // ):
  
 
<code><pre>
 
<code><pre>
Line 51: Line 72:
 
</pre></code>
 
</pre></code>
  
::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
+
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?
  
::What do you predict the output to be?
+
Run the program. What is its output? Why?
  
::Run the program. What is its output? Why?
+
==Question #11==
  
* Question #12: Try the same program in Python:
+
Try the same program in Python:
  
 
<code><pre>
 
<code><pre>
Line 74: Line 97:
 
            
 
            
  
::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 [http://www.lahey.com/float.htm The Perils of Floating Point], but Bruce M. Bush.
+
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 [http://www.lahey.com/float.htm The Perils of Floating Point], but Bruce M. Bush.

Latest revision as of 22:00, 2 December 2012

--D. Thiebaut 17:06, 5 December 2008 (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.