Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC124 Assignment 1 due this Friday, 7pm.

Similar presentations


Presentation on theme: "CISC124 Assignment 1 due this Friday, 7pm."— Presentation transcript:

1 CISC124 Assignment 1 due this Friday, 7pm.
Fall 2018 CISC124 2/24/2019 CISC124 Assignment 1 due this Friday, 7pm. Quiz 1 Next Week! (posted slides have been updated to include the following information on the quiz.) Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

2 Quiz 1 Topics Everything up to and including Thursday’s lecture.
Up to and including Numeric Representation, but nothing after that. Exercises 1, 3 and 4 are fair game. Fall 2018 CISC124 - Prof. McLeod

3 Quiz 1 Topics, Cont. Emphasis on: Java history and operation.
Primitive types, variable declaration and initialization, expressions, conditionals and loops. Type casting of primitive types. Screen (or "console") output using System.out.println() and System.out.printf(). Screen input using the Scanner class. Arrays – one dimensional. Methods and attributes – use, declaration and coding. Numeric Representation. Fall 2018 CISC124 - Prof. McLeod

4 Quiz 1 Format Electronic – in onQ.
40 minutes + 5 minute “grace” period. T/F and short answer. Short answer could be a code segment, a complete method, or possibly a complete program. Code writing will be towards the end of the quiz – leave yourself enough time!!! You will need to be able to read and write procedural Java code. Fall 2018 CISC124 - Prof. McLeod

5 Quiz 1 Format, Cont. The quiz shows that it is open only for the Monday lab between 8:30 and 10:30am. When the Monday lab is over the quiz will be re-opened for the Tuesday 8:30 lab, and so on. If you start the quiz after 9:50am on Monday, for example, you will not get the full available time to write – the quiz will close on you at 10:30am regardless. The quiz can only be opened in JEFF155 – it is IP address restricted to only the lab machines. Fall 2018 CISC124 - Prof. McLeod

6 Quiz 1 Format, Cont. When you type code directly into a multi-line answer box, avoid using the <tab> key. This key will not give you a tabbed space but will just move the cursor out of the box, which is really annoying. Just use the spacebar. Style is not critical, but nice looking code is easier to read and grade by an old guy like me… Fall 2018 CISC124 - Prof. McLeod

7 Quiz 1 Rules You may not use an IDE, or any other programs on any device. No other aids. The TA must be able to see your screen. No talking to other students when writing. If you have written, please keep the noise down until all other students have finished writing the quiz. Fall 2018 CISC124 - Prof. McLeod

8 Quiz 1 Rules, Cont. Do not discuss the quiz with students who have not written it. Do not provide quiz answers to students who have not written it. The latter half of the lab is available for assignment help. Fall 2018 CISC124 - Prof. McLeod

9 Today Continue Numeric Representation: Wrapping of integer types.
IEEE 754 standard for floating point numbers. Extended Precision. Machine Epsilon. Fall 2018 CISC124 - Prof. McLeod

10 Storage of Integers - Cont.
Suppose we wish to add 1 to the largest byte value: This would be equivalent to adding 1 to 127 in base 10 - the result would normally be 128. In base 2, using two’s compliment, the result of the addition is , which is -128 in base 10! So integer numbers wrap around, in the case of overflow - no warning is given in Java! Fall 2018 CISC124 - Prof. McLeod

11 Storage of Integers - Example
Example – calculate n! as an int and as a long. What happens when overflow occurs? How do you calculate a large factorial? How about the BigInteger class! See FactorialDemo.java Fall 2018 CISC124 - Prof. McLeod

12 Storage of Real Numbers
The system used to store real numbers in Java complies with the IEEE standard number 754. Like an int, a float is stored in 4 bytes or 32 bits. These bits consist of 23 bits for the mantissa, 8 bits for the exponent, with one sign bit: exponent mantissa sign bit Fall 2018 CISC124 - Prof. McLeod

13 Storage of Real Numbers - Cont.
The sign bit, s, can be 0 for positive and 1 for negative. The exponent, e, is unsigned. The standard says the exponent is biased by 127 and that the values 0 and 255 are reserved. So the exponent can range from -126 to E is the unbiased value and e is the biased value (E = e - 127). If e is not a reserved value then assume that there is a 1 to the left of the decimal point, which is followed by the mantissa, f to yield the “significand”. The mantissa is always less than 1 this way. Fall 2018 CISC124 - Prof. McLeod

