• Main Page
  • Files
  • File List
  • File Members

/Users/garethloy/Musimathics/Musimat1.2/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         Print( 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         Print( 26/Integer(2.5) );
00033         
00034         /*****************************************************************************
00035          prints 13, but
00036          *****************************************************************************/
00037         
00038         Print( Integer(26/2.5) );
00039         
00040         /*****************************************************************************
00041          prints 10.
00042          *****************************************************************************/
00043 }
00044 
00045 
00047 /* $Revision: 1.2 $ $Date: 2006/09/05 06:32:25 $ $Author: dgl $ $Name:  $ $Id: B0113.cpp,v 1.2 2006/09/05 06:32:25 dgl Exp $ */
00048 // The Musimat Tutorial � 2006 Gareth Loy
00049 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" � 2006 Gareth Loy 
00050 // and published exclusively by The MIT Press.
00051 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00052 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00053 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00054 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00055 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00056 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00057 // The Musimat website:                 http://www.musimat.com/
00058 // This program is released under the terms of the GNU General Public License
00059 // available here:                      http://www.gnu.org/licenses/gpl.txt
00060 

Generated on Fri Nov 26 2010 16:18:25 for Musimat Tutorial by  doxygen 1.7.2