C:/Musimathics_local/Musimat/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 
00031 /*****************************************************************************
00032 the integer value of b (which is 3) is converted to the equivalent Real value (3.0), which is stored 
00033 in Real variable c.
00034 
00035 Converting from Real to Integer, we have some choices. For example, if
00036 *****************************************************************************/
00037 
00038 Real p = 10.0/3.0;   // Real variable a is set to 3.333 . . .
00039 
00040 /*****************************************************************************
00041 then
00042 *****************************************************************************/
00043 
00044 Real d = Floor(p); // d is set to 3.0
00045 
00046 /*****************************************************************************
00047 sets d to 3.0. The built-in Floor() function returns the largest integer less than its Real argu-
00048 ment. The statement
00049 *****************************************************************************/
00050 
00051 Real x = Ceiling(p);
00052 
00053 /*****************************************************************************
00054 sets x to 4 because the built-in Ceiling() function returns the smallest integer greater than its 
00055 argument.
00056 
00057 We can round a Real to the nearest whole number as follows:
00058 *****************************************************************************/
00059 
00060 Real r = Floor(p + 0.5);                                                        // round c to the nearest whole number
00061 
00062 /*****************************************************************************
00063 If a = 2.4, then Floor(a + 0.5) returns 2.0. But if a = 2.5, Floor(a + 0.5) returns 
00064 3.0. Floor(a + 0.5) returns 2.0 for any value a in the range 2.0 to 2.499... and returns 
00065 3.0 for any value a in the range 2.5 to 2.999.... But we don’t have to do rounding ourselves, 
00066 Musimat has a built-in function:
00067 *****************************************************************************/
00068 
00069 Print(Round(2.49999));                                                  // prints 2.0
00070 
00071 }}
00072 
00074 /* $Revision: 1.4 $ $Date: 2006/09/12 17:38:00 $ $Author: dgl $ $Name:  $ $Id: _b0123_8cpp-source.html,v 1.4 2006/09/12 17:38:00 dgl Exp $ */
00075 // The Musimat Tutorial © 2006 Gareth Loy
00076 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 
00077 // and published exclusively by The MIT Press.
00078 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00079 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00080 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00081 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00082 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00083 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00084 // The Musimat website:                 http://www.musimat.com/
00085 // This program is released under the terms of the GNU General Public License
00086 // available here:                      http://www.gnu.org/licenses/gpl.txt
00087 

Generated on Tue Sep 12 10:14:24 2006 for Musimat Tutorial by  doxygen 1.4.7