1 / 31

COMPUTER 2430 Object Oriented Programming and Data Structures I

Learn how to store and manipulate fixed point numbers in computer programming using integers and binary representation. Understand the concepts of implicit binary point, positive and signed integers, and converting between fixed point and decimal numbers.

mcclellan
Download Presentation

COMPUTER 2430 Object Oriented Programming and Data Structures I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPUTER 2430Object Oriented Programming andData Structures I

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

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

  4. 1 0 1 1 0 0 1 1 How to Store Integers in Computer Binary Number 27 26 25 24 23 22 21 20 27 + 23 + 22 + 21 + 20 • + 8 + 4 + 2 + 1 Decimal Number: 143

  5. 1 0 1 1 0 0 1 1 How to Store Signed Integers Binary Number 26 25 24 23 22 21 20 Sign bit - 23 + 22 + 21 + 20 - 8 + 4 + 2 + 1 Decimal Number: -15

  6. 0 0 1 1 0 0 1 1 How to Store Signed Integers Binary Number 26 25 24 23 22 21 20 Sign bit + 23 + 22 + 21 + 20 + 8 + 4 + 2 + 1 Decimal Number: +15

  7. Prog3 • Integers are 4 bytes, 32 bits • We only consider positive numbers and ignore the sign: 31 bits only

  8. How to Store Positive Double Numbers 25 24 23 22 21 20 2-1 2-2 Implicit binary point qVal: 2 Fixed Point Number: intVal qVal

  9. 1 0 1 1 0 0 1 1 How to Store Positive Double Numbers Implicit Binary Point : qVal = 2 25 24 23 22 21 20 2-1 2-2 25 + 21 + 20 + 2-1 + 2-2 32 + 2 + 1 + 0.5 + 0.25 Decimal Number: 35.75

  10. 1 0 1 1 0 0 1 1 How to Store Positive Double Numbers Implicit Binary Point : qVal = 2 25 24 23 22 21 20 2-1 2-2 Decimal Number: 35.7512 35.7512 * 22 = 143.0048 = 143 143.0 / 22 = 35.75

  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. 1 0 1 1 0 0 1 1 How to Store Positive Double Numbers Fixed Point Number: qVal = 4 23 22 21 20 2-1 2-2 2-3 2-4 23 + 2-1 + 2-2 + 2-3 + 2-4 8 + .5 + .25 + 0.125 + 0.0625 Decimal Number: 8.9375

  14. 1 0 1 1 0 0 1 1 How to Store Positive Double Numbers Fixed Point Number: qVal = 4 23 22 21 20 2-1 2-2 2-3 2-4 Decimal Number: 8.937567 8.937567 * 24 = 143.001072 = 143 143.0 / 24 = 8.9375

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

  16. 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 Shift Operator x: 40 x >> 2 New value: 10 (40 / 22 )

  17. 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 Shift Operator x: 40 x << 2 New value: 160 (40 * 22 )

  18. 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 Shift Operator x: 40 x >> 4 New value: 2 (Underflow)

  19. 0 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 Shift Operator x: 40 x << 4 New value: 128 (Overflow)

  20. Prog3 • We ignore overflows • Underflows are not totally ignored

  21. 0 1 1 0 0 0 0 0 Shift Operator x: 40 x >> -2 New value: 0 x << -2 New value: 0 Don’t do it! Use IF statement!

  22. 1 0 0 1 1 1 1 0 0 0 0 1 1 0 0 1 Change qVal dbl: 8.9375 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)

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

  24. Adding Numbers 2.43 + 12.345 ? 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!

  25. Multiplying Numbers (not in Prog3) 2.44 * 1.2 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!

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

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

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

  29. Test 1 • Wednesday, October 10 • Note01 – Note14 • No FixedPoint

  30. Schedule • Lab 4: Due Monday, October 15 • Lab 5: Due Wednesday, October 17 • Prog3: Due Wednesday, October 24 • Lab4 and Lab5 are part of Prog3

  31. Prog2 Grace Time 10 PM, October 8

More Related