CISC124 Assignment 1 due this Friday, 7pm.

Slides:



Advertisements
Similar presentations
Binary Arithmetic Binary addition Binary subtraction
Advertisements

Floating Point Numbers
Assembly Language and Computer Architecture Using C++ and Java
Signed Numbers.
CSE 378 Floating-point1 How to represent real numbers In decimal scientific notation –sign –fraction –base (i.e., 10) to some power Most of the time, usual.
Floating Point Numbers
Simple Data Type Representation and conversion of numbers
Numbers and number systems
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Information Representation (Level ISA3) Floating point numbers.
Computer Arithmetic Nizamettin AYDIN
Number Systems II Prepared by Dr P Marais (Modified by D Burford)
IT253: Computer Organization
Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 12 Last time: –Efficient recursive and non-recursive sorts. –Analyzing the complexity of recursive.
Oct. 18, 2007SYSC 2001* - Fall SYSC2001-Ch9.ppt1 See Stallings Chapter 9 Computer Arithmetic.
Winter 2006CISC121 - Prof. McLeod1 Stuff Assn 5 is available and due Monday, 7pm. Pre-exam tutorial time. (Exam is April 27). April 18, room TBA. Office.
Fixed and Floating Point Numbers Lesson 3 Ioan Despi.
CSC 221 Computer Organization and Assembly Language
How a Computer Processes Information. Java – Numbering Systems OBJECTIVE - Introduction to Numbering Systems and their relation to Computer Problems Review.
CS 160 Lecture 4 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
Numbers in Computers.
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
Winter 2016CISC101 - Prof. McLeod1 Today Numeric representation (or “How does binary and hexadecimal work?”). How can a CPU understand instructions written.
Cosc 2150: Computer Organization Chapter 9, Part 3 Floating point numbers.
William Stallings Computer Organization and Architecture 8th Edition
MATH Lesson 2 Binary arithmetic.
Floating Point Numbers
Floating Point Representations
Data Representation COE 308 Computer Architecture
Programming and Data Structure
Data Representation.
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
Number Representation
Number Systems and Binary Arithmetic
Data Representation Binary Numbers Binary Addition
Integer Division.
Floating Point Math & Representation
Lecture 9: Floating Point
Topics IEEE Floating Point Standard Rounding Floating Point Operations
Floating Point Numbers: x 10-18
Chapter 3 Data Storage.
CS/COE0447 Computer Organization & Assembly Language
Chapter 6 Floating Point
Data Structures Mohammed Thajeel To the second year students
Topic 3d Representation of Real Numbers
Binary Numbers Material on Data Representation can be found in Chapter 2 of Computer Architecture (Nicholas Carter) CSC 370 (Blum)
Number Representations
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
CISC101 Reminders Your group name in onQ is your grader’s name.
Winter 2018 CISC101 11/29/2018 CISC101 Reminders
How to represent real numbers
Dr. Clincy Professor of CS
ECEG-3202 Computer Architecture and Organization
COMS 361 Computer Organization
Storing Negative Integers
Chapter 3 DataStorage Foundations of Computer Science ã Cengage Learning.
Computer Organization
CISC124 Assignment 1 due this Friday, 7pm.
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
CMPE212 – Reminders The other four assignments are now posted.
Floating Point Numbers
Prof. Giancarlo Succi, Ph.D., P.Eng.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Topic 3d Representation of Real Numbers
Floating Point Numbers
Chapter3 Fixed Point Representation
Computer Organization and Assembly Language
Number Representations
Data Representation COE 308 Computer Architecture
Presentation transcript:

CISC124 Assignment 1 due this Friday, 7pm. Fall 2018 CISC124 2/21/2019 CISC124 Assignment 1 due this Friday, 7pm. QWIC Tutorial Tonight at 8pm in Mac-Corry D201. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

Today Building modular code at the method level. Start Numeric Representation. Fall 2018 CISC124 - Prof. McLeod

