• Main Page
  • Files
  • File List
  • File Members

/Users/garethloy/Musimathics/Musimat1.2/MusimatTutorial/B0123.cpp

Go to the documentation of this file.
00001 #include "MusimatTutorial.h"
00002 MusimatTutorialSection(B0123) {
00003         Print("*** B.1.23 Type Conversion ***");
00004         /*****************************************************************************
00005          
00006          B.1.23 Type Conversion
00007          
00008          We can explicitly convert Integer expressions to Real, and vice versa. For example:
00009          *****************************************************************************/
00010         
00011         Real a = 10.0/3.0;
00012         Print(a);
00013         
00014         /*****************************************************************************
00015          prints 3.333 . . . , and
00016          *****************************************************************************/
00017         
00018         Integer b = Integer(a);                                                                 // convert a to Integer
00019         Print(b);
00020         
00021         /*****************************************************************************
00022          prints 3.
00023          
00024          When assigning a to b, the Real value a is converted to an Integer by truncating (discarding) 
00025          the fractional part of a (that is, by discarding 0.333…), and the integer residue (3) is assigned to 
00026          b. If we then write
00027          *****************************************************************************/
00028         
00029         Real c =  Real(b); 
00030         Print(c);
00031         
00032         /*****************************************************************************
00033          the integer value of b (which is 3) is converted to the equivalent Real value (3.0), which is stored 
00034          in Real variable c.
00035          
00036          Converting from Real to Integer, we have some choices. For example, if
00037          *****************************************************************************/
00038         
00039         Real p = 10.0/3.0;   // Real variable a is set to 3.333 . . .
00040         
00041         /*****************************************************************************
00042          then
00043          *****************************************************************************/
00044         
00045         Real d = Floor(p); // d is set to 3.0
00046         Print(d);
00047         
00048         /*****************************************************************************
00049          sets d to 3.0. The built-in Floor() function returns the largest integer less than its Real argu-
00050          ment. The statement
00051          *****************************************************************************/
00052         
00053         Real x = Ceiling(p);
00054         Print(x);
00055         
00056         /*****************************************************************************
00057          sets x to 4 because the built-in Ceiling() function returns the smallest integer greater than its 
00058          argument.
00059          
00060          We can round a Real to the nearest whole number as follows:
00061          *****************************************************************************/
00062         
00063         Real r = Floor(p + 0.5);                                                        // round c to the nearest whole number
00064         Print(r);
00065         
00066         /*****************************************************************************
00067          If a = 2.4, then Floor(a + 0.5) returns 2.0. But if a = 2.5, Floor(a + 0.5) returns 
00068          3.0. Floor(a + 0.5) returns 2.0 for any value a in the range 2.0 to 2.499... and returns 
00069          3.0 for any value a in the range 2.5 to 2.999.... But we don't have to do rounding ourselves, 
00070          Musimat has a built-in function:
00071          *****************************************************************************/
00072         
00073         Print(Round(2.49999));                                                  // prints 2.0
00074         
00075 }
00076 
00078 /* $Revision: 1.2 $ $Date: 2006/09/05 06:32:25 $ $Author: dgl $ $Name:  $ $Id: B0123.cpp,v 1.2 2006/09/05 06:32:25 dgl Exp $ */
00079 // The Musimat Tutorial © 2006 Gareth Loy
00080 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 
00081 // and published exclusively by The MIT Press.
00082 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00083 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00084 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00085 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00086 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00087 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00088 // The Musimat website:                 http://www.musimat.com/
00089 // This program is released under the terms of the GNU General Public License
00090 // available here:                      http://www.gnu.org/licenses/gpl.txt
00091 

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