division problems - web viewdescribe what happens when the calculation gets too big, e.g....

10
Calculation Homework Exercise 1: copy and paste the code to see what the result is 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)

Upload: haduong

Post on 06-Feb-2018

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Division Problems -    Web viewDescribe what happens when the calculation gets too big, e.g. overflows. ... Design your own experiments to investigate the following:

Calculation HomeworkExercise 1: copy and paste the code to see what the result is

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

Page 2: Division Problems -    Web viewDescribe what happens when the calculation gets too big, e.g. overflows. ... Design your own experiments to investigate the following:

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.

The result would be 2004310016

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.

The result would be -2102132736

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.

The result would be 2329020080000000000

Explanation

Find out how numbers are stored in binary in a computer's memory.

Binary code in a computer is 0 and 1 as computers use electricity which is “ON” and “OFF”

Calculate the maximum value that can be stored in 32 bits.

The maximum value of binary columns would be 5, but the biggest individual number this column would have would be 16.

Hence determine the maximum factorial value that can be correctly calculated by this program, since Java stores integer values in 32 bits.

(2^32) -1 is the largest integer number is the maximum value that can be stored.

Describe what happens when the calculation gets too big, e.g. overflows.

Page 3: Division Problems -    Web viewDescribe what happens when the calculation gets too big, e.g. overflows. ... Design your own experiments to investigate the following:

An overflow is the numerical value it too big to be represented within the storage available. As a result of overflow, the least important representable bits are stored and this is called wrap. Another result in processors like the GPU and the DSP, the result of overflow would be saturation once the maximum value is reached.

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 The result from the program is 3 (3+4)/5 The result from the program is 1 3+4/5.0 The result from the program is 3.8 (3+4)/5.0 The result from the program is 1.4 3.0/4.0/5.0 The result from the program is 0.15

Page 4: Division Problems -    Web viewDescribe what happens when the calculation gets too big, e.g. overflows. ... Design your own experiments to investigate the following:

3.0/(4.0/5.0) The result from the program is 3.75

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.

Page 5: Division Problems -    Web viewDescribe what happens when the calculation gets too big, e.g. overflows. ... Design your own experiments to investigate the following:

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.

It prints 0 because it doesn’t have a complete number for a result, thus just leaves it 0. However, if we have the same equation with .0 after each number, then the answer will be 1.0. Thus, it finds 0 because it doesn’t work it out correctly without the .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?

Math.pow function doesn’t suffer with the same problem because even though you give integers it still gives a result of a decimal. Also, when the answer is a decimal, it shows the whole number.

Page 6: Division Problems -    Web viewDescribe what happens when the calculation gets too big, e.g. overflows. ... Design your own experiments to investigate the following:

Find out whether the Math.pow function can suffer an overflow error -e.g. what happens when the answer is very big. 

Math.pow does contain an overflow error: when a “massive” number is inserted, the result comes out as ‘Infinitive’. This means that it cannot show the whole number, therefore having

an overflow error.

Find out whether Math.pow has any specific limitations.For example:- can we use negative numbers in for b and e ?

Negative numbers can be used:

Page 7: Division Problems -    Web viewDescribe what happens when the calculation gets too big, e.g. overflows. ... Design your own experiments to investigate the following:

- can we use small decimals (0.001) for b and e?

Small decimals can also be used:

- can we use zeroes for b and e?- What is Math.pow(0 , 0)  ? Zeroes can be used.

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

The result would be as follows:

Page 8: Division Problems -    Web viewDescribe what happens when the calculation gets too big, e.g. overflows. ... Design your own experiments to investigate the following:

Explanations

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

Processing Help Reference

Practice

1. Calculate the number of hours in one year.

 

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

Page 9: Division Problems -    Web viewDescribe what happens when the calculation gets too big, e.g. overflows. ... Design your own experiments to investigate the following:

 

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 do2 BILLION (2 000 000 000) calculations per second. Calculate the number of calculations it can do in 1 minute.

The answer is too big of a number, thus processing had an overflow error

 

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

Page 10: Division Problems -    Web viewDescribe what happens when the calculation gets too big, e.g. overflows. ... Design your own experiments to investigate the following: