当前位置: 首页>>代码示例>>C++>>正文


C++ Number类代码示例

本文整理汇总了C++中Number的典型用法代码示例。如果您正苦于以下问题:C++ Number类的具体用法?C++ Number怎么用?C++ Number使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Number类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: add

Number add(Number n1, Number n2)
{
	return Number(n1.get_x() + n2.get_x());
}
开发者ID:maxchv,项目名称:IntruductionSummer2015,代码行数:4,代码来源:OverloadOp.cpp

示例2:

Number Number::operator-(const Number num1) const 
{
	return calculator.sub(value, num1);
}
开发者ID:Westerdals,项目名称:pg4400-12-C---Programmering,代码行数:4,代码来源:Number.cpp

示例3: op_numbers

    /* static function, throws OperationError, has no traces but optional pstate for returned value */
    Value_Ptr op_numbers(enum Sass_OP op, const Number& lhs, const Number& rhs, struct Sass_Inspect_Options opt, const ParserState& pstate, bool delayed)
    {
      double lval = lhs.value();
      double rval = rhs.value();

      if (op == Sass_OP::MOD && rval == 0) {
        return SASS_MEMORY_NEW(String_Quoted, pstate, "NaN");
      }

      if (op == Sass_OP::DIV && rval == 0) {
        std::string result(lval ? "Infinity" : "NaN");
        return SASS_MEMORY_NEW(String_Quoted, pstate, result);
      }

      size_t l_n_units = lhs.numerators.size();
      size_t l_d_units = lhs.numerators.size();
      size_t r_n_units = rhs.denominators.size();
      size_t r_d_units = rhs.denominators.size();
      // optimize out the most common and simplest case
      if (l_n_units == r_n_units && l_d_units == r_d_units) {
        if (l_n_units + l_d_units <= 1 && r_n_units + r_d_units <= 1) {
          if (lhs.numerators == rhs.numerators) {
            if (lhs.denominators == rhs.denominators) {
              Number_Ptr v = SASS_MEMORY_COPY(&lhs);
              v->value(ops[op](lval, rval));
              return v;
            }
          }
        }
      }

      Number_Obj v = SASS_MEMORY_COPY(&lhs);

      if (lhs.is_unitless() && (op == Sass_OP::ADD || op == Sass_OP::SUB || op == Sass_OP::MOD)) {
        v->numerators = rhs.numerators;
        v->denominators = rhs.denominators;
      }

      if (op == Sass_OP::MUL) {
        v->value(ops[op](lval, rval));
        v->numerators.insert(v->numerators.end(),
          rhs.numerators.begin(), rhs.numerators.end()
        );
        v->denominators.insert(v->denominators.end(),
          rhs.denominators.begin(), rhs.denominators.end()
        );
        v->reduce();
      }
      else if (op == Sass_OP::DIV) {
        v->value(ops[op](lval, rval));
        v->numerators.insert(v->numerators.end(),
          rhs.denominators.begin(), rhs.denominators.end()
        );
        v->denominators.insert(v->denominators.end(),
          rhs.numerators.begin(), rhs.numerators.end()
        );
        v->reduce();
      }
      else {
        Number ln(lhs), rn(rhs);
        ln.reduce(); rn.reduce();
        double f(rn.convert_factor(ln));
        v->value(ops[op](lval, rn.value() * f));
      }

      v->pstate(pstate);
      return v.detach();
    }
开发者ID:2ndWaveLLC,项目名称:dapprototype,代码行数:69,代码来源:operators.cpp

示例4: is_less

bool Number::is_less(Element *other) const {
	Number *otherValue = Element::as_number(other);
	if (!otherValue) { return false; }
	return _value < otherValue->value();
}
开发者ID:itmm,项目名称:myLisp,代码行数:5,代码来源:number.cpp

示例5: Number

Number operator + (const Number& x, const Number& y) {
	return  Number(x.GetNum() + y.GetNum(), x.GetMod());
}
开发者ID:Artem097,项目名称:RepoRepo,代码行数:3,代码来源:precycle.cpp

示例6: inverse

Number operator/( Number l, Number r)
{
	Number inverse(r.denominator(), r.numerator());	
	return l*inverse;
}
开发者ID:jwbaek,项目名称:LP-Solver,代码行数:5,代码来源:Number.cpp

示例7: print_number

