780.20: 1094 Session 1
Handouts: Background notes for Session 1;
area.cpp/make_area; Recommended C++ options; "Useful Unix Commands";
Your goals for today (note: the goals are always over-ambitious!):
- Get used to the Linux environment (e.g., window manager, editor,
- Download, unpack, compile and run some simple C++ programs,
then modify and repeat
- Try out Mathematica
- Understand how underflow, overflow, and machine precision
limits can be determined "experimentally"
- Try out a makefile: using g++ compiler warnings
- Try out a program that uses a function from
the Gnu Scientific Library (GSL)
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!).
The first step is to get accustomed to the Linux environment. Go at
your own pace.
Use a terminal window and type at the command line for today; later you
can use a file browser if you want.
- If your computer is in Windows, logoff and "restart".
After the Dell screen, there will be a countdown --- hit any key to
At the Grub
prompt, use the arrows to highlight "Fedora Core" and hit return.
Your login account is your Department unix account (see instructor if
you need one).
- Right click on background for a menu with "Open Terminal". Start up
one or two terminal windows.
- Create a directory for 780 ("mkdir 780"), go to that directory
("cd 780"). [Take a look at the unix command summary sheet.]
- Figure out how to
launch a web browser (e.g., Firefox has an "earth with mouse" icon),
find the 780 homepage
download session01.tarz into the 780 directory.
Unpack it with "tar xfvz session01.tarz", which should create the
session_01 subdirectory with some files in it.
Go to the session_01 subdirectory.
- Editor: use vi, emacs, pico, or nedit (I suggest using nedit if you don't
know any of these) to look at the simple program area.cpp.
E.g., type "nedit area.cpp &". (The "&" runs the editor in the
"background" so the terminal is still available.) 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?
- Compile and link area.cpp to create area using
"g++ -o area area.cpp"
(the executable would be called a.out if you left out "-o area").
Run it by typing "area" (or "./area" if you get an error
message about not finding the file; why does this work?).
Add "<< endl" to the end of the output line (starting "cout ...")
and recompile and rerun.
- How can you verify that the program is running correctly?
Later (not in class today!) you may be asked to
carry out the "to do" list in the comments (with assistance, as needed).
Do you know how to do these things now?
- Copy and modify area.cpp to make volume.cpp
("cp area.cpp volume.cpp"), which calculates
the volume of a sphere. Test it.
- Start up Mathematica. If you already know some Mathematica,
try to write a function to calculate the area given the radius, and
return a numerical answer.
For the experts:
Can you write a "program" that asks for the radius like the C++
If you don't know how to do these things, or after you've done them,
load area.nb (File -> Open), run the commands,
and try to understand the definitions.
Overflows, Underflows, and Machine Precision
Follow along in the background notes on number representation as you do this
- You are to empirically determine (put your answers in the blank
If you're really fast and know C++ already, you can
try writing your own codes. Otherwise, use "flows.cpp".
(Look at the code before running it!)
A single-precision number in C++ is a "float" and a
double-precision number in C++ is a "double".
- where under- and overflow occur for single-precision
- where under- and overflow occur for double-precision
- Now determine empirically
Once again, code your own
version or use "precision.cpp".
Explain to your partner in a couple of sentences how the code finds
the machine precision.
- the machine precision for single-precision
- the machine precision for double-precision
Using a Makefile
See the handout with the printout of the makefile "make_area" (on the
back of the area.cpp printout).
Makefiles are useful tools to organize the code for computational
They contain instructions for how to build an executable (what files
have the code, what options to use in compiling, what libraries to link
Here we have only one source (.cpp) file, but the makefile
keeps track of all the g++ options.
- Look at make_area in an editor. We'll go through the details of
a makefile later; for now note the following:
- Comments start with "#" and continuation lines
are indicated by "\" (so "SRCS= \" and "area.cpp" is all on one line).
- The list of options defined by "WARNFLAGS" are options to g++
(see handout for details).
- Run the makefile using "make -f make_area". It will try to
compile area.cpp and then link to create area. Check that it works.
- Try making a copy of make_area ("cp make_area make_flows") and
edit it so that it compiles and links flows.cpp.
Using the GSL Scientific Library
Links to the GSL documentation can be
found on the 780.20 web page. Several example programs have been
taken from the documentation and converted to C++.
- Examine the file "J0_test.cpp" in
an editor. 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).
- Compile J0_test.cpp according to the directions in the file and
verify that the correct answer is given. Try a different x value
(or modify the code to read one in).
- Try to verify your answer using Mathematica (under the Help menu,
start the Help Browser, choose "Master Index", and look under Bessel
Copying Your Session to Another Account
If you were logged into your partner's unix account, you might want to have a
your own copy of everything you did. Here's one way to copy a directory
(and subdirectory) to another unix account, using "secure copy" or
- If you are currently in the directory "session_01", go up one
directory using "cd .." (the two dots mean the directory immediately
above). You should now be in the directory you originally created
- If your username is "myname", then you can transfer the entire
session_01 directory to your account with the command:
scp -pr session_01 myname@fox:
Answer "yes" if asked about connecting and then enter your password
The "p" and "r" are options to scp that preserve the date and
permissions of the file (this is the "p") and transfer all
subdiretories recursively (this is the "r"). Don't forget the @ or
the : at the end. This will create the directory "session_01" on
the top-level directory on the account "myname" (with the same name).
- This is a handy command for file transfers; we'll look at it
more extensively later on.
780.20: 1094 Session 1.
Last modified: 03:36 pm, January 03, 2006.