Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER 2430 Object Oriented Programming and Data Structures I

Similar presentations


Presentation on theme: "COMPUTER 2430 Object Oriented Programming and Data Structures I"— Presentation transcript:

1 COMPUTER 2430 Object Oriented Programming and Data Structures I

2 Fixed Point Numbers Prog2
Computers need to perform floating point calculations Microprocessor chosen doesn't have a floating point capability

3 We use two integers to represent a double number.
Fixed Point Numbers We use two integers to represent a double number. intVal qVal

4 How to Store Integers in Computer
Binary Number 1 Decimal Number: 143

5 How to Store Signed Integers
Binary Number Sign bit 1 Decimal Number: -15

6 How to Store Signed Integers
Binary Number Sign bit 1 Decimal Number: +15

7 Prog2 Integers are 4 bytes, 32 bits
We only consider positive numbers and ignore the sign

8 How to Store Positive Float Numbers
Implicit binary point qVal: 2 Fixed Point Number: intVal qVal

9 How to Store Positive Float Numbers
Implicit Binary Point : qVal = 2 1 Decimal Number:

10 How to Store Positive Float Numbers
Implicit Binary Point : qVal = 2 Decimal Number: * 22 = = 143 1 143.0 / 22 =

11 Double to FixedPoint intVal = (int)(dbl * Math.pow(2, qVal)) Choose your qVal Different qVal values store the same double number with different precisions

12 FixedPoint to Double intVal = (int)(dbl * Math.pow(2, qVal)) dbl = intVal / Math.pow(2, qVal) You may not get the original double number!

13 How to Store Positive Float Numbers
Fixed Point Number: qVal = 4 1 Decimal Number:

14 How to Store Float Numbers in Computer
Fixed Point Number: qVal = 4 Decimal Number: * 24 = = 143 1 143.0 / 24 =

15 How to Store Data in Computer
1 Char: ? Integer 143 Fixed Point Number: qVal = 4 8.9375 Fixed Point Number: qVal = 2 37.75

16 Shift Operator 1 x: 40 x >> 2 New value: 10 (40 / 22 ) 1
1 x: 40 x >> 2 New value: 10 (40 / 22 ) 1 x << 2 New value: 160 (40 * 22 ) 1

17 Shift Operator 1 x: 40 x >> 4 New value: 2 (Underflow) 1
1 x: 40 x >> 4 New value: 2 (Underflow) 1 x << 4 New value: 128 (Overflow) 1

18 Prog2 We ignore overflows Underflows are not totally ignored

19 Shift Operator 1 x: 40 x >> -2 New value: 0 x << -2
1 x: 40 x >> -2 New value: 0 x << -2 Don’t do it!

20 Change qVal 1 dbl: 8.9375 Fixed Point Number: dbl * 24
dbl: Fixed Point Number: dbl * 24 qVal: 4, intVal: 143 Change qVal to 2: dbl * 22 (but we don’t have the original dbl any more!) intVal = intVal >> 2 8.75 (underflow) 1

21 Change qVal 1 dbl: 2.9375 Fixed Point Number: dbl * 24
1 dbl: Fixed Point Number: dbl * 24 qVal: 4, intVal: 47 Change qVal to 6: dbl * 26 intVal = intVal << 2 (no overflow) 1

22 Adding Numbers Must align on the decimal point! fpn1.plus(fpn2)?
2.43 ? Must align on the decimal point! fpn1.plus(fpn2)? fpn1.plus(fpn2, resultQ) Must convert fpn1 and fpn2 to resultQ! Do not change either one! Use local variables!

23 Multiplying Numbers (not in Prog2)
2.44 * 488 244 2928 2.928 fpn1.times(fpn2, resultQ) intval: fpn1.intVal * fpn2.intVal qVal: fpn1.qVal + fpn2.qVal Must change qVal of the product to resultQ!

24 Method equals 1 1 Convert fpn1 to qVal 2 intVal = 37
fpn1: intVal = 149 qVal = 4 fpn1: intVal = 37 qVal = 2 1 1 Convert fpn1 to qVal 2 intVal = 37 Convert fpn2 to qVal 4 intVal = 148 1 1 Fpn1.equals(fpn2): false

25 Method equals 1 1 Convert fpn1 to qVal 2 intVal = 37
fpn1: intVal = 148 qVal = 4 fpn1: intVal = 37 qVal = 2 1 1 Convert fpn1 to qVal 2 intVal = 37 Convert fpn2 to qVal 4 intVal = 148 1 1 Fpn1.equals(fpn2): true

26 Method lessThan Convert to the smaller qVal
Compare intVal after conversion When returns true, it is smaller When returns false, still could be smaller

27 Schedule Quiz 3: Next Monday Lab 4: Due next Wednesday
Lab 5: Due following Monday Lab 6: following Wednesday Prog2: following Friday Lab4, Lab5 and Lab6 are part of Prog2

28 Lab 3 Put your entire UserName_Lab3 project folder under your individual folder on K:\ drive


Download ppt "COMPUTER 2430 Object Oriented Programming and Data Structures I"

Similar presentations


Ads by Google