~ubuntu-branches/ubuntu/trusty/c++-annotations/trusty

« back to all changes in this revision

Viewing changes to yo/first/staticcast.yo

  • Committer: Package Import Robot
  • Author(s): Frank B. Brokken
  • Date: 2011-09-12 16:08:05 UTC
  • mfrom: (1.1.17 upstream)
  • Revision ID: package-import@ubuntu.com-20110912160805-r9dq68beojgzuien
Tags: 9.0.2-1
New upstream release (editorial changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
both are numbers, but as the tt(int) has no fractions precision is potentially
6
6
reduced. But the converse also holds true. When the quotient of
7
7
two tt(int) values must be assigned to a tt(double) the fraction part of the
8
 
division will get lost unless a cast is used.
 
8
division is lost unless a cast (of either the numerator or denominator to
 
9
tt(double)) is used.
9
10
 
10
11
Here is an example of such a cast (assuming tt(quotient) is of type
11
12
tt(double) and tt(lhs) and tt(rhs) are tt(int)-typed variables):
12
13
        centt(quotient = static_cast<double>(lhs) / rhs;)
13
 
    If the cast is omitted, the division operator will ignore the remainder as
 
14
    If the cast is omitted, the division operator ignores the remainder as
14
15
its operands are tt(int) expressions. Note that the division should be put
15
16
outside of the cast expression. If the division is put inside (as in
16
 
tt(static_cast<double>(lhs / rhs))) an em(integer division) will have been
 
17
tt(static_cast<double>(lhs / rhs))) an em(integer division) has already been
17
18
performed em(before) the cast has had a chance to convert the type of an
18
19
operand to tt(double).
19
20
 
41
42
    Casts like these provide information to the compiler about how to handle
42
43
the provided data. Very often (especially with data types differing only in
43
44
size but not in representation) the cast won't require any additional
44
 
code. Additional code will be required, however, to convert one representation
 
45
code. Additional code is  required, however, to convert one representation
45
46
to another, e.g., when converting tt(double) to tt(int).