0% found this document useful (0 votes)
2 views38 pages

L2-Variables and Floating Point Number System

The document outlines the fundamentals of computer-oriented numerical methods, focusing on variables, complex variables, and floating-point representation. It explains the definition, naming conventions, and types of variables in C, as well as the significance of floating-point arithmetic and the IEEE Standard 754. Additionally, it covers binary representation, rounding modes, and special values in floating-point operations.

Uploaded by

khushbu kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views38 pages

L2-Variables and Floating Point Number System

The document outlines the fundamentals of computer-oriented numerical methods, focusing on variables, complex variables, and floating-point representation. It explains the definition, naming conventions, and types of variables in C, as well as the significance of floating-point arithmetic and the IEEE Standard 754. Additionally, it covers binary representation, rounding modes, and special values in floating-point operations.

Uploaded by

khushbu kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

CA 552: UNIT-1

Computer oriented numerical methods


• Introduction to variables and complex variables
• Basic concepts: Floating point number system
Objective of the CA552

The primary objective of the course is to


develop the basic understanding of the
construction of numerical algorithms, and
perhaps more importantly, the applicability and
limits of their appropriate use.
Introduction to variables
• Variables are simply names used to refer to some
location in memory – a location that holds a value
with which we are working. It may help to think
of variables as a placeholder for a value.
• A variable is nothing but a name given to a storage
area that our programs can manipulate.

• A variable is a named location in a memory where a


program can manipulate the data.

Example: int fact, float power etc


What Are Variables in C?
• Variables in C have the same meaning as
variables in algebra. That is, they represent
some unknown, or variable, value.

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).

int The most natural size of integer for the


machine.
float A single-precision floating point value.
double A double-precision floating point value.
void Represents the absence of type.
Pointers and arrays
Suppose we have an array,
int num[]={23,34,12,44,56,17};
The following figure shows how this array is located in
memory:

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.

• Some important physical quantities are complex variables (the wave-


function )
• Evaluating definite integrals.
• Obtaining asymptotic solutions of differentials equations.
• Integral transforms
• Many Physical quantities that were originally real become complex as
simple theory is made more general.
Wave function
Evaluating integration
Asymptotic solutions of differentials equations
Number Systems
Floating Point Puzzles
– For each of the following C expressions, either:
• Argue that it is true for all argument values
• Explain why not true

• 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

• IEEE Standard 754


– Established in 1985 as uniform standard for floating
point arithmetic
• Before that, many idiosyncratic formats
– Supported by all major CPUs
• Driven by Numerical Concerns
– Nice standards for rounding, overflow, underflow
– Hard to make go fast
• Numerical analysts predominated over hardware types in
defining standard
Fractional Binary Numbers
2i
2i–1

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

• Computers use a form of scientific notation for


floating-point representation
• Numbers written in scientific notation have three
components:
Floating-Point Representation
• Computer representation of a floating-point
number consists of three fixed-size fields:

• This is the standard arrangement of these fields.


“Normalized” Numeric Values
• Condition
– exp  000…0 and exp  111…1
• Exponent coded as biased value
E = Exp – Bias
• Exp : unsigned value denoted by exp
• Bias : Bias value
– Single precision: 127 (Exp: 1…254, E: -126…127)
– Double precision: 1023 (Exp: 1…2046, E: -1022…1023)
– in general: Bias = 2e-1 - 1, where e is number of exponent bits
• Significand coded with implied leading 1
M = 1.xxx…x2
• xxx…x: bits of frac
• Minimum when 000…0 (M = 1.0)
• Maximum when 111…1 (M = 2.0 – )
• Get extra leading bit for “free”
Normalized Encoding Example
• Value
Float F = 15213.0;
– 1521310 = 111011011011012 = 1.11011011011012 X 213
• Significant
M = 1.11011011011012
frac = 110110110110100000000002
• Exponent
E = 13
Bias = 127
Exp = 140 = 100011002

Floating Point Representation (Class 02):


