- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/MusimatLib/Musimat.cpp

Go to the documentation of this file.

    00001 /* $Revision: 1.2 $ $Date: 2006/09/09 06:22:53 $ $Author: dgl $ $Name:  $ $Id: _musimat_8cpp-source.html,v 1.2 2006/09/09 06:22:53 dgl Exp $ */
    00002
    00003 // The Musimat Tutorial © 2006 Gareth Loy
    00004 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy
    00005 // and published exclusively by The MIT Press.
    00006 // This program is released WITHOUT ANY WARRANTY; without even the implied
    00007 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00008 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
    00009 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
    00010 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
    00011 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
    00012 // The Musimat website:                 http://www.musimat.com/
    00013 // This program is released under the terms of the GNU General Public License
    00014 // available here:                      http://www.gnu.org/licenses/gpl.txt
    00015
    00019
    00020 #include <string.h>
    00021 #include "Musimat.h"
    00022
    00023 Integer Length( String s ) { Return( Integer( strlen(s)+1 ) ); }
    00024
    00034 Integer RealToRational( Real f, Integer Reference a, Integer Reference b ) {
    00035         If (f == 0.0) {
    00036                 a = 0;
    00037                 b = 1;
    00038                 Return( 0 );
    00039         } Else If (f == 1.0) {
    00040                 a = b = 1;
    00041                 Return( 0 );
    00042         }
    00043
    00044         Const Integer count = 3000000;
    00045         Const Real limit = 0.0000001 /* or try: LDBL_EPSILON * 10000 */;
    00046
    00047         If (f < limit) {
    00048                 a = 1;
    00049                 b = count;
    00050                 Return( 0 );
    00051         }
    00052
    00053         a = b = 1;      // start off with a ratio of 1/1
    00054         Integer i;
    00055         For (i = 0; i < count; i++) {
    00056                 If ( Abs(Real(a)/Real(b) - f) < limit )
    00057                         Return i;
    00058                 Else {
    00059                         Real x = Abs( Real(a+1)/Real(b) - f );
    00060                         Real y = Abs( Real(a)/Real(b+1) - f );
    00061                         If (x < y)
    00062                                 a++;
    00063                         Else
    00064                                 b++;
    00065                 }
    00066         }
    00067         Return i; // If we get here, we've not converged in the number of cycles available
    00068 }
    00069
    00070 Void About()
    00071 {
    00072 Print("The Musimat Tutorial (©) 2006 Gareth Loy");
    00073 Print("Derived from Chapter 9 and Appendix B of \"Musimathics Vol. 1\" © 2006 Gareth Loy");
    00074 Print("and published exclusively by The MIT Press.");
    00075 Print("This program is released WITHOUT ANY WARRANTY; without even the implied");
    00076 Print("warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
    00077 Print("For information on usage and redistribution, and for a DISCLAIMER OF ALL");
    00078 Print("WARRANTIES, see the file, LICENSE.txt, in this distribution.");
    00079 Print("\"Musimathics\" is available here:       http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916");
    00080 Print("Gareth Loy's Musimathics website: http://www.musimathics.com/");
    00081 Print("The Musimat website:     http://www.musimat.com/");
    00082 Print("This program is released under the terms of the GNU General Public License");
    00083 Print("available here: http://www.gnu.org/licenses/gpl.txt");
    00084 }
    00085