SlideShare a Scribd company logo
1 of 13
Download to read offline
2’s Complement and
Floating-Point
CSE 351 Section 3
Two’s Complement
•An n-bit, two’s complement
number can represent the
range [−2 𝑛−1 , 2 𝑛−1 − 1].
• Note the asymmetry of this
range about 0 – there’s one more
negative number than positive
•Note what happens when
you overflow
4-bit two’s complement range
Understanding Two’s Complement
• An easier way to find the decimal value of a two’s complement number:
~x + 1 = -x
• We can rewrite this as x = ~(-x - 1), i.e. subtract 1 from the given number,
and flip the bits to get the positive portion of the number.
• Example: 0b11010110
• Subtract 1: 0b11010110 - 1 = 0b11010101
• Flip the bits: ~0b11010101 = 0b00101010
• Convert to decimal: 0b00101010 = (32+8+2)10 = 4210
• Multiply by negative one, Answer: -4210.
Two’s Complement: Operations
1. Convert numbers to binary
• 0xAB = 0b10101011
• 1710 = 0b00010001
2. Compute x | y
0000 ... 1010 1011
| 0000 ... 0001 0001
--------------------
0000 ... 1011 1011
3. Compute ~(x | y) (flip the bits)
~ 0000 0000 0000 0000 0000 0000 1011 1011
-----------------------------------------
1111 1111 1111 1111 1111 1111 0100 0100
int x = 0xAB;
int y = 17;
int z = 5;
int result = ~(x | y) + z;
What is the value of
result in decimal?
Two’s Complement: Operations
4. Compute ~(x | y) + z
• You can either convert to decimal before or after
adding z
• 1710 = 0b00010001
• Convert
111111111111111111111111010001002 to
decimal
(hint: -x = ~x + 1)
• ~111111111111111111111111010001002 =
0000000000000000000000000101110112
• ~0000……101110112 + 1 = 0000……101111002
• -x = 0000……101111002
• x = 1000……101111002
int x = 0xAB;
int y = 17;
int z = 5;
int result = ~(x | y) + z;
What is the value of
result in decimal?
Floating Point
• value = (-1)S * M * 2E
• Numerical Form
• Sign bit s determines whether number is negative or positive
• Significand (mantissa) M normally a fractional value in range [1.0, 2.0)
• Exponent E weights value by a (possibly negative) power of two
• Representation in Memory
• MSB s is sign bit s
• exp field encodes E (but is not equal to E) – remember the bias
• Frac field encodes M (but is not equal to M)
s exp mant
Floating Point
• value = (-1)S * M * 2E
• Value: ±1 × Mantissa × 2Exponent
• Bit Fields: (-1)S × 1.M × 2(E+bias)
• Bias
• Read exponent as unsigned, but with bias of –(2w-1-1) = –127
• Representable exponents roughly ½ positive and ½ negative
• Exponent 0 (Exp = 0) is represented as E = 0b 0111 1111
• Why?
• Floating point arithmetic = easier
• Somewhat compatible with 2’s complement
s exp mant
Floating Point
• Exponent overflow yields +∞ or -∞
• Floats with value +∞, -∞, and NaN can be used in operations
• Result usually still +∞, -∞, or NaN; sometimes intuitive, sometimes not
• Floating point ops do not work like real math, due to rounding!
• Not associative:
• (3.14 + 1e100) – 1e100 != 3.14 + (1e100 – 1e100)
• Not distributive:
• 100 * (0.1 + 0.2) != 100 * 0.1 + 100 * 0.2
• Not cumulative
• Repeatedly adding a very small number to a large one may do nothing
s exp mant
Floating Point Examples
• How do you represent -1.5 in floating point?
• Sign bit: 1
• First the integral part of the value: 1 = 0b1
• Now compute the decimal: 0.5 = 0b0.1
• 1.510 = 1.1b
• Don’t need to normalize because it’s already in scientific notation: 1.1 x 20
• Exponent: 0 + 127 = 12710 = 011111112
• Mantissa: 10000000000000000000002
• -1.510 = 1 01111111 10000000000000000000002 = 0xBFC00000
s exp mant
1 8 23
Floating Point Examples
• How do you represent 1.0 in floating point?
• Sign bit: 0
• First the integral part of the value: 1 = 0b1
• 1.010 => 1.0b
• Don’t need to normalize because it’s already in scientific notation: 1.0 x 20
• Exponent: 0 + 127 = 12710 = 011111112
• Mantissa: 000000000000000000000002
• 1.010 = 0 01111111 000000000000000000000002 = 0x3F800000
s exp mant
1 8 23
Floating Point Addition
(–1)s1*M1*2E1 + (-1)s2*M2*2E2
(Assume E1 > E2)
• Exact Result: (–1)s*M*2E
• Sign s, mantissa M:
• M = M1 + M2, result of signed align & add
• Exponent E: E1
• Fixing
• If M ≥ 2, shift M right, increment E
• if M < 1, shift M left k positions, decrement E by k
• Overflow if E out of range
• Round M to fit frac precision
s exp mant
1 8 23
What is floating point result of 12.3125 + 1.5?
12.312510 = 0 10000010 10001010000000000000000
1.510 = 0 01111111 10000000000000000000000
In scientific notation: (1.1000101 x 23) + (1.1 x 20 )
1100.0101 x 20
+ 1.1 x 20
------------------
1101.1101 x 20
Adjusted mantissa: 1.1011101 => M = 1011101
Ans: 0 10000010 10111010000000000000000
Floating Point Multiplication
(–1)s1*M1*2E1 * (–1)s2*M2*2E2
• Exact Result: (–1)s*M*2E
• Sign s: s1 ^ s2
• Mantissa M: M1 * M2
• Exponent E: E1 + E2
• Fixing
• If M ≥ 2, shift M right, increment E
• If E out of range, overflow
• Round M to fit frac precision
s exp mant
1 8 23
What is floating point result of 1.1* 1.0?
1.110 = 0 01111111 00011001100110011001101
1.010 = 0 01111111 00000000000000000000000
In scientific notation: (1.1 x 20) + (1.0 x 20 )
1.00011001100110011001101 x 20
* 1.0 x 20
--------------------------------------
0000000000000000000000000
1000110011001100110011010
Result: 1.000110011001100110011010 x 20
Ans: 0 01111111 00011001100110011001101
Floating Point Worksheet

