Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

stuff.h

Go to the documentation of this file.
00001 #ifndef stuff_h
00002 #define stuff_h
00003 
00005 //
00006 // stuff.h
00007 //
00008 // Author: Daniel Flath 
00009 // Date:   2000/03/01
00010 //
00012 
00013 namespace stuff {
00014 
00016 //
00017 // Function: Round
00018 //
00019 // Input:
00020 //   float num:  input number to be rounded
00021 //
00022 // Return value:
00023 //   The rounded integer equivalent of num.
00024 //
00026 int Round(float num) {
00027   int retVal;
00028   int bNegative;
00029 
00030   if (num < 0) {
00031     num *= -1;
00032     bNegative = 1;
00033   } else {
00034     bNegative = 0;
00035   }
00036 
00037   int numFloor = (int)num;
00038   int numCeil = (int)(num + 1);
00039   if ((num - (float)numFloor) < ((float)numCeil - num))
00040     retVal = numFloor;
00041   else
00042     retVal =  numCeil;
00043 
00044   return bNegative ? -retVal : retVal;
00045 }
00046 
00047 
00049 //
00050 // Function: IntToStr
00051 //
00052 // Input:
00053 //   int in:     number to convert
00054 //   char *buf:  string to store result in
00055 //   int length: size of buf
00056 //
00057 // Return value:
00058 //   Number of characters written to buf excluding the null
00059 //   terminator.
00060 //
00062 int IntToStr(int in, char *buf, int length) {
00063     if (length > 1) {
00064         // simple case:
00065         if (in == 0) {
00066             buf[0] = '0';
00067             buf[1] = 0;
00068             return 1;
00069         }
00070 
00071         int len = 0;
00072         int temp = in;
00073         if (temp == 0)
00074             len = 1;
00075         while (temp != 0) {
00076             temp /= 10;
00077             len++;
00078         }
00079         if (in < 0) {
00080             len++;
00081         }
00082 
00083         len = (len < length - 1) ? len : length - 1;
00084 
00085         char *ch = buf + len - 1;
00086         
00087         temp = in;
00088         if (temp < 0)
00089             temp *= -1;
00090 
00091         int chint = 0;
00092         while (temp != 0) {
00093             chint = temp % 10;
00094             temp /= 10;
00095             ch[0] = chint + 48;
00096             ch--;
00097         } 
00098         
00099         buf[len] = 0;
00100         if (in < 0) {
00101             buf[0] = '-';            
00102         }
00103 
00104         return len;
00105     }
00106     else
00107         return 0;
00108 }
00109 
00110 } // end namespace stuff
00111 #endif

Generated at Wed Nov 21 12:22:45 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000