The standard internal representation (in a computer) is
to store the digits "314159" and the exponent "1" (the power
of 10 here), all of which takes less space.
This is called a floating point representation because
we can adjust the exponent to make the decimal point "float"
Another example: 42.93 = 0.4293 × 102,
which implies the digits "4293" and exponent "2".
Back to
double:
The
float
format reserves a fixed number of spaces for digits.
This limits accuracy.
When you perform mathematical calculations with
numbers represented in the
float
format, you can run into accuracy issues quickly.
This is why Java allows the accuracy to be doubled
using
double.
Hardly anyone uses
float.
anymore. We'll use
double.
Again, as with integers, the standard operators apply:
7.1 Exercise:
Write a program called
Area.java
to print out the area of a circle whose radius is
entered from the terminal. For the latter,
use the
IOTool.readDoubleFromTerminal()
method in
IOTool.java that you'll
need to download.
7.2 Exercise:
Can you find a well-known formula or law in science or engineering
that uses all four of the standard operators:
+, -, *, /? If not, find one that uses as many
as possible.
7.3 Exercise:
Do the increment and decrement operators work with
real numbers? Write a program called
TestIncrement.java
to find out. In this program, declare variables x and y of type
double,
setting them to x=1.1 and y=1.2;
then apply the increment (++) and decrement (--) operators to
x and y respectively.
Describe the output in
module7.pdf.
Now consider this simple example:
7.4 Exercise:
Change the assignment to:
and see what prints. Explain the result.
7.1 Real-valued variables in loops
Let's write a program to sum the numbers
0.1, 0.2, ..., 1.0.
Here's one way:
7.5 Exercise:
Use a table (in module7.pdf)
to trace the execution of the above loop.
Show the changing values of sum, x, and i.
7.6 Video:
We'll now examine a number of variations:
We can use real variables directly in the for-loop:
Here's the same example with shortcut operators:
Next, consider this product computation:
7.7 Exercise:
Do you recognize what the above loop is computing?
Edit, compile and execute
LoopExample4.java
to see the output.
7.8 Exercise:
In
MyLoopExample4.java
modify the above program to compute the product
of numbers 0.1 * 0.2 * ... * 1.0.
What do you observe in the output that is
inconsistent with the product in the previous
exercise? What is the explanation?
7.9 Audio:
7.2 An example with a nested loop
Let's write a program to explore this sum:
1 + (1/2) + (1/2)2 + ... + (1/2)n.
Let's do this in steps:
First, some pseudocode at the highest level:
sum = 0
for k=0 to n {
x = (1/2) raised-to-power k
sum = sum + x
}
Print sum
But (1/2)k = (1/2) * (1/2) * ... * (1/2) (k-times).
⇒ This is itself a loop.
Let's first write the inner loop for fixed k:
7.10 Exercise:
Try this out to see that it works, writing your
code in
NestedLoopExample.java.
We'll now put this into the larger loop for the sum.
7.11 Exercise:
Trace the execution using a table
(module7.pdf), with changing values
of sum, k, power and i.
7.12 Exercise:
Does this program work for the corner cases of
n=0 and n=1?
(yes)
7.13 Exercise:
Explore what you get for larger values of n.
Does there appear to be a limiting value for the sum?
7.3 Casting
Consider the following program:
7.14 Exercise:
What you suppose will be printed? Try it.
Then, change the program to try the assignment the
other way around: initially
assign the value of 1 to i and
then assign i to x.
An assignment from an int to a
double works fine:
About casting:
The assignment from int to double
works because every int is a valid double value.
However, a double need not be a valid int.
⇒ Which is why the compiler complains.
Recall: we faced the same issue in going from
int
to
char.
However, as we did in that case, we can force an assignment using an
explicit cast
The result: the largest integer less than the real.
⇒ The result is 1 above.
An explicit cast is need even if the double's
value happens to be an integer like 1.0.
As we'll see later, casting is a general operation
that can be applied to different variable types.
7.15 Exercise:
What do you get when you cast the real value
0.5 to
an int?
Write code in
TestCasting.java
to find out.
7.16 Audio:
7.4 Reading and writing
We'll now work on our code reading skills. The goal is
to read a more complex piece of code.
Consider this program:
We'll read this program in steps:
First, observe that a variable called sum
is declared, stuff gets added to it, and then it
gets printed:
Now let's read the outermost loop:
Here, we notice that a loop variable called x
"drives" the loop.
Note the start, end, and increment.
Make sure your eyes catch the closing brace (bracket)
of the outer loop.
So far, so good.
Next, look at the next level:
The loop "driver" is variable y.
Note the start, end and increment.
Observe also that y is used not only
in the calculation, but as part of the condition
for the innermost loop.
Next, we see that there is a variable declared
and used entirely within this loop:
(To save space, we've only shown the code inside main).
Here, you say to yourself: "There's a variable
that's initialized to 1, then it multiplicatively
accumulates stuff, and gets added to sum."
Now for the innermost loop:
This loop is driven by z.
Notice that the start value depends on the current
value of y.
Thus, each time we do the innermost loop will be
different because y changes.
When writing nested for-loops:
Write the first line of outermost loop and the closing brace:
Then, back up into the body and write the code inside:
... and so on.
7.17 Exercise:
Armed with your improved reading skills and added
knowledge of syntax, go back to
Module 0 and read
through the first primes program. Examine the
for-loops in particular - you should be able to
trace through them, even without understanding the rest.
7.5 When things go wrong
Since we're getting good at detecting syntax errors,
let's focus on logical errors, or errors in thinking.
7.18 Exercise:
What is the bug in this program written
to compute the sum of numbers 0.01, 0.02, ..., 0.1?
7.19 Exercise:
What about this one that's intended to compute
the same sum as in the exercise above? (We've shown only the code
inside main.)
7.20 Exercise:
What is the bug in this program written
to compute the product of numbers 1, 2, ..., 10?