static void print_number(const Number & n)
{
	print_ref(n);
	std::cerr << "+ type() : " << n.type() << std::endl;
}
开发者ID:moe123,项目名称:mirroring-coconut-gitlab,代码行数:5,代码来源:coconut_test.cpp

示例8: return

bool operator==( Number l, Number r ) { 
    return (l.numerator() == r.numerator() && l.denominator() == r.denominator());
}
开发者ID:jwbaek,项目名称:LP-Solver,代码行数:3,代码来源:Number.cpp

示例9: Number

Number operator+( Number l, Number r)
{
	int lnum = l.num;
	int lden = l.denom;
	int rnum = r.num;
	int rden = r.denom;
	
	// if one is whole number
	if (lden == 1)
	{
		return Number(lnum*rden + rnum, rden);
	}
	if (rden == 1)
	{
		return Number(rnum*lden + lnum, lden);
	}
	// sum of whole numbers
	int sumWhole = lnum/lden + rnum/rden;
	// what is left after subtracting whole number
	lnum = lnum%lden;
	rnum = rnum%rden;	

	// lMaxMul is the largest integer that you can multiply 
	// by lnum to not go over INT_MAX
	int lMaxMul = INT_MAX/abs(lnum);
	int rMaxMul = INT_MAX/abs(rnum);
	int gcdDenom = gcdFunction(lden,rden);

	// if numerators will not go over INT_MAX when
	// multiplied for common denom
	if (lMaxMul >= rden/gcdDenom && rMaxMul >= lden/gcdDenom)
	{
		// if the sum of numerators multiplied by their respective
		// factors will not go over INT_MAX
		if ( INT_MAX- lnum*(rden/gcdDenom) >= rnum*(lden/gcdDenom))
		{
			// numerator (without reducing)
			int OriginalNum = lnum*(rden/gcdDenom) + rnum*(lden/gcdDenom);
			// reduce with both lden and rden
			Number reduce1(OriginalNum, lden);
			Number reduce2(reduce1.numerator(), rden/gcdDenom);	
			return Number(reduce2.numerator() + sumWhole*(reduce1.denominator())*(reduce2.denominator()), (reduce1.denominator())*(reduce2.denominator()));
		} // if 
	} // if
	
	// else, sum of two numbers is over 1 (or under -1)
	l.num = lnum;
	r.num = rnum;
	Number fill = l.fill();
	// fill the first number so it reaches 1 or -1	
	// i.e. if l = 3/5 and r = 4/7, fill = 2/5
	// 3/5 + 4/7 = 3/5 + 2/5 - 2/5 + 4/7
	//           = 1 - 2/5 + 4/7
	if (lnum > 0)
		sumWhole++;
	else
		sumWhole--;	
	Number wholeNumber(sumWhole, 1);	
	Number sumFractions = r - fill;
	return wholeNumber + sumFractions;
}
开发者ID:jwbaek,项目名称:LP-Solver,代码行数:61,代码来源:Number.cpp

示例10: denom

// copy constructor
Number::Number(const Number &c) :
	num(c.numerator()), denom(c.denominator())
{}
开发者ID:jwbaek,项目名称:LP-Solver,代码行数:4,代码来源:Number.cpp

示例11: readCharacter

//-------------------------------------------------------------------------------------------------
// FuntionName: readCharacter
// Purpose: read each character input from stdin, then add it to number object
//          case charater is ' ' => build number.
//          case charater is '\n' => Calculate a sum of squares for Line, then loop for other Line
//          case charater is 'q' => end of test.
//          Other case will be reject.
//--------------------------------------------------------------------------------------------------
bool readCharacter( Number& num, char*& buff, char& sign, bool& isInputIncorrect )
{
   char c = getchar();
   if( c == ENTER )
   {
      putc('\n',stdout);
   }
   else if( c != END_PROGRAM )
   {
      putc(c,stdout);
   }

   if( (c >= '0' && c <= '9') || c == ENTER || c == END_PROGRAM || c == '-' || c == ' ')
   {
      if(c == END_PROGRAM)
      {
         sign = END_PROGRAM;
         return true;
      }
      else
      {
         if( c != ' ' && c != ENTER )
         {
            num.setListCharacter(c);
         }
         else
         {
            bool ret = num.setNumber();
            if(ret)
            {
               countLine += num.getNumber() * num.getNumber();
               num.reset();
            }
            else
            {
               num.reset();
               countLine = 0;
               isInputIncorrect = true;
               return true;
            }

            if( c == ENTER )
            {
               char buf[10];
               sprintf(buf,"%d\n",countLine);
               appenData(buff,buf);
               countLine = 0;
            }
         }

         readCharacter(num, buff, sign, isInputIncorrect);
      }
   }
   else
   {
      num.reset();
      countLine = 0;
      isInputIncorrect = true;
      return true;
   }
}
开发者ID:homeqc,项目名称:longtm,代码行数:69,代码来源:CalculateSquares.cpp

