- Main Page
- Namespaces
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/MusimatTutorial/B0113.cpp

Go to the documentation of this file.

    00001 #include "MusimatTutorial.h"
    00002 MusimatTutorialSection(B0113) {
    00003 Print("*** B.1.13 Type Promotion and Type Coercion ***");
    00004 /*****************************************************************************
    00005
    00006 B.1.13 Type Promotion and Type Coercion
    00007
    00008 What if the values in an expression are not of the same type? For example, since both operands
    00009 in the expression 2/3 are integers, the quotient will be an integer. The quotient of 4.5/2.25 will
    00010 be a real number because both operands are reals. But what is the quotient of 2/2.25? Our options
    00011 are to coerce the numerator to be a Real and then perform real division, or coerce the denominator
    00012 to be an Integer and then perform integer division. Which shall it be?
    00013
    00014 Since the set of all reals includes the set of all integers, it makes sense to promote the integer
    00015 2 to the corresponding real value 2.0 and then perform real division. Musimat automatically con-
    00016 verts 2/2.25 into 2.0/2.25 and then performs real division. In general, integer values are auto-
    00017 matically promoted to reals wherever they occur in an expression with reals.
    00018
    00019 If automatic type promotion is not desired, the type of an expression can be coerced by directly
    00020 indicating its type. Consider the expression:
    00021 *****************************************************************************/
    00022
    00023 10/Integer(3.33);
    00024
    00025 /*****************************************************************************
    00026 First, the real value 3.33 is truncated to the integer value 3, then because both numerator and
    00027 denominator are now integers, integer division is performed. Beware of things being done for you
    00028 automatically by computers! You still must pay attention to head off unintended consequences.
    00029 Consider:
    00030 *****************************************************************************/
    00031
    00032 26/Integer(2.5);
    00033
    00034 /*****************************************************************************
    00035 equals 13, but
    00036 *****************************************************************************/
    00037
    00038 Integer(26/2.5);
    00039
    00040 /*****************************************************************************
    00041 equals 10.
    00042
    00043 *****************************************************************************/
    00044 }}
    00045
    00046
    00048 /* $Revision: 1.1 $ $Date: 2006/09/08 19:11:31 $ $Author: dgl $ $Name:  $ $Id: _b0113_8cpp-source.html,v 1.1 2006/09/08 19:11:31 dgl Exp $ */
    00049 // The Musimat Tutorial © 2006 Gareth Loy
    00050 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy
    00051 // and published exclusively by The MIT Press.
    00052 // This program is released WITHOUT ANY WARRANTY; without even the implied
    00053 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00054 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
    00055 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
    00056 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
    00057 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
    00058 // The Musimat website:                 http://www.musimat.com/
    00059 // This program is released under the terms of the GNU General Public License
    00060 // available here:                      http://www.gnu.org/licenses/gpl.txt
    00061