Today CISC124. Building Modular Code. Designing Methods. Designing Methods, Cont. Designing Methods, Cont. Assignment 1 due this Friday, 7pm.

Similar documents
Inf2C - Computer Systems Lecture 2 Data Representation

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Floating Point Arithmetic

CSCI 402: Computer Architectures. Arithmetic for Computers (3) Fengguang Song Department of Computer & Information Science IUPUI.

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Introduction to Computers and Programming. Numeric Values

Numeric Encodings Prof. James L. Frankel Harvard University

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

System Programming CISC 360. Floating Point September 16, 2008

COMP2611: Computer Organization. Data Representation

Computer Systems C S Cynthia Lee

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

Module 2: Computer Arithmetic

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Foundations of Computer Systems

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

ECE232: Hardware Organization and Design

CS101 Introduction to computing Floating Point Numbers

Lecture 13: (Integer Multiplication and Division) FLOATING POINT NUMBERS

Floating Point Numbers

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

COMP2121: Microprocessors and Interfacing. Number Systems

Course Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

Chapter 2. Data Representation in Computer Systems

CS61c Midterm Review (fa06) Number representation and Floating points From your friendly reader

CS429: Computer Organization and Architecture

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Floating Point Numbers

Floating Point Numbers

Computer Arithmetic Ch 8

Data Representation in Computer Memory

Floating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.

Data Representation Floating Point

15213 Recitation 2: Floating Point

Computer Organisation CS303

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

Digital Logic. The Binary System is a way of writing numbers using only the digits 0 and 1. This is the method used by the (digital) computer.

Most nonzero floating-point numbers are normalized. This means they can be expressed as. x = ±(1 + f) 2 e. 0 f < 1

Number Systems. Both numbers are positive

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

Data Representation Floating Point

Floating Point January 24, 2008

Things to know about Numeric Computation

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

COMPUTER ARCHITECTURE AND ORGANIZATION. Operation Add Magnitudes Subtract Magnitudes (+A) + ( B) + (A B) (B A) + (A B)

CS 261 Fall Floating-Point Numbers. Mike Lam, Professor.

CS101 Lecture 04: Binary Arithmetic

Giving credit where credit is due

COMP Overview of Tutorial #2

FLOATING POINT NUMBERS

Giving credit where credit is due

Computer Arithmetic Ch 8

Finite arithmetic and error analysis

Computer Arithmetic Floating Point

UNIT-III COMPUTER ARTHIMETIC

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

Divide: Paper & Pencil

Characters, Strings, and Floats

Computer Organization: A Programmer's Perspective

Lecture Notes: Floating-Point Numbers

Floating Point : Introduction to Computer Systems 4 th Lecture, May 25, Instructor: Brian Railing. Carnegie Mellon

Floating Point Numbers

Floating-Point Numbers in Digital Computers

CHW 261: Logic Design

Floating Point Arithmetic

Chapter 2 Data Representations

Floating Point Numbers. Lecture 9 CAP

Floating-Point Numbers in Digital Computers

ECE260: Fundamentals of Computer Engineering

Chapter Three. Arithmetic

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

Time (self-scheduled): Location Schedule Your Exam: What to bring:

Floating point. Today. IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Next time.

Chapter 10 - Computer Arithmetic

Question 4: a. We want to store a binary encoding of the 150 original Pokemon. How many bits do we need to use?

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other


Homework 1 graded and returned in class today. Solutions posted online. Request regrades by next class period. Question 10 treated as extra credit

IEEE-754 floating-point

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

CO212 Lecture 10: Arithmetic & Logical Unit

IEEE Floating Point Numbers Overview

UNIT 7A Data Representation: Numbers and Text. Digital Data

COMPUTER ORGANIZATION AND ARCHITECTURE

Chapter 3 Arithmetic for Computers (Part 2)

Data Representation Floating Point

Organisasi Sistem Komputer

Bits, Words, and Integers

Bindel, Fall 2016 Matrix Computations (CS 6210) Notes for

Transcription:

CISC124 Today Assignment 1 due this Friday, 7pm. QWIC Tutorial Tonight at 8pm in Mac-Corry D201. Building modular code at the method level. Start Numeric Representation. Fall 2018 CISC124 - Prof. McLeod 1 Fall 2018 CISC124 - Prof. McLeod 2 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). 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 3 Fall 2018 CISC124 - Prof. McLeod 4 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. 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 5 Fall 2018 CISC124 - Prof. McLeod 6

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 7 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 8 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. 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 9 Fall 2018 CISC124 - Prof. McLeod 10 Assignment 1 Lectures have covered everything you need to know for the assignment. Start the assignment before your lab this week 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 11 Fall 2018 CISC124 - Prof. McLeod 12

Numeric Representation In base 2 (digits either 0 or 1): r=2, a binary number: (110101.11) 2 = 1 2 5 +1 2 4 +0 2 3 +1 2 2 +0 2 1 +1 2 0 +1 2-1 +1 2-2 =53.75 (in base 10) r is the radix or the base of the number 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 16 3 +6 16 2 +5 16 1 +15 16 0 = 46687 10 Fall 2018 CISC124 - Prof. McLeod 13 Fall 2018 CISC124 - Prof. McLeod 14 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 15 For example, convert 625.45 10 to binary: So, 625 10 is 1001110001 2 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 least significant digit 2 1 0 most 0 1 significant digit Fall 2018 CISC124 - Prof. McLeod 16 For the 0.45 10 part: So, 0.45 10 is 0.011100 2 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 1.60 1 2 1.20 1 2 0.40 0 Fall 2018 CISC124 - Prof. McLeod 17... 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 18

Converting between binary and hex is much easier - done by grouping the numbers: For example: And going from binary to hex: (10110011101.11011) 2 = (?) 16 (2C6B.F06) 16 = (?) 2 ( 2 C 6 B. F 0 6 ) 16 ( 0101 1001 1101. 1101 1000 ) 2 ( 5 9 D. D 8 ) 16 ( 0010 1100 0110 1011. 1111 0000 0110) 2 Fall 2018 CISC124 - Prof. McLeod 19 Fall 2018 CISC124 - Prof. McLeod 20 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 1x2 0 + 1x2 1 + 1x2 2 + + 1x2 7 = 255, base 10. 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 -1x2 7, or -128 base 10. Fall 2018 CISC124 - Prof. McLeod 21 Fall 2018 CISC124 - Prof. McLeod 22 Two s Complement System for 1 byte: binary base 10 10000000-128 10000001-127 11111111-1 00000000 0 00000001 1 01111111 127 For example, the binary number 10010101 is 1x2 0 + 1x2 2 + 1x2 4-1x2 7 = 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 23 Fall 2018 CISC124 - Prof. McLeod 24

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! 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 25 Fall 2018 CISC124 - Prof. McLeod 26 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 27 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 0 00000000 00000000 00000000 0000000 sign bit Fall 2018 CISC124 - Prof. McLeod 28 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 29 So a value is stored as: value = (-1) s 1.f 2 E 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 30

Or, value = 0.011, which is 0.375 10 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 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 31 Fall 2018 CISC124 - Prof. McLeod 32 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 33 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 34 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 35