14 Storage of Real Numbers - Cont.
So a value is stored as: value = (-1)s  1.f  2E For example if s = 0, e = 125 and f = E = 125 – 127 = -2 The significand is value = 1   2-2 Fall 2018 CISC124 - Prof. McLeod

15 Storage of Real Numbers - Cont.
Or, value = 0.011, which is The maximum float would be for : s = 0 e = 254 f = We can use a calculator like: Fall 2018 CISC124 - Prof. McLeod

16 Storage of Real Numbers - Cont.
Gives: E38, which is the constant: Float.MAX_VALUE. Changing s to 1 gives E38 The smallest, non-zero normalized value has e = 1 and f = , which gives E-38, which is Float.MIN_NORMAL. Fall 2018 CISC124 - Prof. McLeod

17 Storage of Real Numbers - Cont.
These are all “normalized” numbers because we are not using the reserved exponent values, 0 and 255. A “denormalized” (or “subnormal”) number has e = 0. This implies a 0 to the left of the decimal point. So: value = (-1)s  0.f  2-126 Fall 2018 CISC124 - Prof. McLeod

18 Storage of Real Numbers - Cont.
This allows the range of float values to be extended to slightly lower values (to 1.4  , Float.MIN_VALUE). If e and f are 0, then you get -0 or +0 depending on s. If e is 255 and f is 0 then you get –Infinity or +Infinity depending on s. (Displayed to the console as –inf or inf. If e is 255 and f is not 0, then you get NaN. Shown as nan. Fall 2018 CISC124 - Prof. McLeod

19 Aside – Why Denormalized Numbers?
Without these small numbers, if you made a float any smaller than 1.2E-38, you would “flush to zero”. The smaller increments of denormalized numbers allows a more “gradual” underflow. So “they” say… Fall 2018 CISC124 - Prof. McLeod

20 Storage of a Real Number, Cont.
double, (8 bytes) roughly ±4.9 x to 15 significant digits. The IEEE754 standard states how this number is constructed in memory: These 8 bytes consist of 52 bits for the mantissa, 11 bits for the exponent, with one sign bit: exponent mantissa sign bit Fall 2018 CISC124 - Prof. McLeod

21 Storage of a Real Number - Cont.
The sign bit, s, can be 0 for positive and 1 for negative. The exponent, e, ranges from to (or 0 to 2047 in base 10) The exponent, e, is unsigned. The IEEE754 standard says the exponent is biased by 1023 and that the values 0 and 2047 are reserved. So the exponent, E, can range from to in base 10. Fall 2018 CISC124 - Prof. McLeod

22 Storage of a Real Number - Cont.
For example if s = 0, e = (104010) and f = … E = – = 1710 The significand is … value = 1  …  217 or value = , or e5 Fall 2018 CISC124 - Prof. McLeod

23 Storage of a Real Number - Cont.
The maximum double would be for : s = 0 E = f = … (52 ones) or …  21023 Using the calculator: Gives: e308, Double.MAX_VALUE. Fall 2018 CISC124 - Prof. McLeod

24 Storage of a Real Number - Cont.
The smallest double would be for : s = 0 E = f = … (52 zeros) or …  Gives: e-308 Double.MIN_NORMAL Fall 2018 CISC124 - Prof. McLeod

25 Storage of a Real Number - Cont.
Smallest possible denormalized double: s = 0 e = 0 f = … (51 zeros followed by a one) Gives: 4.9E-324, which is Double.MIN_VALUE Fall 2018 CISC124 - Prof. McLeod

26 Special Values Note that you cannot refer to NaN, -Infinity or Infinity in code unless you use a special constant. These constants: NaN POSITIVE_INFINITY NEGATIVE_INFINITY are all available from both the Float and Double wrapper classes. Fall 2018 CISC124 - Prof. McLeod

27 When Bad Things Happen! The IEE754 standard requires the following behaviour when calculations go wrong: Invalid Operation Provide NaN Division by Zero Provide +Infinity or -Infinity Overflow Provide the largest possible normalized value or ±Infinity Underflow Provide smallest possible normalized number, a denormalized number or ±0 Inexact Value Provide the nearest rounded value Fall 2018 CISC124 - Prof. McLeod

28 When Bad Things Happen, Cont.
The standard also states that the language must somehow set a flag (like throwing an exception, for example), when something like this happens. Java does not do this – floating point operations never throw exceptions! However, Wrapper classes supply methods to detect NaN and Infinity. So, it is up to you (the programmer!) to check your calculations and prevent “bad things” from happening. Fall 2018 CISC124 - Prof. McLeod

29 IEEE754 Standard See the following web sites for more info:
Or: Fall 2018 CISC124 - Prof. McLeod

30 Floating Point Numbers are Not Real!
A floating point number can only occupy a finite amount of storage in memory. Mathematical formulae assume that numbers are continuous – but computer values are discrete. This effect is also important for two kinds of numbers: Numbers like 0.1 that can be written exactly in base 10, but cannot be stored exactly in base 2. Real numbers (like  or e) that have an infinite number of digits in their “real” representation can only be stored in a finite number of digits in memory. Fall 2018 CISC124 - Prof. McLeod

31 Aside - Extended Precision
For example, the Windows calculator can provide 32 accurate digits. It is using an “arbitrary precision mathematical library” (see the Wikipedia entry on this topic, for example). It is the CPU that may have built-in support for 10 or 12 byte numbers. Most C++ compilers support the “long double” type, which uses 12 bytes. .NET programs can take advantage of extended precision calculations. Fall 2018 CISC124 - Prof. McLeod

32 From the MSDN: Understanding Extended Precision:
“Extended Precision, a new feature of Calculator, means that all operations are accurate to at least 32 digits. Calculator also stores rational numbers as fractions to retain accuracy. For example, 1/3 is stored as 1/3, rather than However, errors accumulate during repeated operations on irrational numbers. For example, Calculator will truncate pi to 32 digits, so repeated operations on pi will lose accuracy as the number of operations increases.” Fall 2018 CISC124 - Prof. McLeod

33 Aside - Extended Precision, Cont.
Java does not support this in the same way as .NET, but it can use extended exponents to prevent overflow or underflow, unless the keyword strictfp is specified for a class or method. Fall 2018 CISC124 - Prof. McLeod

34 Aside - strictfp This is a Java modifier that can be used in either class or method headers. When it is used Java will store all intermediate calculation values according to the IEEE754 standard. Otherwise if extended precision is available, it will be used for intermediate values, and only the final result will be rounded according to the IEEE754 standard (when it is stored in a normal double precision register). strictfp is used by the StrictMath class which is used by the Math class, for example. Fall 2018 CISC124 - Prof. McLeod

35 Roundoff Error – Cont. Compute: And, compare to 1000. float sum = 0;
for (int i = 0; i < 10000; i++) sum += 0.1f; System.out.println(sum); Fall 2018 CISC124 - Prof. McLeod

36 Roundoff Error – Cont. Prints a value of 999.9029 to the screen.
If sum is declared to be a double then the value: is printed to the screen. So, the individual roundoff errors have piled up to contribute to a cumulative error in this calculation. As expected, the roundoff error is smaller for a double than for a float. Fall 2018 CISC124 - Prof. McLeod

37 Aside - BigDecimal Class
For when you need a floating point numeric type that does not have any limit to the number of digits you can store (how much RAM you have!). However, it is usually a good idea to specify the scale of the numbers you wish to use. See SumPointOne.java. Why not use BigDecimal for everything? Fall 2018 CISC124 - Prof. McLeod

38 Machine Epsilon - ε ε is the largest positive value that when added to 1 still produces 1, ie: 1 + ε == 1 Let’s find out what this value is for floats and doubles (see DetermineEpsilon.java). Fall 2018 CISC124 - Prof. McLeod

39 Machine Epsilon – ε, Cont.
For float: E-8 For double: E-16 If you are subtracting two numbers and the difference between the numbers is less than ε, then you will get zero. If you are adding two numbers, n1 and n2, and n2/n1 < ε, then you will get n1. Fall 2018 CISC124 - Prof. McLeod


Download ppt "CISC124 Assignment 1 due this Friday, 7pm."

Similar presentations


Ads by Google