Building Modular Code You should already know the advantages to modularity: Easier to build. Easier to test. Easier to debug. Easier to modify. Easier to share. Building objects just takes modular design to the next level. But for now, let’s focus on methods (or functions). Fall 2018 CISC124 - Prof. McLeod

Designing Methods Methods are written to avoid repeating code. So, make sure you only have one method to do the “one thing”. Methods should be short: How short is “short”? One to ten lines? If you can satisfy all the other rules and the code still explains itself, then the method is short enough. Fall 2018 CISC124 - Prof. McLeod

Designing Methods, Cont. Methods should only do one thing and do it well. Yah, but how can we define “one thing”? Write the most abstract description of what the method does – does it still have the word “and” in it? Look for loosely coupled sections within a method if it seems too large – these are natural dividing points. Sometimes the coder puts an empty line between sections as an unconscious admission of loose coupling. Fall 2018 CISC124 - Prof. McLeod

Designing Methods, Cont. Keep all code within the method at the same level of abstraction. If you find yourself writing detailed code right beside high level code, you are not following this rule. Your program should be readable as a top-down narrative. The most abstract methods will be at the top of the program, leading to the least abstract methods further down. (BTW, I’m bad and I don’t always follow this rule.) Fall 2018 CISC124 - Prof. McLeod

Designing Methods, Cont. Use less than three parameters wherever possible – the best number is zero! Try to use parameters for input only. Flag parameters are ugly – they are saying that the method does at least two things. If needed, multiple parameters can be grouped into an object or list, provided they share a theme. For example: drawCircle(Point centrePoint, int radius) is better than drawCircle(int centreX, int centreY, int radius) Fall 2018 CISC124 - Prof. McLeod

Designing Methods, Cont. Check to see how readable the method name is when it has its parameter list – does it read like a sentence? (verb then noun?). Fall 2018 CISC124 - Prof. McLeod

Designing Methods, Cont. The method should not spawn any side effects. Such as changing the contents of variables passed by reference. Or spawning off another process that is at the same level of abstraction as the method itself. The method should only invoke methods that are at a lower level of abstraction than itself. A method should either do something or answer something, not both. Fall 2018 CISC124 - Prof. McLeod

How to Write Good Methods It is not easy to do, especially if you are used to writing much larger methods. Be prepared to write and then re-write your code several times. Each time, you will probably be breaking larger methods into smaller ones. Each time you re-write (or refactor) the code it gets better (tidier, easier to read, and often shorter!). Will a multiple method program be faster than a single method version that does the same thing? Fall 2018 CISC124 - Prof. McLeod

Assignment 1 Lectures have covered everything you need to know for the assignment. Start the assignment before your lab this week… Fall 2018 CISC124 - Prof. McLeod

Numeric Representation - Topics Base 10 numbers in binary and hexadecimal. Two’s complement and IEEE754, for the storage of numbers in RAM. Source and effects of roundoff error. Convergence and increasing the accuracy of summations. Fall 2018 CISC124 - Prof. McLeod

Numeric Representation In base 2 (digits either 0 or 1): r=2, a binary number: (110101.11)2= 1×25+1×24+0×23+1×22+0×21+1×20 +1×2-1 +1×2-2 =53.75 (in base 10) “r” is the “radix” or the base of the number Fall 2018 CISC124 - Prof. McLeod

Numeric Representation - Cont. Hexadecimal Numbers: a base-16 system with 16 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F: For example: r = 16, a hex number: (B65F)16 = 11×163+6×162+5×161+15×160 = 4668710 Fall 2018 CISC124 - Prof. McLeod

Numeric Representation - Cont. The above series show how you can convert from binary or hex to decimal. How to convert from decimal to one of the other bases?: integral part: divide by r and keep the remainder. decimal part: multiply by r and keep the carry “r” is the base - either 2 or 16 Fall 2018 CISC124 - Prof. McLeod