示例12: compare

    int Number::compare(const Number& num) const
    {
#ifndef PMP_DISABLE_VECTOR
        if (is_v() || num.is_v())
        {
            bool comparisons[3];
            compare(num, comparisons);
            if (comparisons[0])      return -1;
            else if (comparisons[1]) return 0;
            else if (comparisons[2]) return 1;
            else return -2;
        }
#endif

        floating_type f;
        switch (type())
        {
        case Number::INTEGER:
            switch (num.type())
            {
            case Number::INTEGER:
                return get_i().compare(num.get_i());

            case Number::FLOATING:
                f = to_f();
                return f.compare(num.get_f());

            case Number::RATIONAL:
                f = to_f();
                return f.compare(num.to_f());

            default:
                assert(0);
                return 0;
            }

        case Number::FLOATING:
            switch (num.type())
            {
            case Number::INTEGER:
                f = num.to_f();
                return get_f().compare(f);

            case Number::FLOATING:
                return get_f().compare(num.get_f());

            case Number::RATIONAL:
                return get_f().compare(num.to_f());

            default:
                assert(0);
                return 0;
            }

        case Number::RATIONAL:
            switch (num.type())
            {
            case Number::INTEGER:
                f = num.to_f();
                return to_f().compare(f);

            case Number::FLOATING:
                return to_f().compare(num.get_f());

            case Number::RATIONAL:
                return to_f().compare(num.to_f());

            default:
                assert(0);
                return 0;
            }

        default:
            assert(0);
            return 0;
        }
    }
开发者ID:wyrover,项目名称:Calc-H,代码行数:77,代码来源:PmpNumber.cpp

示例13: return

    bool operator == (const Number& x) {
		    return (mod == x.GetMod() && num == x.GetNum());
	}
开发者ID:Artem097,项目名称:RepoRepo,代码行数:3,代码来源:precycle.cpp

示例14: libmesh_error_msg

void ExodusII_IO::write_element_data (const EquationSystems & es)
{
  // Be sure the file has been opened for writing!
  if (MeshOutput<MeshBase>::mesh().processor_id() == 0 && !exio_helper->opened_for_writing)
    libmesh_error_msg("ERROR, ExodusII file must be initialized before outputting element variables.");

  // This function currently only works on SerialMeshes. We rely on
  // having a reference to a non-const MeshBase object from our
  // MeshInput parent class to construct a MeshSerializer object,
  // similar to what is done in ExodusII_IO::write().  Note that
  // calling ExodusII_IO::write_timestep() followed by
  // ExodusII_IO::write_element_data() when the underlying Mesh is a
  // ParallelMesh will result in an unnecessary additional
  // serialization/re-parallelization step.
  MeshSerializer serialize(MeshInput<MeshBase>::mesh(), !MeshOutput<MeshBase>::_is_parallel_format);

  // To be (possibly) filled with a filtered list of variable names to output.
  std::vector<std::string> names;

  // If _output_variables is populated, only output the monomials which are
  // also in the _output_variables vector.
  if (_output_variables.size() > 0)
    {
      std::vector<std::string> monomials;
      const FEType type(CONSTANT, MONOMIAL);

      // Create a list of monomial variable names
      es.build_variable_names(monomials, &type);

      // Filter that list against the _output_variables list.  Note: if names is still empty after
      // all this filtering, all the monomial variables will be gathered
      std::vector<std::string>::iterator it = monomials.begin();
      for (; it!=monomials.end(); ++it)
        if (std::find(_output_variables.begin(), _output_variables.end(), *it) != _output_variables.end())
          names.push_back(*it);
    }

  // If we pass in a list of names to "get_solution" it'll filter the variables coming back
  std::vector<Number> soln;
  es.get_solution(soln, names);

  if(soln.empty()) // If there is nothing to write just return
    return;

  // The data must ultimately be written block by block.  This means that this data
  // must be sorted appropriately.
  if(MeshOutput<MeshBase>::mesh().processor_id())
    return;

  const MeshBase & mesh = MeshOutput<MeshBase>::mesh();

#ifdef LIBMESH_USE_COMPLEX_NUMBERS

  std::vector<std::string> complex_names = exio_helper->get_complex_names(names);

  exio_helper->initialize_element_variables(complex_names);

  unsigned int num_values = soln.size();
  unsigned int num_vars = names.size();
  unsigned int num_elems = num_values / num_vars;

  // This will contain the real and imaginary parts and the magnitude
  // of the values in soln
  std::vector<Real> complex_soln(3*num_values);

  for (unsigned i=0; i<num_vars; ++i)
    {

      for (unsigned int j=0; j<num_elems; ++j)
        {
          Number value = soln[i*num_vars + j];
          complex_soln[3*i*num_elems + j] = value.real();
        }
      for (unsigned int j=0; j<num_elems; ++j)
        {
          Number value = soln[i*num_vars + j];
          complex_soln[3*i*num_elems + num_elems +j] = value.imag();
        }
      for (unsigned int j=0; j<num_elems; ++j)
        {
          Number value = soln[i*num_vars + j];
          complex_soln[3*i*num_elems + 2*num_elems + j] = std::abs(value);
        }
    }

  exio_helper->write_element_values(mesh, complex_soln, _timestep);

#else
  exio_helper->initialize_element_variables(names);
  exio_helper->write_element_values(mesh, soln, _timestep);
#endif
}
开发者ID:YSB330,项目名称:libmesh,代码行数:92,代码来源:exodusII_io.C

