780.20: 1094 Session 1
Handouts: Background notes for Session 1;
.cpp printouts; Recommended C++ options;
GSL intro
Your goals for today (note: the goals are always over-ambitious!):
- Get used to the wxDev-C++ environment (even if you plan
to use Linux later)
- Download, unzip, compile and run some simple C++ programs,
then modify and repeat
- Understand how underflow, overflow, and machine precision
limits can be determined "experimentally"
- Set up Dev-C++ compiler warnings and library
- Try out a program that uses a function from
the Gnu Scientific Library (GSL)
- Use MATLAB or Mathematica to verify a result
Please work in pairs with comparable computing experience.
The instructors will bounce around 1094 and answer questions (don't be
shy to ask about anything!). Hand in this sheet at the end of class.
Dev-C++
The first step is to log on, download and unzip some C++ "source code",
and then start up Dev-C++.
- Log on with your Physics Department Windows account.
(If you don't know your username and/or password, please let one of the
instructors know.)
- We recommend that you use a shared disk set up for Physics 780.20.
To do so, click on the Start menu and right-click on My Computer.
Select "Map Network Drive" and type \\phypcs\physics 780
for the Folder. Select a drive letter (Z: is a good choice).
Now you can create a working directory ("folder") on this drive with
your name (or make up a name for your group). Be sure to
copy the folder to your personal space(s) (on the U: disk) at the end.
- Start up a web browser ("Firefox" is recommended but you can use "Internet
Explorer" as well). Go to the 780.20 home page
(http://www.physics.ohio-state.edu/~ntg/780/) and find the "1094
Sessions" section. Download session01.zip and unzip it
into your 780 folder (double clicking on session01.zip in an Explorer window,
then "Extract all files" is one way to do this) or just
open the .zip file with Winzip.
- Start up Dev-C++ by looking under Start->All Programs->wx-devcpp.
A Dev-C++ window should open up. Answer "ok" to all of the
questions (set up may take a couple of minutes). Now we're ready
to roll.
- Load the program area.cpp by one of these three methods:
- From the menus: File->Open Project or File
- Shortcut: Ctrl+O (hold down the "Ctrl" and "O" keys together)
- Icons: Click the 2nd icon on the 1st row of icons (point at it
with your mouse to get a momentary title)
There are (at least) three ways to do anything in Dev-C++, analogous
to these. Use the window that pops up to track down the area.cpp
file and press "open".
- The program should
have appeared in the big window. Questions about
the code? (If you are new to C++, you're not expected to understand
everything; see the discussion in the background notes for some
details.) Why use "area" and "radius" for variables rather than "A"
and "R", which are quicker to type?
- Let's compile, link, and run it.
Use the 4th icon on the 2nd line ("Compile & Run") or F9 or
Execute->Compile & Run. You should get a little status box that
says it is compiling and then a
pop-up window.
Enter a radius and it disappears before you can do anything!
To make the window stay open, add the line system("PAUSE");
before the return 0; line. Then try again.
- Modify the program to create some errors and see what
Dev-C++ does to get used to error messages.
Some possibilities (try these then invent your own):
- remove a semi-colon
- comment out using namespace std; (using //)
- remove a } (or change it from "}" to ")" or "]")
Each time, "compile and run" again. If there is an error, a section at
the bottom of the Dev-C++ window should pop up and give an error
message with the line number it thinks is a problem and an explanation
(which is often not helpful!). Fix the error and make sure the program
still works. Note that "undeclared"
errors are reported as being at lines that are incorrect. Why?
- List two ways to verify that the program is
running correctly (i.e., giving correct answers):
Later (not in class today!) you may be asked to
carry out the "to do" list in the comments (with assistance, as needed).
Which do you know how to do now?
- Copy, rename, and modify area.cpp to make a program
volume.cpp, which calculates
the volume of a sphere. Test it. [Warning: Most people
get this wrong the first time. Test carefully!]
- Start up MATLAB.
For the experts:
Write a program ("script") to duplicate what area.cpp does.
For everyone else: Load area.m, look at it in the editor,
and run it.
Think about the comparison with the C++ version; which is better
to use?
(There is also a Mathematica area.nb notebook
to look at.)
Overflows, Underflows, and Machine Precision
Refer to the background notes on number representation as you do this
section.
- For this section, we need to turn off the default Dev-C++
optimization. Go to
Tools->Compiler Options->Settings->Optimization->Further optimizations
and set "Best Optimization" to "No".
-
If you're know C++ already and you're really fast, you can
try writing your own codes. Otherwise, use "flows.cpp".
(Look at the code before running it! Where does the output go?
You can use ConTEXT (recommended) or Notepad to look at it.)
A single-precision number in C++ is a "float" and a
double-precision number in C++ is a "double".
You are to empirically determine (put your answers in the blank
spaces)
- where under- and overflow occur for single-precision
floating-point numbers;
- where under- and overflow occur for double-precision
floating-point numbers.
- Now for the machine precision.
Once again, code your own
version or use "precision.cpp".
[Note: This code has an intentional bug. Compare to
flows.cpp to track it down.]
Determine empirically
- the machine precision for single-precision
floating-point numbers;
- the machine precision for double-precision
floating-point numbers. [Note: you have to change
the code for this to work correctly.]
Explain in a couple of sentences how the code finds
the machine precision (note that there is an output file):
Setting g++ Compiler Options
See the handout with the list of recommended C++ options.
We'll set some using the Dev-C++ menus.
- In Dev-C++, go to Tools->Environment Options->Files &
Directories->User's Default Directories. Change this to
your 780 directory (use icon at right).
- Go to Tools->Compiler Options->Compiler. [Also look under
the Programs tab.] Add the following to "calling compiler":
-Werror -Wall -W -Wshadow -fno-common
Add the following to "linker" (for using GSL):
-lgsl -lgslcblas
Using the GSL Scientific Library
A link to the GSL documentation can be
found on the 780.20 web page. Here we look at our first
example program, which has been adapted
from the documentation.
- We'll first create a Dev-C++ "Project", which is a collection of
files and instructions associated with a program.
Create a New Project (first icon on second
row or File->New->Project). Select "Console Application", "C++
Project", name it "J0_test", and save it in your 780/session_01
folder. The project files will be shown in the window at the upper
left. Note that main.cpp was created.
- Load the real main file
J0_test.cpp" by Project->Add to Project.
Then remove main.cpp by Project->Remove from Project (don't
save the changes).
- This test program calculates the cylindrical Bessel
function J0(x) at x=5 using the GSL library function
"gsl_sf_bessel_J0" (you would find out the name of the function by looking
at the web page with the GSL reference manual).
Look up "Bessel function Wikipedia" using Google. How can
you use this to check you are calculating the right thing?
-
Compile and run J0_test.cpp and
verify that the correct answer (given in the file)
is printed. Look at the "Compile Log" tab at the bottom. This is what
you would have typed to compile and link!
- Now calculate for x=3
(modify the code for this value or add code to read in any x).
Answer:
- Verify your answer using MATLAB or Mathematica.
If MATLAB, under the Help menu, select "MATLAB Help", choose
"Search Results", and search for "Bessel Functions".
If Mathematica, under the Help menu,
start the Help Browser, choose "Master Index", and look under Bessel
function).
Did you succeed?
Copying Your Session to Another Account
If you were logged into your partner's account, you should make
your own copy of everything you did.
The easiest way to get your own copy is to work on the share disk
and then you can copy the working directory to your own account
(after logging in).
780.20: 1094 Session 1.
Last modified: 08:16 am, March 24, 2008.
furnstahl.1@osu.edu