## Calculations ### Commands Explained

 Command Explanation Examples System.out.println displays the result of a calculation and/or some text on the screen System.out.println(3*3+4*4)      ==> 25 +  -  *  / add ,  subtract ,  multiply  ,  divide 4*5 - 6 / 2  ==>  17 Math.pow(b , e) calculates the power  b ^ e Math.pow(3,4)  ==> 81 parentheses (  ) use parentheses to control the order of operations 3*3 + 4*4 ==> 25 3*(3 + 4)*4 ==> 84

### Run the Program

Download Source Code, copy it into a Java IDE,
then run it and check that you get the same output as shown above.

### Factorial = Big Numbers

The first calculation, 1*2*3*4*5*6*7*8*9 is called factorial, abbreviated 9 !
Factorial is useful in probability (e.g. gambling and insurance) as well as physics.
These factorial numbers get very big very fast, so it's useful to have a computer
that does the calculations - they are too difficult by hand.

Question :  Can the computer continue to really big factorials?  For example, can it do 99 ! ?

Investigation
1. Change the first command to calculate 15!, e.g. 1*2*3...*14*15.
Run the program and write down the result.
2. Change the program to calculate 20!, e.g.  1*2*3*4...*19*20.
Run the program and write down the result.
There is obviously something wrong here.
3. Now take a pocket calculator, or use the calculator built in to your computer , or use Google ...
and get a more believable answer for 20 !  .  Write it down.
Explanation

Find out how numbers are stored in binary in a computer's memory.
Calculate the maximum value that can be stored in 32 bits.
Hence determine the maximum factorial value that can be correctly
calculated by this program, since Java stores integer values in 32 bits.
Describe what happens when the calculation gets too big, e.g. overflows.

### Division Problems

Division is a difficult arithmetic operation, so people make frequent mistakes.
The computer also makes mistakes, but for a different reason.

Question : Why (and when) does division give incorrect answers?

Investigation

Find out what the computer prints for each of the following calculations -
write the results in your notebook.

• 3+4/5
• (3+4)/5
• 3+4/5.0
• (3+4)/5.0
• 3.0/4.0/5.0
• 3.0/(4.0/5.0)

Explanation

Java treats integers (whole numbers) differently than floating point (decimal) numbers.
When dividing integers, Java does it like people would divide up a bunch of coconuts.
7 / 3 ==> divide 7 coconuts among 3 people.  We could give 2 coconuts to each person,
but there would still be 1 coconut left over.  It's pretty difficult to divide a coconut into
pieces, so we don't know what to do.  The extra probably doesn't go anywhere, just gets left out.
So  7 / 3 ==> 2 .  That is how Java does integer division.

When dividing decimals, Java does it correctly, giving a correct decimal answer.
So  7.0 / 3.0 ==>  2.3333333 .

And parentheses are necessary if we want addition to be done before division.
Java follows the normal math rules for order of operations.

So explain why  1/2 + 1/3 + 1/6  prints 0.

### Powers with Math.pow(b , e)

3^4 = 81  -  this can be calculated with  Math.pow(3,4);

Investigation

Design your own experiments to investigate the following:
• Find out whether the Math.pow function has the same problems as division.
That is, does it give different answers with integers than with decimals.
• Find out whether the Math.pow function can suffer an overflow error -
e.g. what happens when the answer is very big.

• Find out whether Math.pow has any specific limitations.
For example:
- can we use negative numbers in for b and e ?
- can we use small decimals (0.001) for b and e ?
- can we use zeroes for b and e ?
- what is  Math.pow(0 , 0)  ?

• Find out how Math.pow can be used to calculate square-roots.

Explanations

For explanations, consult the Processing Help Reference, or consult a Java textbook.

### Practice

1. Calculate the number of hours in one year.

2. Calculate 1/2 + 1/3 + 1/4 + ... + 1/9.

3. By guessing and calculating, find a solution to this problem:
X * X * X * X = 100
You only need to get an approximate answer with 2 decimal places correct.

4. A computer works at a speed of 2 GigaHertz - that means that it can do
2 BILLION (2 000 000 000) calculations per second.
Calculate the number of calculations it can do in 1 minute.

5. Write a command that CORRECTLY calculates a value for 20 !