/* This function prints a number in decimal format */
void print_number(int n)
{
  int digit, numDigits, divisor, remainder;

  numDigits = countNumDigits(n);
  divisor = powerOfTen(numDigits - 1);
  remainder = n;

  for (;numDigits > 0; numDigits--) {
    digit = remainder / divisor;
    print_char('0' + digit);
    remainder -= (digit * divisor);
    divisor = divisor / 10;
  }
}
Ejemplo n.º 2
0
std::pair<std::string,std::pair<unsigned,std::string> > 
decomposeDirectScaledUnit(const std::string& s) {
  if (!isDirectScaledUnit(s)) {
    LOG_FREE_AND_THROW("openstudio.QuantityRegex","Cannot decompose " << s
      << " into a numerator and scaled denominator because it is not an direct scaled unit.");
  }
  std::pair<std::string,std::pair<unsigned,std::string> > result;
  boost::match_results<std::string::const_iterator> match;

  boost::regex_search(s,match,regexDirectScaledUnit());
  result.first = std::string(match[1].first,match[1].second);
  std::string powerOfTen(match[2].first,match[2].second);
  result.second.first = powerOfTen.size() - 1;
  result.second.second = std::string(match[3].first,match[3].second);

  return result;
}
Ejemplo n.º 3
0
double sqrt(double a)
{
   /*
         find more detail of this method on wiki methods_of_computing_square_roots
         *** Babylonian method cannot get exact zero but approximately value of the square_root
    */
    double z = a;
    double rst = 0.0;
    int max = 8;     // to define maximum digit
    int i;
    double j = 1.0;
    for(i = max ; i > 0 ; i--){
        // value must be bigger then 0
        if(z - (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)) >= 0)
        {
            while( z - (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)) >= 0)
            {
                j++;
                if(j >= 10) break;
            }
            j--; //correct the extra value by minus one to j
            z -= (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)); //find value of z

            rst += j * powerOfTen(i);     // find sum of a
            j = 1.0;


          }

     }

     for(i = 0 ; i >= 0 - max ; i--){
         if(z - (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)) >= 0)
         {
             while( z - (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)) >= 0)
             {
                 j++;
             }
             j--;
             z -= (( 2 * rst ) + ( j * powerOfTen(i)))*( j * powerOfTen(i)); //find value of z

             rst += j * powerOfTen(i);     // find sum of a
             j = 1.0;
          }
     }
     // find the number on each digit
     return rst;
}