Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004.

Similar presentations


Presentation on theme: "CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004."— Presentation transcript:

1 CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004

2 A Floating Point Floating point variable types (or floats) allow us to store non-integer numbers (real numbers), which can have fractional parts as well as whole (integral) parts. for any given storage size, floating point variables can represent much larger numbers than integer variables can. The reason for this is that a float type is represented internally in exponential format. The exponential format consists of two parts: the mantissa and the exponent. The mantissa contains the significant digits - that is, the digits that represent the precision (detail) of a quantity. The greater the number of mantissa digits, the greater the precision. The second part, the exponent, merely specifies where to place a decimal point that separates the whole part from the fractional part of a number - hence, the term 'floating point'.

3 Exponentials The reason that floats can store larger numbers is that the size of the number is indicated by a power of ten. Actually, the size is indicated by a power of 2 since all internal values are in binary format but we use decimal numbers (base 10) in our illustrations because this is more natural. 1.2345x10 4 = 1.2345x10x10x10x10 = 12345 1.2345x10 4 is represented as 1.2345e4 or 1.2345E4 Here are example floats (all have the same value): 20.0 2e2 0.2E3

4 Floats can have round-off error More advanced material here (don’t worry about this too much)! Suppose we have a hypothetical storage location that can hold four decimal digits; the largest integer that this could represent would be 9999 but we use a float representation and apportion the mantissa as three digits and the exponent as one digit. Then we could represent a much larger number within these four digits, the largest being: 999 x 10 9 = 999,000,000,000 (Here the mantissa is 999 and the exponent is 9.) Of course we can really only represent a small subset of the numbers less than 999x10 9. Computations involving large numbers thus result in inaccuracies, called round-off error.

5 Floats can have round-off error (2) The maximum value that can be represented here is: 999 x 10 9, but the maximum precision that can be maintained is only three digits - the length of the mantissa. For example, we could not store the number 425,735,485,001 in that format - we could only approximate it with 426e9 So floating point data types can hold larger numbers than integer types of the same length. But a floating point type offers no more precision than an integer type of the same length.

6 Declarations Floating point numbers are declared in C using the float keyword. For example, the following declares a floating point variable named balance. float balance; To maintain higher-precision numbers, we must increase the storage size, and this is the purpose of yet another data type provided by the C programming language: the double precision type. double velocity=14354.3;

7 Bit Manipulation All data is represented internally as sequences of bits - C has some operators for bit manipulation OperatorNameDescription & bitwise ANDThe bits in the result are set to 1 if the corresponding bits in the two operands are both 1. | bitwise ORThe bits in the result are set to 1 if at least one of the corresponding bits in the two operands is 1. ^ bitwise exclusive ORThe bits in the result are set to 1 if exactly one of the corresponding bits in the two operands is 1. << left shiftShifts the bits of the first operand left by the number of bits specified by the second operand; fill from right with 0 bits. >> right shiftShifts the bits of the first operand right by the number of bits specified by the second operand; the method of filling from the left is machine dependent. ~ One’s complementAll 0 bits are set to 1 and all 1 bits are set to 0.

8 Enumeration constants Set of integer constants represented by identifiers Enumeration constants are like symbolic constants whose values are automatically set Values start at 0 and are incremented by 1 Values can be set explicitly with = Need unique constant names Example: enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; Creates a new type enum Months in which the identifiers are set to the integers 1 to 12


Download ppt "CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004."

Similar presentations


Ads by Google