示例15: exponent

Number exponent(Number a,Number b){
	int dec = 1000000000;
	long long gcd;
	Number result;
	long double temp=0;
	a.nomerator = (a.decimalSystem < 0)? 1 : a.nomerator;
	a.denomerator = (a.decimalSystem < 0)? 1 : a.denomerator;
	b.nomerator = (b.decimalSystem < 0)? 1 : b.nomerator;
	b.denomerator = (b.decimalSystem < 0)? 1 : b.denomerator;
	if (a.nomerator == 1 && a.nomerator == 1)
	{
		return a;
	}
	if (a.ifINF && b.nomerator == 0)
	{
		result.setValue(1,1);
		result.ifInited = true;
		return result;
	}
	if (a.ifINF || b.ifINF)
	{
		result.setValue(INT64_MAX,1);
		result.ifINF = true;
		return result;
	}

	temp = static_cast<double>(a.nomerator)/a.denomerator;
	if (temp<0){
		Number res(0,1);
		res.decimalSystem = -1;
		return res;
	}
	if ((static_cast<double>(b.nomerator)/b.denomerator)<0)// Check when exponent is less then zero
		temp = pow(temp,static_cast<double>(b.denomerator)/b.nomerator*(-1));
	else 
		temp = pow(temp,static_cast<double>(b.nomerator)/b.denomerator);
	if (temp >= INT64_MAX){
		result.nomerator=INT64_MAX;
		result.updateDecimalSystem(a,b);
		result.ifINF = true;
		return result;
	}
	if (abs(ceil(temp)-temp)<0.00000001){
		result.nomerator = static_cast< long long>(ceil(temp));
		result.updateDecimalSystem(a,b);
		return result;
	}
	if (abs(floor(temp)-temp)<0.00000001){
		result.nomerator = static_cast< long long>(floor(temp));
		result.updateDecimalSystem(a,b);
		return result;
	}
	long long nextDenomerator = 1;
	while(temp<dec){
		temp*=10;
		nextDenomerator*=10;
	}
	if(static_cast< long long>(temp*10)%10<5)
		result.nomerator=static_cast< long long>(floor(temp));
	else
		result.nomerator=static_cast< long long>(ceil(temp));
	result.denomerator = nextDenomerator;
	gcd = GCD(result.nomerator,result.denomerator);
	if (gcd!=0){
		result.nomerator/=gcd;
		result.denomerator/=gcd;
	}
	result.updateDecimalSystem(a,b);
	return result;
}
开发者ID:ozeron,项目名称:Calculator,代码行数:70,代码来源:Data.cpp


注:本文中的Number类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。