• Main Page
  • Files
  • File List
  • File Members

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

Go to the documentation of this file.
00001 #include "MusimatTutorial.h"
00002 MusimatTutorialSection(B0115) {
00003         Print("*** B.1.15 Functions ***");
00004         /*****************************************************************************
00005          
00006          B.1.15 Functions
00007          
00008          Functional notation in mathematics allows us to encapsulate and name arithmetic expressions. For 
00009          example, if we have defined the function f (a, b) = a + b, then f stands for a + b. The value or values 
00010          in parentheses after a function name, called arguments, supply the function with inputs. Functions 
00011          also typically return a result. For example, using this definition of f, 7 = f (3, 4). 
00012          
00013          Programming languages typically come with a set of predefined functions for the most common 
00014          necessities, and they also allow new functions to be created. For example, in Musimat all oper-
00015          ators also have a functional representation, so writing
00016          *****************************************************************************/
00017         
00018         Real x = Divide(11.0, 4.0);
00019         Print("x=", x);
00020         
00021         /*****************************************************************************
00022          is the same as writing
00023          *****************************************************************************/
00024         
00025         Real e = 11.0/4.0;
00026         Print("e=", e);
00027         
00028         /*****************************************************************************
00029          In this case, e is set to the quotient, 2.75. Real division is performed because both numerator 
00030          and denominator are reals. If we want to perform integer division, both numerator and denominator 
00031          must be integers. We could write
00032          *****************************************************************************/
00033         
00034         Integer f = Divide(11, 4);
00035         Print("f=", f);
00036         
00037         /*****************************************************************************
00038          or equivalently,
00039          *****************************************************************************/
00040         
00041         Integer g = 11/4;
00042         Print("g=", g);
00043         
00044         /*****************************************************************************
00045          In either case, f and g are set to the quotient, 2, and the remainder is discarded. To get the remainder after 
00046          integer division we can write
00047          *****************************************************************************/
00048         
00049         Integer h = Mod( 11, 4 ); // remainder of integer division 
00050         Print("h=", h);
00051         
00052         /*****************************************************************************
00053          The variable h is set to 3, the remainder of 11/4. For positive integers m and n, Mod(m, n) 
00054          lies between 0 and n - 1. Why is this function called Mod instead of, say, Remainder? See appen-
00055          dix A, A.6. The equivalent operator form for remaindering also looks a little strange: 
00056          *****************************************************************************/
00057         
00058         Integer i = 11 % 4;
00059         Print("i=", i);
00060         
00061         /*****************************************************************************
00062          The % sign does not have its usual meaning of "percent" in Musimat. Instead, it means "remain-
00063          der of integer division." Mod and % can only be applied to integer operands.
00064          
00065          Some useful built-in functions are not associated with operators. Exponentiation is performed 
00066          by the function Pow(). These three statements,
00067          *****************************************************************************/
00068         
00069         Real base = 10.0;
00070         Real exp = 2.0;
00071         Real j = Pow(base, exp);
00072         Print("j=", j);
00073         
00074         /*****************************************************************************
00075          are equivalent to writing "j = 10.0 to the power of 2.0", and the result stored in j is 100.0. 
00076          Going the other way: Log10(x) is equivalent to "log base 10 of x", and
00077          *****************************************************************************/ 
00078         
00079         Real k = Log10(100.0); 
00080         Print("k=", k);
00081         
00082         /*****************************************************************************
00083          sets k to 2.
00084          
00085          Another built-in function, Print(), allows us to observe the value of a variable or expression. 
00086          When executed, the statements
00087          *****************************************************************************/
00088         
00089         Real l = 11.0/4.0;
00090         Print("l=", l);
00091         
00092         /*****************************************************************************
00093          display the value of l, or 2.75. The way in which the value is displayed varies with the type of the 
00094          expression and the type of computer. If the computer is a person, for example, he or she might say "two 
00095          point seven five." If it is an electromechanical computer, it might show the value on a display screen.
00096          
00097          When the predefined function Halt() is executed, the method in progress stops at that step in 
00098          the program. The argument to Halt(), if any, can be used to indicate the answer or result obtained 
00099          by the program up to that point.
00100          
00101          One final built-in function is Random(), which returns a real number in the range of 0.0 to 1.0 
00102          chosen at random.
00103          *****************************************************************************/
00104 }
00105 
00107 /* $Revision: 1.2 $ $Date: 2006/09/05 06:32:25 $ $Author: dgl $ $Name:  $ $Id: B0115.cpp,v 1.2 2006/09/05 06:32:25 dgl Exp $ */
00108 // The Musimat Tutorial © 2006 Gareth Loy
00109 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 
00110 // and published exclusively by The MIT Press.
00111 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00112 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00113 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00114 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00115 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00116 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00117 // The Musimat website:                 http://www.musimat.com/
00118 // This program is released under the terms of the GNU General Public License
00119 // available here:                      http://www.gnu.org/licenses/gpl.txt
00120 

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