Binary Representations Real Numbers
• High-level language program (in C) swap (int v[], int k) {
int temp = v[k]; v[k] = v[k+1]; v[k+1] = temp;
• Assembly language program (for MIPS) swap: sll $2, $5, 2 add $2, $4,$2
C compiler
The Big Picture
}
lw $15, lw $16, sw $16, sw $15, jr $31
0($2)
4($2)
0($2)
4($2) assembler
• Machine (object) code (for MIPS)
000000 00000 00101 0001000010000000 000000 00100 00010 0001000000100000
…
Agenda
– Represent unsigned and signed integers in binary
• Today
– Fixed point representations
– Scientific Notation
– IEEE 754 floating point standard – Special Cases
• Last Time:
How to Represent Real Numbers?
Real Numbers • Positionalnotationallowsforfractions
anan-1………a1a0 . a-1a-2……..a-m
• Let’s start with fixed point representation – Choose n and m
– Radix point is always in the same position
– Easy to implement
– Limited range
• 152.310
1*10^2+5*10^1+2*10^0+3*10^(-1)
• 1011.012 P
Real Numbers
Binary to Decimal
• Integersscaledbyanappropriatefactor • Directexpansionwithpositionalweights
• 0.110012 2 12412 12
5
• Use the same trick as before 0.1101010012 OD48 16
Binary to Hexadecimal
GroupthemI
0.2BE16 o 0010 1011 1110 2
Decimal to Binary
• Multiply by 2 and note the integer part
• Subtractintegerpartandrepeatuntilnofractionleft
0.62510
0625
tio
O
0
I 01
Decimal to Binary
• Can all decimal fractions be expressed exactly in Binary?
0.110
0.2
0 2 2 0.4 0.24 2 0.8
0.8 2 16
011 2
Infinite
2 1.2 b22 0.4
0.6
How to Represent VERY Small and Big Numbers in Decimal?
How big is Coronavirus?
Particle
Size (meter)
PM10
0.00001
Red Blood Cell
0.000007
PM2.5
0.0000025
Bacteria
0.0000005
Coronavirus
0.0000001
Particles filtered by masks
0.000000007
What numbers do we need?
1.0 × 10-9
3.15576 × 109 1.47 × 1013 2.99792458 × 1010 6.67300 × 10-11 1.98892 × 1030 2.08 × 1022
1.0 × 10-15
Seconds per nanosecond Seconds per century
US National Debt
Speed of light in cm/s Gravitational constant
Mass of sun in kilograms Distance to Andromeda in m Size of a proton in meters
Scientific Notation for Decimal
• Weusescientificnotationforbigandsmallnumbers – Use a single digit to the left of the decimal point
– Multiplied by base (e.g., 10) raised to some exponent
– Use e or E to denote the exponent part
1.0 × 10-15 1.0e-15 1.0E-15
• Anormalizednumberhasnoleadingzero – 1.010 x 10-9 normalized
– 0.110 x 10-8 not normalized
– 10.010 x 10-10 not normalized
Scientific Notation for Binary
• Binary numbers can be written in scientific notation too
• 1.02 x 2-1 • 1.12 x 23
How to Represent Floating Points in Computer Systems?
• IEEE 754
Floating Point Standards
• The standard for representing real numbers since 1985
– Used in nearly all computers today
– Defines two representations • single precision (32 bits)
• double precision (64 bits)
– In high level languages, data of this type is called • float (single precision)
• double (for double precision)
Floating Points for Binary Numbers
• FloatingPoints:theradixpointis”floating”basedonthe exponent
Write binary number in scientific notions: 1.xxxxxxx2 × 2yyyy
– x is the fraction / significand / coefficient / mantissa – y is the exponent
– always has a one to the left of the binary point
Single Precision
Sign Exponent Fraction 1 bit 8 bits 23 bits
A real number can be described as (-1)S x (1+fraction) x 2Exponent
• Numbers are in normalized form. Why?
S
E
F
Base 2
S
Exponent
Mantissa
0.0011 × 20
0
0000000
001100…
0.011 × 2−1
0
1111111
011000…
0.11 × 2−2
0
1111110
110000…
All equivalent to the same real number. The encoding is wasteful
Sign-bit
Sign Exponent Fraction 1 bit 8 bits 23 bits
S
E
F
A real number can be described as (-1)S x (1+fraction) x 2Exponent
• IEEE 754 does not use 2’s complement
• The first bit is the sign – S=0: positive numbers – S=1: negative numbers
Exponent Part
Sign Exponent Fraction 1 bit 8 bits 23 bits
S
E
F
• In IEEE 754, actual representation is
(-1)S x (1 + Fraction) x 2(E – bias)
• Exponent = E – bias
• E = exponent + bias
• In single-precision, bias = 127
– Represent negative exponents
– Want easy integer style comparison / sorting
Fraction Part
Sign Exponent Fraction 1 bit 8 bits 23 bits
124110
• In IEEE 754, actual representation is
(-1)S x (1 + Fraction) x 2exponent
S
E
F
• 23-bits of fraction is stored
11 2 3
exponente 3 Bexponent bias 124
• Clarification:
– Fraction refers to the number after the radix point – The leading 1 is not stored because it is redundant
Single Precision Floating Point
• Convert -0.75 from decimal to single precision step 1 convert the absolute value into binary
0.752 1 05 2
1 0.75710 1071
step 2 write it in scientific notation
0.111 20
1.1 24
step3 Define SEF
S signal bias
1 127211263100111 111032
E exponent
1 000 o
F 159,3bit 0 23zeros
Single Precision Floating Point
• Convert11000000101000…0000fromsingle precision to decimal
Step1 step2
step3
Define S exponent
51
241 1274318
F 01000 0012 122 art
fraction
put them together 4 1.252
Double Precision
Sign Exponent Fraction 1 bit 11 bits 52 bits
S
E
F
• More bits! Especially for the fraction • More precision
• Double precision uses a bias of 1023
Double Precision
• Convert 3.25 from decimal to double precision
Convert to 3no Ill
step1
on x2 0.5
binary
0.5 2 3.25 11 01
step 2
to
Scientific notation 11.012 1.7012 EF
step 3 Identify S Sto 1 10232624
0
E
T 010
4 010
O
Special Cases?
Denormalized Numbers
• DenormalizedNumbers
– A set of numbers in the tiny interval ( -2-126, 2-126 )
– This includes the number 0
– Smallest normalized is 1.0 x 2-126 = 2-126
– Smallest denormalized is 0.000 01 x 2-126 = 2-149 I
• E = 0 is used to represent denormalized numbers cannot represent • Otherwise, we cannot represent tiny numbers
• Allows us to squeeze more precision out of a floating point
o
NaN: invalid, NaN
• using NaN in math always
results in NaN
• Using NaN in math always
results in NaN
• Nonzerodividedbyzero
– Not the end of the world!
– Results in positive or negative infinity
• 0/0(invalid),orsubtractinginfinityfrominfinity – Results in NaN
– Using NaN in math always results in NaN
• The largest E is reserved for infinity or NaN
Unusual events
Special Cases (single precision)
E
F
Numbers Represented
0
0
0
0
Nonzero
± denormalized number
1-254
Anything
± floating point number
255
0
± infinity
255
Nonzero
NaN (Not a Number)
Itgnap BEinorme
Questions
Sign Exponent Fraction SEEKER 1 bit 8 bits 23 bits
• What is the largest number that can be represented in single
precision?
S
E
F
TEAK
8 1 Ez 1111 1110 exponent 254 127 127 Fell n
45 1.1111 x2d 2 22127
• What is the smallest number that can be represented in single d
precision?
841
Summary
• Fixedpointrepresentations • ScientificNotation
• IEEE754FloatingPoints
• SpecialCases
• Next time:
– Characters, Issues in number representations