Recall how we did yards, feet, and inches in lecture:
// get whole measurement in inches yards = inches / inches_p_yrd; // retrieve whole yards inches = inches % inches_p_yrd; // remove whole yards to get // leftover inches feet = inches / inches_p_ft; // retrieve whole feet inches = inches % inches_p_ft; // remove whole feet to get // leftover inches
See how pairs of /% operations are used to retrieve a whole number of parts and then get what's left of the smaller unit. Or, pictorially:
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
49 inches. Counting out the whole yards:
IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII IIIIIIIIIIIII Y IIIIIIIIIIIII
Leaves 13 inches. The only trick in C++ is that it takes two operations to do this retrieve and leftover: / and then %. Again, we have 13 inches, taking out the whole feet:
IIIIIIIIIIII I F I
Leaves 1 inch. Mathematically what we did was:
__1_R13 49 = 36*1 + 13 36 / 49 36 ---- 13 __1_R1 13 = 12*1 + 1 12 / 13 12 ---- 1
But, in C++, we are a little more compact:
1 = 49 / 36 13 = 49 % 36 1 = 13 / 12 1 = 13 % 12
This pattern can be followed for any set of unit conversions: metric, English, monetary, etc. And it extends to any number of units:
// get whole measurement in inches miles = inches / inches_p_mi; // retrieve whole miles inches = inches % inches_p_mi; // remove whole miles to get // leftover inches yards = inches / inches_p_yrd; // retrieve whole yards inches = inches % inches_p_yrd; // remove whole yards to get // leftover inches feet = inches / inches_p_ft; // retrieve whole feet inches = inches % inches_p_ft; // remove whole feet to get // leftover inches
Even better, you can even skip parts:
// get whole measurement in inches inches = inches % inches_p_yrd; // remove whole miles, yards, etc. // to get leftover inches feet = inches / inches_p_ft; // retrieve whole feet inches = inches % inches_p_ft; // remove whole feet to get // leftover inches
(This only works when the constants being divided by are multiples of each other -- related, if you will.)
There are two forms of typecasting: C-style and (new) C++-style. C-style places the datatype you are casting to and places it in parentheses before the item (variable/expression) to be cast:
yard_frac = (double)inches/inches_p_yrd; or grow_rate = (double)new_popul/old_popul;
The (new) C++-style typecast uses the static_cast
keyword:
yard_frac = static_cast<double>(inches)/inches_p_yrd; or grow_rate = static_cast<double>(new_popul)/old_popul;
Both work when casting to any datatype, one is just shorter.
Warning: It can be easy to slip with the parentheses:
yard_frac = static_cast<double>(inches/inches_p_yrd); or grow_rate = static_cast<double>(new_popul/old_popul);
Here we are performing an integer division and then casting that integer result to a double. We've still lost the decimal places we were after!
But you can screw up with the C-style (it's just more difficult), too:
average = (double)(min + max)/2;
Might be mis-typed as:
average = (double)min + max/2;
But that isn't really the typecast's fault -- it was an algebra error!