CSCI130 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

CSCI130

Description:

In Math: to represent a real number in binary, we use a radix point instead of a decimal point ... We represent the number without the radix point! ... – PowerPoint PPT presentation

Number of Views:120
Avg rating:3.0/5.0
Slides: 13
Provided by: usersC1
Category:
Tags: csci130 | radix

less

Transcript and Presenter's Notes

Title: CSCI130


1
Lecture 2 (1.1-2.3)
  • CSCI130
  • Instructor Dr. Imad Rahal

2
Real Number Representation
  • We deal with a lot of real numbers in our lives
    (class average) and while using computers
  • Fractions or numbers with decimal parts
  • 3/100.3 or 82.34
  • 82.34 810 21 31/10 41/100
  • In Math to represent a real number in binary, we
    use a radix point instead of a decimal point
  • 1101.11 8 4 1 ½ ¼ 13.75
  • On Computers How can we represent the radix
    point? 0? 1?

3
Real Number Representation
  • We resort to the floating-point representation
  • We represent the number without the radix
    point!!!
  • Based on a popular scientific notation that has
    three parts
  • Sign
  • Mantissa (one non-zero digit before the decimal
    point, and two or more after), and
  • Exponent (written with an E following by a sign
    and an integer which represents what power to
    raise 10 to)
  • 6.124E5
  • 6.124 105 612,400
  • Sign? Mantissa? Exponent?
  • Scientific to decimal
  • -9.14E-3 -9.14 10-3 -9.14/1000 - 0.00914

4
Real Number Representation
  • Decimal to scientific
  • Divide (or multiply) by 10 until you have only
    one digit (non-zero) before the decimal point
  • Add (or subtract) one to the exponent every time
    you divide (or multiply)
  • 123.8 ?
  • 1.238E2
  • 0.2348 ?
  • 2.348E-1

5
Real Number Representation
  • Floating-point numbers are very similar
  • But use only 0s and 1s instead
  • /- 1.xxxx2exponent
  • For an eight bit number
  • 0 000 0000
  • Sign, exponent, mantissa (not the standard lab
    2)
  • Sign 0 for positive and 1 for negative
  • The exponent has three digits 0,7, but can be
    positive or negative
  • shift by 4 ? -4,3
  • 000?-4, 001?-3, 010?-2, , 111?3
  • i.e. subtract 4 from the corresponding decimal
    value
  • Use base two now (i.e. 2 raised to the power of
    the exponent value)

6
Real Number Representation
  • The mantissa has one (non-zero) place before the
    decimal point and a number of places after it
  • in binary our digits are 0 and 1 and so we always
    have 1.xxxx
  • No need to represent the one (add one to the
    result)
  • 11100010 ? 1 110 0010
  • Negative
  • 1106, -4 ? 2, exponent22
  • Mantissa 0010 01/2 01/4 11/8 01/16
    1/8
  • without the initial 1 which weve omitted ? 11/8
  • Or 1 decimal equivalent/16 1 2/16
  • - (22)(11/8) -4 1/2

7
Real Number Representation
  • Floating point to decimal conversion
  • 1 break bit pattern into sign (1 bit), exponent
    (3 bits), mantissa (4 bits)
  • 2 sign is if 1 and otherwise
  • 3 exponent decimal equivalent -4
  • 4 mantissa 1 decimal equivalent/24
  • 5 number (sign) mantissa2exponent
  • Try 00101000

8
Real Number Representation
  • What about from decimal to floating point?
  • Format the number into the form 1.xxxx2exponent
  • Multiply (or divide) by 2 until we have a 1
    before the decimal point
  • Subtract (or add) 1 from (to) the exponent for
    every such multiplication (or division)
  • Add 4 to result (in floating-to-decimal
    conversion we subtracted 4)
  • Convert exponent to binary
  • Subtract 1 from the resulting mantissa (in
    floating-to-decimal conversion we added 1)
  • Multiply mantissa by 16 (in floating-to-decimal
    conversion we multiplied by 16)
  • Round to the nearest integer
  • Convert mantissa to binary

9
Real Number Representation
  • -8.48
  • Sign is negative ? 1
  • 8.48 ? 4.24 ? 2.12? 1.06 ? 3 divisions
  • exponent34 7 or 1112
  • mantissa mantissa -1 ? .06
  • multiply mantissa by 16 (write it in terms of
    16ths) ? 0.96 1 0r 0001
  • 1 111 0001

10
Real Number Representation
  • Decimal to floating-point conversion
  • Sign bit 1 if negative and 0 otherwise
  • Exponent 0, mantissa absolute(number)
  • While mantissa lt 1 do
  • mantissa mantissa 2
  • exponent exponent -1
  • While mantissa gt 2 do
  • mantissa mantissa / 2
  • exponent exponent 1
  • Exponent exponent 4
  • Change to binary
  • Mantissa (mantissa -1)16
  • Round off to nearest integer and change to binary
  • Assemble number sign exponent mantissa

11
Real Number Representation
  • 0.319
  • Positive ? sign bit 02
  • mantissa 0.319 2 0.638 ? exponent -1
  • mantissa 0.638 2 1.276 ? exponent -2
  • exponent -2 4 2 0102
  • mantissa (mantissa -1)16 4.416 4 01002
  • 0 010 0100
  • - 0.319
  • Negative ? sign bit 12
  • exponent 0102
  • mantissa 01002
  • 1 010 0100
  • 0
  • Sign bit 02
  • mantissa? Not going to change to 1.xxxx no matter
    what!

12
Real Number Representation
  • No representation for 0
  • 0 000 0000 1.0 2-4 (we assumed there is a 1
    before the mantissa)
  • Assume this to be zero!
  • Another issue, is the method exact?
  • rounding? truncation (close numbers give same
    floating point values)
  • Mantissa 4.416 or 4.0123 or 4.400 are all 4
    01002
  • Problem is also due to using only 8 bits (4-bit
    mantissa)
  • Floating-point numbers require 32 or even 64 bits
    usually
  • But still we will have to round off and truncate
  • That happens regularly with us even when not
    using computers
  • PI 22/7
  • 2/3 or 1/3
  • We approximate a lot but we should know it !
  • Higher precision applications use much larger
    size registers
Write a Comment
User Comments (0)
About PowerShow.com