Hex: 4 6 6 D B 4 0 0
Binary: 0100 0110 0110 1101 1011 0100 0000
0000
140: 100 0110 0
15213: 1110 1101 1011 01
Denormalized Values
• Condition
– exp = 000…0
• Value
– Exponent value E = –Bias + 1
– Significant value M = 0.xxx…x2
• xxx…x: bits of frac
• Cases
– exp = 000…0, frac = 000…0
• Represents value 0
• Note that have distinct values +0 and –0
– exp = 000…0, frac  000…0
• Numbers very close to 0.0
• Lose precision as get smaller
• “Gradual underflow”
Special Values
• Condition
– exp = 111…1
• Cases
– exp = 111…1, frac = 000…0
• Represents value(infinity)
• Operation that overflows
• Both positive and negative
• E.g., 1.0/0.0 = 1.0/0.0 = +, 1.0/0.0 = 
– exp = 111…1, frac  000…0
• Not-a-Number (NaN)
• Represents case when no numeric value can be determined
• E.g., sqrt(–1), 
Summary of Floating Point
Real Number Encodings

 -Normalized -Denorm +Denorm +Normalized +

NaN NaN
0 +0
Tiny Floating Point Example

• 8-bit Floating Point Representation


– the sign bit is in the most significant bit.
– the next four bits are the exponent, with a bias of
7.
– the last three bits are the frac
• Same General Form as IEEE Format
– normalized, denormalized
– representation of 0, NaN, infinity

7 6 3 2 0
s exp frac
Values Related to the Exponent
Exp exp E 2E

0 0000 -6 1/64 (denorms)


1 0001 -6 1/64
2 0010 -5 1/32
3 0011 -4 1/16
4 0100 -3 1/8
5 0101 -2 1/4
6 0110 -1 1/2
7 0111 0 1
8 1000 +1 2
9 1001 +2 4
10 1010 +3 8
11 1011 +4 16
12 1100 +5 32
13 1101 +6 64
14 1110 +7 128
15 1111 n/a (inf, NaN)
Dynamic Range
s exp frac E Value

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

• Binary Fractional Numbers


– “Even” when least significant bit is 0
– Half way when bits to right of rounding position = 100…2
• Examples
– Round to nearest 1/4 (2 bits right of binary point)
Value Binary Rounded Action Rounded Value
2 3/32 10.00011210.002 (<1/2—down)2
2 3/16 10.00110210.012 (>1/2—up) 2 1/4
2 7/8 10.11100211.002 (1/2—up) 3
2 5/8 10.10100210.102 (1/2—down) 2 1/2
FP Multiplication
• Operands
(–1)s1 M1 2E1 *
(–1)s2 M2 2E2
• Exact Result
(–1)s M 2E
– Sign s: s1 ^ s2
– Significand 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
• Implementation
– Biggest chore is multiplying significands
FP Addition
• Operands
(–1)s1 M1 2E1 E1–E2

(–1)s2 M2 2E2 (–1)s1 M1


– Assume E1 > E2
+ (–1)s2 M2
• Exact Result
(–1)s M 2E (–1)s M
– Sign s, significand M:
• 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
Floating Point in C
• C Guarantees Two Levels
float single precision
double double precision
• Conversions
– Casting between int, float, and double changes numeric
values
– Double or float to int
• Truncates fractional part
• Like rounding toward zero
• Not defined when out of range
– Generally saturates to TMin or TMax
– int to double
• Exact conversion, as long as int has ≤ 53 bit word size
– int to float
• Will round according to rounding mode
Answers to Floating Point Puzzles
int x = …; Assume neither
float f = …; d nor f is NAN
double d = …;

• x == (int)(float) x No: 24 bit significant


• x == (int)(double) x Yes: 53 bit significant
• f == (float)(double) f Yes: increases precision
• d == (float) d No: loses precision
• f == -(-f); Yes: Just change sign bit
• 2/3 == 2/3.0 No: 2/3 == 0
• d < 0.0 ((d*2) < 0.0) Yes!
• d > f -f > -d Yes!
• d * d >= 0.0 Yes!
• (d+f)-d == f No: Not associative
Summary

• IEEE Floating Point Has Clear Mathematical


Properties
– Represents numbers of form M X 2E
– Can reason about operations independent of
implementation
• As if computed with perfect precision and then rounded
– Not the same as real arithmetic
• Violates associativity/distributivity
• Makes life difficult for compilers & serious numerical
applications programmers

You might also like