More Related Content

Similar to Floating_point_representation.pdf

Review on Number Systems: Decimal, Binary, and Hexadecimal
Review on Number Systems: Decimal, Binary, and HexadecimalReview on Number Systems: Decimal, Binary, and Hexadecimal
Review on Number Systems: Decimal, Binary, and HexadecimalUtkirjonUbaydullaev1
 
ch3a-binary-numbers.ppt
ch3a-binary-numbers.pptch3a-binary-numbers.ppt
ch3a-binary-numbers.pptRAJKUMARP63
 
Data representation
Data representationData representation
Data representationChew Hoong
 
ch3a-binary-numbers.ppt
ch3a-binary-numbers.pptch3a-binary-numbers.ppt
ch3a-binary-numbers.pptRabiaAsif31
 
Convertion of single precision
Convertion of single precisionConvertion of single precision
Convertion of single precisionnikhilarora2211
 
BOOTH ALGO, DIVISION(RESTORING _ NON RESTORING) etc etc
BOOTH ALGO, DIVISION(RESTORING _ NON RESTORING) etc etcBOOTH ALGO, DIVISION(RESTORING _ NON RESTORING) etc etc
BOOTH ALGO, DIVISION(RESTORING _ NON RESTORING) etc etcAbhishek Rajpoot
 
digital-electronics.pptx
digital-electronics.pptxdigital-electronics.pptx
digital-electronics.pptxsulekhasaxena2
 
1. NUMBER SYSTEM.pptx Computer Applications in Pharmacy
1. NUMBER SYSTEM.pptx Computer Applications in Pharmacy1. NUMBER SYSTEM.pptx Computer Applications in Pharmacy
1. NUMBER SYSTEM.pptx Computer Applications in PharmacyVedika Narvekar
 
B.sc cs-ii-u-1.8 digital logic circuits, digital component floting and fixed ...
B.sc cs-ii-u-1.8 digital logic circuits, digital component floting and fixed ...B.sc cs-ii-u-1.8 digital logic circuits, digital component floting and fixed ...
B.sc cs-ii-u-1.8 digital logic circuits, digital component floting and fixed ...Rai University
 
Bca 2nd sem-u-1.8 digital logic circuits, digital component floting and fixed...
Bca 2nd sem-u-1.8 digital logic circuits, digital component floting and fixed...Bca 2nd sem-u-1.8 digital logic circuits, digital component floting and fixed...
Bca 2nd sem-u-1.8 digital logic circuits, digital component floting and fixed...Rai University
 

Similar to Floating_point_representation.pdf (20)

binary-numbers.ppt
binary-numbers.pptbinary-numbers.ppt
binary-numbers.ppt
 
