L2-Variables and Floating Point Number System
L2-Variables and Floating Point Number System
x=a+b
z + 2 = 3(y - 5)
• Remember that variables in algebra are
represented by a single alphabetic character.
Defining Variables
A variable definition means to tell the compiler where and
how much to create the storage for the variable. A variable
definition specifies a data type and contains a list of one or
more variables of that type as follows:
type variable_list;
int i, j, k;
char c, ch;
float f, salary;
double d;
Naming Variables
• Variables in C may be given representations
containing multiple characters. But there are
rules for these representations.
• Variable names in C
– May only consist of letters, digits, and underscores
– May be as long as you like, but only the first 31
characters are significant
– May not begin with a number
– May not be a C reserved word (keyword)
Type of Variables
Type Description
char Typically a single octet(one byte).
23 34 12 44 56 17
4001 4003 4005 4007 4009 4011
Introduction to complex variables
Functions of a complex variable provide us some powerful and widely useful
tools in in theoretical science.
• x == (int)(float) x
• x == (int)(double) x
int x = …;
• f == (float)(double) f
float f = …;
• d == (float) d
double d = …;
• f == -(-f);
• 2/3 == 2/3.0
Assume neither
• d < 0.0 ((d*2) < 0.0)
d nor f is NaN
• d > f -f > -d
• d * d >= 0.0
• (d+f)-d == f
IEEE Floating Point
4
••• 2
1
bi bi–1 • • • b2 b1 b0 . b–1 b–2 b–3 • • • b–j
1/2
1/4 •••
1/8
2–j
• Representation
– Bits to right of “binary point” represent fractional powers of 2
– Represents rational number: i
bk 2 k
k j
Fractional Binary Number Examples
• Value Representation
5-3/4 101.112
2-7/8 10.1112
63/64 0.1111112
• Observations
– Divide by 2 by shifting right
– Multiply by 2 by shifting left
– Numbers of form 0.111111…2 just below 1.0
• 1/2 + 1/4 + 1/8 + … + 1/2i + … 1.0
Representable Numbers
• Limitation
– Can only exactly represent numbers of the form x/2k
– Other numbers have repeating bit representations
• Value Representation
1/3 0.0101010101[01]…2
1/5 0.001100110011[0011]…2
1/10 0.0001100110011[0011]…2
Floating Point Representation
• Numerical Form
– –1s M 2E
• Sign bit s determines whether number is negative or positive
• Significand M normally a fractional value in range [1.0,2.0).
• Exponent E weights value by power of two
• Encoding
s exp frac
– MSB is sign bit
– exp field encodes E
– frac field encodes M
Floating Point Precisions
• Sizes
– Single precision: 8 exp bits, 23 frac bits
• 32 bits total
– Double precision: 11 exp bits, 52 frac bits
• 64 bits total
– Extended precision: 15 exp bits, 63 frac bits
• Only found in Intel-compatible machines
• Stored in 80 bits
– 1 bit wasted
Floating-Point Representation
NaN NaN
0 +0
Tiny Floating Point Example
7 6 3 2 0
s exp frac
Values Related to the Exponent
Exp exp E 2E
0 0000 000 -6 0
0 0000 001 -6 1/8*1/64 = 1/512 closest to zero
Denormalized 0 0000 010 -6 2/8*1/64 = 2/512
numbers …
0 0000 110 -6 6/8*1/64 = 6/512
0 0000 111 -6 7/8*1/64 = 7/512 largest denorm
0 0001 000 -6 8/8*1/64 = 8/512 smallest norm
0 0001 001 -6 9/8*1/64 = 9/512
…
0 0110 110 -1 14/8*1/2 = 14/16
0 0110 111 -1 15/8*1/2 = 15/16 closest to 1 below
Normalized
0 0111 000 0 8/8*1 = 1
numbers closest to 1 above
0 0111 001 0 9/8*1 = 9/8
0 0111 010 0 10/8*1 = 10/8
…
0 1110 110 7 14/8*128 = 224
0 1110 111 7 15/8*128 = 240 largest norm
0 1111 000 n/a inf
Floating Point Operations
• Conceptual View
– First compute exact result
– Make it fit into desired precision
• Possibly overflow if exponent too large
• Possibly round to fit into frac
• Rounding Modes (illustrate with Rs rounding)
Rs1.40 Rs1.60 Rs1.50 Rs2.50 –Rs1.50
– Zero Rs1 Rs1 Rs1 Rs2 –Rs1
– Round down (-) Rs1 Rs1 Rs1 Rs2 –Rs2
– Round up (+) Rs2 Rs2 Rs2 Rs3 –Rs1
– Nearest Even (default) Rs1 Rs2 Rs2 Rs2 –Rs2
Note:
1. Round down: rounded result is close to but no greater than true result.
2. Round up: rounded result is close to but no less than true result.
Closer Look at Round-To-Even
• Default Rounding Mode
– Hard to get any other kind without dropping into assembly
– All others are statistically biased
• Sum of set of positive numbers will consistently be over- or under-
estimated
• Applying to Other Decimal Places / Bit Positions
– When exactly halfway between two possible values
• Round so that least significant digit is even
– E.g., round to nearest hundredth
1.2349999 1.23 (Less than half way)
1.2350001 1.24 (Greater than half way)
1.2350000 1.24 (Half way—round up)
1.2450000 1.24 (Half way—round down)
Rounding Binary Numbers