Increment - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

Increment

Description:

Consider the fraction 1/3. In decimal, it is represented as ... computer stores real numbers up to only 6 decimal places, and we run the ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 9
Provided by: ianmar
Category:

less

Transcript and Presenter's Notes

Title: Increment


1
Increment Decrement OperatorsRounding Errors
ECOR 1606 - Problem Solving and Computers
Carleton University Department of Systems and
Computer Engineering
2
Increment and Decrement Operators
  • Example

int main() int a 5 a a--
  • equivalent to a a 1
  • increases the value of the variable a by 1
  • equivalent to a a - 1
  • decreases the value of the variable a by 1
  • Most commonly used in for loops

int main() int i for (i 0 i lt 10
i) // do something 10 times
  • Only use with integer variables.

3
Postfix Increment
  • Can be used in expressions
  • e.g. assignment statements
  • e.g. function calls
  • These are examples of "postfix-increment"
  • the value is used, then the variable is
    incremented

int a 5, b b a
value of variable a (e.g. 5) is copied to b, then
a is incremented (e.g. to 6).
int a 5 funct(a)
value of variable a (e.g. 5) is passed to
function funct( ), then a is incremented (e.g. to
6).
4
Prefix Increment
  • Alternative "prefix-increment"
  • increment the variable, then use its value
  • put before the variable name
  • e.g. assignment statements
  • e.g. function calls

int a 5, b b a
the variable a is incremented, then the new value
of a (e.g. 6) is copied to b.
the variable a is incremented, then the new value
of a (e.g. 6) is passed to function funct( ).
int a 5 funct(a)
5
Postfix and Prefix Decrement
  • Can also use postfix- and prefix-decrement
  • e.g.

int a 5, b b a--
int a 5, b b --a
produces
produces
b 5 a 4
b 4 a 4
6
Floating-point Number Representation Error
  • Consider the fraction 1/3. In decimal, it is
    represented as
  • 0.333333333333333333333333333333333333333
  • This number can not be exactly represented with
    finite precision (a finite number of places after
    the decimal point).
  • If we were only allowed, for example, six places
    after the decimal point, the closest we could
    come to representing 1/3 is
  • 0.333333
  • Suppose our computer stores real numbers up to
    only 6 decimal places, and we run the following
    program fragment

double x, y x 1.0 / 3.0 y x
3.0 if (y 1.0) // do
something
x 0.333333 y 0.999999 y ! 1.0
true or false?
7
Floating-point Number Representation Error
  • Another example

double x for (x 0.0 x ! 1.0 x
1.0 / 3.0) cout ltlt "x " ltlt x ltlt
endl
what happens?
x 0.000000
x 0.333333
x 0.666666
x 0.999999
x 1.333332
x 1.666665
etc. Infinite loop!
8
Floating-point Number Representation Error
  • Computers actually store floating-point numbers
    in binary, so even non-repeating decimal numbers
    may require infinite precision in binary
  • e.g., The closest that a type double variable can
    come to representing the decimal number 0.1 is
  • 0.1000000000000000055513312
  • As a result, you should never make exact
    comparisons between floating-point numbers, but
    make approximate comparisons.

16 zeros
if (angle 90.0) // this is a right
angle
if (fabs(angle - 90.0) lt 1e-6) // this is
a right angle
Bad You'll never get equality
Good You can get arbitrarily close to a right
angle
89.999999 lt angle lt 90.000001
Write a Comment
User Comments (0)
About PowerShow.com