Review on Number Systems: Decimal, Binary, and Hexadecimal
Review on Number Systems: Decimal, Binary, and HexadecimalReview on Number Systems: Decimal, Binary, and Hexadecimal
Review on Number Systems: Decimal, Binary, and Hexadecimal
 
ch3a-binary-numbers.ppt
ch3a-binary-numbers.pptch3a-binary-numbers.ppt
ch3a-binary-numbers.ppt
 
ch3a-binary-numbers.ppt
ch3a-binary-numbers.pptch3a-binary-numbers.ppt
ch3a-binary-numbers.ppt
 
Data representation
Data representationData representation
Data representation
 
ch3a-binary-numbers.ppt
ch3a-binary-numbers.pptch3a-binary-numbers.ppt
ch3a-binary-numbers.ppt
 
Convertion of single precision
Convertion of single precisionConvertion of single precision
Convertion of single precision
 
BOOTH ALGO, DIVISION(RESTORING _ NON RESTORING) etc etc
BOOTH ALGO, DIVISION(RESTORING _ NON RESTORING) etc etcBOOTH ALGO, DIVISION(RESTORING _ NON RESTORING) etc etc
BOOTH ALGO, DIVISION(RESTORING _ NON RESTORING) etc etc
 
Number system
Number systemNumber system
Number system
 
Class10
Class10Class10
Class10
 
digital-electronics.pptx
digital-electronics.pptxdigital-electronics.pptx
digital-electronics.pptx
 
1. NUMBER SYSTEM.pptx Computer Applications in Pharmacy
1. NUMBER SYSTEM.pptx Computer Applications in Pharmacy1. NUMBER SYSTEM.pptx Computer Applications in Pharmacy
1. NUMBER SYSTEM.pptx Computer Applications in Pharmacy
 
Number system
Number systemNumber system
Number system
 
B.sc cs-ii-u-1.8 digital logic circuits, digital component floting and fixed ...
B.sc cs-ii-u-1.8 digital logic circuits, digital component floting and fixed ...B.sc cs-ii-u-1.8 digital logic circuits, digital component floting and fixed ...
B.sc cs-ii-u-1.8 digital logic circuits, digital component floting and fixed ...
 
Floating Point Numbers
Floating Point NumbersFloating Point Numbers
Floating Point Numbers
 
09 Arithmetic
09  Arithmetic09  Arithmetic
09 Arithmetic
 
Number system
Number systemNumber system
Number system
 
Bca 2nd sem-u-1.8 digital logic circuits, digital component floting and fixed...
Bca 2nd sem-u-1.8 digital logic circuits, digital component floting and fixed...Bca 2nd sem-u-1.8 digital logic circuits, digital component floting and fixed...
Bca 2nd sem-u-1.8 digital logic circuits, digital component floting and fixed...
 
lec2_BinaryArithmetic.ppt
lec2_BinaryArithmetic.pptlec2_BinaryArithmetic.ppt
lec2_BinaryArithmetic.ppt
 
L2 number
L2 numberL2 number
L2 number
 

Recently uploaded

Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Recently uploaded (20)

Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 

