float Data Type - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

float Data Type

Description:

float Data Type – PowerPoint PPT presentation

Number of Views:110
Avg rating:3.0/5.0
Slides: 20
Provided by: evank
Category:
Tags: converter | data | float | money | type

less

Transcript and Presenter's Notes

Title: float Data Type


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 ltstdio.hgt
  • 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 276.50
3
Example Finding an Average
  • Suppose you want to determine a students
    average.
  • int num 4
  • float avg 909295100/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 (909295100)/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 94.25.
  • 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 (909295100)/num

6
Rules of Promotion
  • 2. Use a Cast Operator
  • int num 4
  • float avg (909295100)/(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 (909295100) / 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 94.25.
  • 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.89999999999999999999999
  • 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) lt
  • .00001) )
  • printf( OK, close enough to equal
  • for me!\n )
  • Note you have to check for the reverse too

10
Comparing floats contd
  • Complete working examplefloat fDollarDiff 0
  • fDollarDiff fDollarsReceived
  • fDollarsExpected
  • if ( -.00001 lt fDollarDiff
  • fDollarDiff lt .00001 )
  • printf( OK, close enough to equal
  • for me!\n )

11
Good case for a constant
  • By the way that .00001 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 contd
  • 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 contd
  • To use precision, place a decimal point (.)
    followed by an integer representing the precision
    between the percent sign() and the conversion
    specifier (ie .2f)

17
switch Multiple-Selection Structure
  • Used when testing a variable or expression for
    EQUALITY (ie no gt, lt, gt, lt 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.

18
switch Multiple-Selection Structure
  • switch (expression)
  • case value1
  • action(s)
  • break
  • case value2
  • action(s)
  • break
  • case default
  • actions(s)
  • break

keyword switch
expression can be a variable or a more
complicated expression
could use more than one case if the same actions
are required
actions within a single case do not need brackets
the default case will be executed in the event
that no other case is
19
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.
Write a Comment
User Comments (0)
About PowerShow.com