Allowance

   Download Source Code



Practice

  1. The original allowance was 860 per year.  Find the correct weekly allowance to combine
    with 160 Euro December bonus to produce exactly 860 Euros per year.
     
  2. Kim's brother Jim receives 80 Euros per month, but no December bonus.
    Calculate the yearly total for Jim.
     
  3. Lois Lane and Clark Kent have 9 children. 
    The first child, Oney, receives 1.11 Euros per day, plus 11.11 bonus in December.
    The second child, Tui, receives 2.22 Euros per day, plus 22.22 bonus in December.
    The third child, Threep, receives 3.33 Euros per day, plus 33.33 bonus in December.
    This continues up to the ninth child, Ninny, who gets 9.99 per day plus a 99.99 bonus.
    Write ONE CALCULATION to calculate the total allowance for ALL the children,
    assuming there are 365 days in one year.

Scientific Notation

For very large floating point  (decimal) values, we can use Scientific Notation.
For example, in Chemistry a Mole is defined as  6.02 x 1023 molecules.
We could write this as: 6.02*10*10*10 .... 10 , repeating 23 tens. 
But it's simpler to write:  6.02e23 .
The number 6.02 is called the mantissa.  The number 23 is the exponent.

We can also do arithmetic with these Scientific Notation numbers. 
For example:   6.02e23 * 2 ==>  12.04e23
But Java will always normalize the result, writing the mantissa so that it has
just one number before the decimal point.  Then the exponent must also change.
So:   12.04e23 ==>  1.204e24.

You might not need to use such big numbers, but you might be surprised when
Java uses Scientific Notation to print a result. 
For example :  1000.00 * 1000 * 1000 * 1000 ==> 1.0e12
It's easy to change the Scientific Notation back to a normal number.
The exponent 12 tells us to move the decimal point 12 spots to the right.
That means you need to add some zeroes.
1.0e12 ==>  1 000 000 000 000  =  1 trillion

Scientific notation is the most general representation for a float (floating point) value.

Investigation

Explanation

Java float values only have 8 decimal place accuracy.  So when something like
6/11 is stored, it can be stored as  0.54545454.  There should be lots more repetitions of 54,
but there is no space and the rest is truncated (chopped off).  Or it might get rounded off,
which makes it closer to being correct, but still not 100% correct.  In fact, the actual value
produced by 6/11.0 is 0.54545456, which isn't even rounded off correctly.  Once the number
is stored in the memory, the storage error is permanent. In a long calculation,
the truncation errors will occur after each part of the calculation,
and may result in a noticeably incorrect answer.  But if you are lucky,
rounding errors in one part of the calculation may balance out with truncation
errors in another part, and produce a very accurate answer at the end.


Questions

  1. Which is largest : 2e3 ,  3e2 , 32e1 , 123
     
  2. How do you write 10*100*1000*10000 in scientific notation?
     
  3. Why does the computer calculate 1/10.0 correctly,
    but it calculates 1/3.0 incorrectly?
     
  4. Find the largest possible value that can be written as a float in Scientific Notation.

  5. We can write a "d" after a Scientific Notation value, to force it to use double storage.
       println( 9e99d + 8e98d );

    That allows us to write larger numbers.  Find out the largest value that can be
    written as a Scientific Notation double value.

  6. In an ideal world, with perfect computers, we would expect all the following
    commands to print the same answer:

    println(1/7f + 1/7f + 1/7f + 1/7f + 1/7f + 1/7f + 1/7f);
    println(1/7d + 1/7d + 1/7d + 1/7d + 1/7d + 1/7d + 1/7d);
    println(7 * (1/7) );
    println(7d * (1d/7d) );
    println(7d * (1f/7f) );

    Run these commands and explain the different results.

  7. Which type of numbers do you think is best - integer, float or double?