1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning below) –distance –weight, etc.
2 Float Example (Program 2.2) /* Float Example Program */ #include int main () { float var1, var2, var3, sum; var1 = 87.25; var2 = 92.50; var3 = 96.75; sum = var1 + var2 + var3; printf ("Sum: %.2f", sum); } %f: indicates floating values %.2f displays a floating point value with 2 decimal points. Output: Sum:
3 Example: Finding an Average Suppose you want to determine a student’s average. int num = 4; float avg = /num; Problem #1: Operator Precedence –By rules of operator precedence, 100/4 is evaluated first. Hence, avg is set to: 302. –To solve this problem, use (): float avg = ( )/num;
4 Finding an Average Problem #2: –90, 92, 95, 100 and 4 are all integers. Hence, this is integer division. –Integer division can result in data truncation (or loss of data.) –Hence, avg is set to: 94.0, but the students real average is –There are actually three ways to solve this problem.
5 Rules of Promotion Promotion: when mixing ints and floats, everything is promoted to floats. In our average example, there are three ways to force promotion, and get the right answer of 94.25: 1. change num to a float: float num = 4.0; float avg = ( )/num;
6 Rules of Promotion 2. Use a Cast Operator int num = 4; float avg = ( )/(float)num; In this case, num is explicitly cast to a float. And, because we have one float, everything else is promoted. Note that you can also use the (int) cast to cast a float to an integer. 3. Divide by 4.0 float avg = ( ) / 4.0;
7 Finding an Average Problem #2: –90, 92, 95, 100 and 4 are all integers. Hence, this is integer division. –Integer division can result in data truncation (or loss of data.) –Hence, avg is set to: 94.0, but the students real average is –There are actually three ways to solve this problem.
8 NEVER USE floats HERE: In a condition for equality comparison –Floats may be represented differently from what you think by the computer E.g. 1.9 to you may be –1.9 will not necessarily equal 1.9! In critical calculations for the same reason –E.g..1 added 10 times often will not add up to 1 –Use long ints instead and keep track of where your decimal point is (e.g. $1.75 should be stored as 175)
9 Comparing floats If you need to compare floats, determine some threshold and test against that E.g. if ( (fDollarsReceived – fDollarsExpected) <=.00001) ) { printf( “OK, close enough to equal “ “for me!\n” ); } Note you have to check for the reverse too
10 Comparing floats cont’d Complete working example float fDollarDiff = 0 ; fDollarDiff = fDollarsReceived – fDollarsExpected ; if ( <= fDollarDiff && fDollarDiff <= ) { printf( “OK, close enough to equal “ “for me!\n” ); }
11 Good case for a constant By the way that is a prime candidate for #define! –E.g #define f_ACCEPTABLE_THRESHOLD.00001
12 printf conversion specifications The format control string (the first argument to printf) contains: –conversion specifiers (%d, %i, %f) –field widths –precisions –other info we will not look at
13 Field width The exact size of the field in which data is to be printed is specified by a field width. If the field width is larger than the data being printed, the data will normally be right- justified within that field. Use a “-” sign before the number in field width to left-justify If the field width is smaller than the data being printed, the field width is increased to print the data.
14 Field width cont’d An integer representing the field width is inserted between the percent sign (%) and the conversion specifier –For example, %4d int iYourAnswer = 1 ; printf( “The answer %4d is correct!\n”, iYourAnswer ); Will print the following The answer 1 is correct!
15 Precision You can also specify the precision with which data is to be printed. Precision has different meaning -- for different types. –integer: minimum number of digits to be printed –float: number of digits to appear after the decimal point
16 Precision cont’d To use precision, place a decimal point (.) followed by an integer representing the precision between the percent sign(%) and the conversion specifier (ie %.2f)
switch Multiple-Selection Structure Used when testing a variable or expression for EQUALITY (ie no >, =, <= tests) separately for each of the constant integral values it may assume. Preferred over if else in situations where you are testing the same expressions for equality with many different values. Allows you to perform different actions for each test.
switch Multiple-Selection Structure switch (expression){ case value1: action(s); break; case value2: action(s); break; … case default: actions(s); break; } keyword switch could use more than one case; if the same actions are required expression can be a variable or a more complicated expression actions within a single case do not need brackets the default case will be executed in the event that no other case is
beware of “fall through” If you forget to use the break keyword between cases, unexpected things may happen. Once a case tests true, all the statements following that case, will be executed until the next break. Experienced programmers may use this on purpose. For this class we will not.