Floating_point_representation.pdf

  • 2. Two’s Complement •An n-bit, two’s complement number can represent the range [−2 𝑛−1 , 2 𝑛−1 − 1]. • Note the asymmetry of this range about 0 – there’s one more negative number than positive •Note what happens when you overflow 4-bit two’s complement range
  • 3. Understanding Two’s Complement • An easier way to find the decimal value of a two’s complement number: ~x + 1 = -x • We can rewrite this as x = ~(-x - 1), i.e. subtract 1 from the given number, and flip the bits to get the positive portion of the number. • Example: 0b11010110 • Subtract 1: 0b11010110 - 1 = 0b11010101 • Flip the bits: ~0b11010101 = 0b00101010 • Convert to decimal: 0b00101010 = (32+8+2)10 = 4210 • Multiply by negative one, Answer: -4210.
  • 4. Two’s Complement: Operations 1. Convert numbers to binary • 0xAB = 0b10101011 • 1710 = 0b00010001 2. Compute x | y 0000 ... 1010 1011 | 0000 ... 0001 0001 -------------------- 0000 ... 1011 1011 3. Compute ~(x | y) (flip the bits) ~ 0000 0000 0000 0000 0000 0000 1011 1011 ----------------------------------------- 1111 1111 1111 1111 1111 1111 0100 0100 int x = 0xAB; int y = 17; int z = 5; int result = ~(x | y) + z; What is the value of result in decimal?
  • 5. Two’s Complement: Operations 4. Compute ~(x | y) + z • You can either convert to decimal before or after adding z • 1710 = 0b00010001 • Convert 111111111111111111111111010001002 to decimal (hint: -x = ~x + 1) • ~111111111111111111111111010001002 = 0000000000000000000000000101110112 • ~0000……101110112 + 1 = 0000……101111002 • -x = 0000……101111002 • x = 1000……101111002 int x = 0xAB; int y = 17; int z = 5; int result = ~(x | y) + z; What is the value of result in decimal?
  • 6. Floating Point • value = (-1)S * M * 2E • Numerical Form • Sign bit s determines whether number is negative or positive • Significand (mantissa) M normally a fractional value in range [1.0, 2.0) • Exponent E weights value by a (possibly negative) power of two • Representation in Memory • MSB s is sign bit s • exp field encodes E (but is not equal to E) – remember the bias • Frac field encodes M (but is not equal to M) s exp mant
  • 7. Floating Point • value = (-1)S * M * 2E • Value: ±1 × Mantissa × 2Exponent • Bit Fields: (-1)S × 1.M × 2(E+bias) • Bias • Read exponent as unsigned, but with bias of –(2w-1-1) = –127 • Representable exponents roughly ½ positive and ½ negative • Exponent 0 (Exp = 0) is represented as E = 0b 0111 1111 • Why? • Floating point arithmetic = easier • Somewhat compatible with 2’s complement s exp mant
  • 8. Floating Point • Exponent overflow yields +∞ or -∞ • Floats with value +∞, -∞, and NaN can be used in operations • Result usually still +∞, -∞, or NaN; sometimes intuitive, sometimes not • Floating point ops do not work like real math, due to rounding! • Not associative: • (3.14 + 1e100) – 1e100 != 3.14 + (1e100 – 1e100) • Not distributive: • 100 * (0.1 + 0.2) != 100 * 0.1 + 100 * 0.2 • Not cumulative • Repeatedly adding a very small number to a large one may do nothing s exp mant
  • 9. Floating Point Examples • How do you represent -1.5 in floating point? • Sign bit: 1 • First the integral part of the value: 1 = 0b1 • Now compute the decimal: 0.5 = 0b0.1 • 1.510 = 1.1b • Don’t need to normalize because it’s already in scientific notation: 1.1 x 20 • Exponent: 0 + 127 = 12710 = 011111112 • Mantissa: 10000000000000000000002 • -1.510 = 1 01111111 10000000000000000000002 = 0xBFC00000 s exp mant 1 8 23
  • 10. Floating Point Examples • How do you represent 1.0 in floating point? • Sign bit: 0 • First the integral part of the value: 1 = 0b1 • 1.010 => 1.0b • Don’t need to normalize because it’s already in scientific notation: 1.0 x 20 • Exponent: 0 + 127 = 12710 = 011111112 • Mantissa: 000000000000000000000002 • 1.010 = 0 01111111 000000000000000000000002 = 0x3F800000 s exp mant 1 8 23
  • 11. Floating Point Addition (–1)s1*M1*2E1 + (-1)s2*M2*2E2 (Assume E1 > E2) • Exact Result: (–1)s*M*2E • Sign s, mantissa M: • M = M1 + M2, result of signed align & add • Exponent E: E1 • Fixing • If M ≥ 2, shift M right, increment E • if M < 1, shift M left k positions, decrement E by k • Overflow if E out of range • Round M to fit frac precision s exp mant 1 8 23 What is floating point result of 12.3125 + 1.5? 12.312510 = 0 10000010 10001010000000000000000 1.510 = 0 01111111 10000000000000000000000 In scientific notation: (1.1000101 x 23) + (1.1 x 20 ) 1100.0101 x 20 + 1.1 x 20 ------------------ 1101.1101 x 20 Adjusted mantissa: 1.1011101 => M = 1011101 Ans: 0 10000010 10111010000000000000000
  • 12. Floating Point Multiplication (–1)s1*M1*2E1 * (–1)s2*M2*2E2 • Exact Result: (–1)s*M*2E • Sign s: s1 ^ s2 • Mantissa M: M1 * M2 • Exponent E: E1 + E2 • Fixing • If M ≥ 2, shift M right, increment E • If E out of range, overflow • Round M to fit frac precision s exp mant 1 8 23 What is floating point result of 1.1* 1.0? 1.110 = 0 01111111 00011001100110011001101 1.010 = 0 01111111 00000000000000000000000 In scientific notation: (1.1 x 20) + (1.0 x 20 ) 1.00011001100110011001101 x 20 * 1.0 x 20 -------------------------------------- 0000000000000000000000000 1000110011001100110011010 Result: 1.000110011001100110011010 x 20 Ans: 0 01111111 00011001100110011001101