Numeric Representation - Cont. For example, convert 625.4510 to binary: So, 62510 is 10011100012 Divisor(r) Dividend Remainder 2 625 2 312 (quotient) 1 2 156 0 2 78 0 2 39 0 2 19 1 2 9 1 2 4 1 2 2 0 2 1 0 0 1 least significant digit most significant digit Fall 2018 CISC124 - Prof. McLeod

Numeric Representation - Cont. For the “0.4510” part: So, 0.4510 is 0.0111002 625.45 is: (1001110001.011100)2 Multiplier(r) Multiplicand Carry 2 0 .45 2 0 .90 (product) 0 2 1 .80 1 2 1 .60 1 2 1 .20 1 2 0 .40 0 2 0 .80 0 2 0 .40 0 ... Fall 2018 CISC124 - Prof. McLeod

Aside - Roundoff Error Consider 0.1, for example: (0.1)10 = (0.0 0011 0011 0011 0011 0011…)2 What happens to the part of a real number that cannot be stored? It is lost - the number is either truncated or rounded. The “lost part” is called the Roundoff Error. Fall 2018 CISC124 - Prof. McLeod

Numeric Representation - Cont. Converting between binary and hex is much easier - done by “grouping” the numbers: For example: (2C6B.F06)16 = (?)2 ( 2 C 6 B . F 0 6 )16 ( 0010 1100 0110 1011 . 1111 0000 0110)2 Fall 2018 CISC124 - Prof. McLeod

Numeric Representation - Cont. And going from binary to hex: (10110011101.11011)2 = (?)16 ( 0101 1001 1101 . 1101 1000 )2 ( 5 9 D . D 8 )16 Fall 2018 CISC124 - Prof. McLeod

Storage of Integers An “un-signed” 8 digit (one byte) binary number can range from 00000000 to 11111111 00000000 is 0 in base 10. 11111111 is 1x20 + 1x21 + 1x22 + … + 1x27 = 255, base 10. Fall 2018 CISC124 - Prof. McLeod

Storage of Integers - Cont. So, how can a negative binary number be stored? One way is to use the Two’s Complement system of storage: Make the most significant bit a negative number: So, the lowest “signed” binary 8 digit number is now: 10000000, which is -1x27, or -128 base 10. Fall 2018 CISC124 - Prof. McLeod

Storage of Integers - Cont. Two’s Complement System for 1 byte: binary base 10 10000000 -128 10000001 -127 11111111 -1 00000000 00000001 1 01111111 127 Fall 2018 CISC124 - Prof. McLeod

Storage of Integers - Cont. For example, the binary number 10010101 is 1x20 + 1x22 + 1x24 - 1x27 = 1 + 4 + 16 - 128 = -107 base 10 Now you can see how the primitive integer type, byte, ranges from -128 to 127. Fall 2018 CISC124 - Prof. McLeod

Storage of Integers - Cont. Suppose we wish to add 1 to the largest byte value: 01111111 +00000001 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 10000000, 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

Storage of Integers - Cont. An int is stored in 4 bytes using “two’s complement”. An int ranges from: 10000000 00000000 00000000 00000000 to 01111111 11111111 11111111 11111111 or -2147483648 to 2147483647 in base 10 Fall 2018 CISC124 - Prof. McLeod

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

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: 0 00000000 00000000 00000000 0000000 exponent mantissa sign bit Fall 2018 CISC124 - Prof. McLeod

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 +127. 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

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 = 10000000000000000000000 E = 125 – 127 = -2 The significand is 1.10000000000000000000000 value = 1  1.10000000000000000000000  2-2 Fall 2018 CISC124 - Prof. McLeod

Storage of Real Numbers - Cont. Or, value = 0.011, which is 0.37510 The maximum float would be for : s = 0 e = 254 f = 11111111111111111111111 We can use a calculator like: https://www.h-schmidt.net/FloatConverter/IEEE754.html Fall 2018 CISC124 - Prof. McLeod

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

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

Storage of Real Numbers - Cont. This allows the range of float values to be extended to slightly lower values (to 1.4  10-45, 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

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