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


C++ Rational::numerator方法代码示例

本文整理汇总了C++中Rational::numerator方法的典型用法代码示例。如果您正苦于以下问题:C++ Rational::numerator方法的具体用法?C++ Rational::numerator怎么用?C++ Rational::numerator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Rational的用法示例。


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

示例1: DivisionByZeroException

Token
TokenEvalContext::evalDivide(const Token& lexpr, const Token& rexpr) const
{
  Token left = evalToken(lexpr);
  Token right = evalToken(rexpr);

  if (left.isInt()) {
    int value = left.intValue();
    if (value == 0)
      throw DivisionByZeroException();

    if (right.isInt())
      return Token::newInt(left.srcpos(),
                           left.bitwidth(), value / right.intValue());
    else if (right.isFloat())
      return Token(left.srcpos(),
                   kFloat, double(value) / right.floatValue());
    else if (right.isRational())
      return Token(left.srcpos(),
                   kRational, Rational(value, 1) / right.rationalValue());
    else
      throw BadExpressionException(fromInt(__LINE__));
  }
  else if (left.isFloat()) {
    double value = left.floatValue();
    if (value == 0)
      throw DivisionByZeroException();

    if (right.isInt())
      return Token(left.srcpos(),
                   kFloat, value / double(right.intValue()));
    else if (right.isFloat())
      return Token(left.srcpos(),
                   kFloat, value / right.floatValue());
    else if (right.isRational())
      return Token(left.srcpos(),
                   kFloat, value / right.rationalValue().toFloat());
    else
      throw BadExpressionException(fromInt(__LINE__));
  }
  else if (left.isRational()) {
    Rational value = left.rationalValue();
    if (value.numerator() == 0)
      throw DivisionByZeroException();

    if (right.isInt())
      return Token(left.srcpos(),
                   kRational, value / Rational(right.intValue(), 1));
    else if (right.isFloat())
      return Token(left.srcpos(),
                   kFloat, value.toFloat() / right.floatValue());
    else if (right.isRational())
      return Token(left.srcpos(),
                   kRational, value / right.rationalValue());
    else
      throw BadExpressionException(fromInt(__LINE__));
  }

  throw BadExpressionException(fromInt(__LINE__));
}
开发者ID:hvellyr,项目名称:herschel,代码行数:60,代码来源:tokeneval.cpp

示例2: assert

Expression * Trigonometry::shallowReduceDirectFunction(Expression * e, Context& context, Expression::AngleUnit angleUnit) {
  assert(e->type() == Expression::Type::Sine || e->type() == Expression::Type::Cosine || e->type() == Expression::Type::Tangent);
  Expression * lookup = Trigonometry::table(e->operand(0), e->type(), context, angleUnit);
  if (lookup != nullptr) {
    return e->replaceWith(lookup, true);
  }
  Expression::Type correspondingType = e->type() == Expression::Type::Cosine ? Expression::Type::ArcCosine : (e->type() == Expression::Type::Sine ? Expression::Type::ArcSine : Expression::Type::ArcTangent);
  if (e->operand(0)->type() == correspondingType) {
    float trigoOp = e->operand(0)->operand(0)->approximateToScalar<float>(context, angleUnit);
    if (e->type() == Expression::Type::Tangent || (trigoOp >= -1.0f && trigoOp <= 1.0f)) {
      return e->replaceWith(e->editableOperand(0)->editableOperand(0), true);
    }
  }
  if (e->operand(0)->sign() == Expression::Sign::Negative) {
    Expression * op = e->editableOperand(0);
    Expression * newOp = op->setSign(Expression::Sign::Positive, context, angleUnit);
    newOp->shallowReduce(context, angleUnit);
    if (e->type() == Expression::Type::Cosine) {
      return e->shallowReduce(context, angleUnit);
    } else {
      Multiplication * m = new Multiplication(new Rational(-1), e->clone(), false);
      m->editableOperand(1)->shallowReduce(context, angleUnit);
      return e->replaceWith(m, true)->shallowReduce(context, angleUnit);
    }
  }
  if ((angleUnit == Expression::AngleUnit::Radian && e->operand(0)->type() == Expression::Type::Multiplication && e->operand(0)->numberOfOperands() == 2 && e->operand(0)->operand(1)->type() == Expression::Type::Symbol && static_cast<const Symbol *>(e->operand(0)->operand(1))->name() == Ion::Charset::SmallPi && e->operand(0)->operand(0)->type() == Expression::Type::Rational) || (angleUnit == Expression::AngleUnit::Degree && e->operand(0)->type() == Expression::Type::Rational)) {
    Rational * r = angleUnit == Expression::AngleUnit::Radian ? static_cast<Rational *>(e->editableOperand(0)->editableOperand(0)) : static_cast<Rational *>(e->editableOperand(0));
    int unaryCoefficient = 1; // store 1 or -1
    // Replace argument in [0, Pi/2[ or [0, 90[
    Integer divisor = angleUnit == Expression::AngleUnit::Radian ? r->denominator() : Integer::Multiplication(r->denominator(), Integer(90));
    Integer dividand = angleUnit == Expression::AngleUnit::Radian ? Integer::Addition(r->numerator(), r->numerator()) : r->numerator();
    if (divisor.isLowerThan(dividand)) {
      Integer piDivisor = angleUnit == Expression::AngleUnit::Radian ? r->denominator() : Integer::Multiplication(r->denominator(), Integer(180));
      IntegerDivision div = Integer::Division(r->numerator(), piDivisor);
      dividand = angleUnit == Expression::AngleUnit::Radian ? Integer::Addition(div.remainder, div.remainder) : div.remainder;
      if (divisor.isLowerThan(dividand)) {
        div.remainder = Integer::Subtraction(piDivisor, div.remainder);
        if (e->type() == Expression::Type::Cosine || e->type() == Expression::Type::Tangent) {
          unaryCoefficient *= -1;
        }
      }
      Rational * newR = new Rational(div.remainder, r->denominator());
      Expression * rationalParent = angleUnit == Expression::AngleUnit::Radian ? e->editableOperand(0) : e;
      rationalParent->replaceOperand(r, newR, true);
      e->editableOperand(0)->shallowReduce(context, angleUnit);
      if (Integer::Division(div.quotient, Integer(2)).remainder.isOne() && e->type() != Expression::Type::Tangent) {
        unaryCoefficient *= -1;
      }
      Expression * simplifiedCosine = e->shallowReduce(context, angleUnit); // recursive
      Multiplication * m = new Multiplication(new Rational(unaryCoefficient), simplifiedCosine->clone(), false);
      return simplifiedCosine->replaceWith(m, true)->shallowReduce(context, angleUnit);
    }
    assert(r->sign() == Expression::Sign::Positive);
    assert(!divisor.isLowerThan(dividand));
  }
  return e;
}
开发者ID:Tilka,项目名称:epsilon,代码行数:57,代码来源:trigonometry.cpp

示例3: Power

Rational Rational::Power(const Rational & i, const Integer & j) {
  Integer absJ = j;
  absJ.setNegative(false);
  Integer newNumerator = Integer::Power(i.numerator(), absJ);
  Integer newDenominator = Integer::Power(i.denominator(), absJ);
  if (j.isNegative()) {
    return Rational(newDenominator, newNumerator);
  }
  return Rational(newNumerator, newDenominator);
}
开发者ID:Tilka,项目名称:epsilon,代码行数:10,代码来源:rational.cpp

示例4: return

double
INTERN_MP_FLOAT::to_double(const Root_of_2<MP_Float> &x)
{
  typedef MP_Float RT;
  typedef Quotient<RT> FT;
  typedef CGAL::Rational_traits< FT > Rational;
  Rational r;
  const RT r1 = r.numerator(x.alpha());
  const RT d1 = r.denominator(x.alpha());

  if(x.is_rational()) {
    std::pair<double, int> n = to_double_exp(r1);
    std::pair<double, int> d = to_double_exp(d1);
    double scale = std::ldexp(1.0, n.second - d.second);
    return (n.first / d.first) * scale;
  }

  const RT r2 = r.numerator(x.beta());
  const RT d2 = r.denominator(x.beta());
  const RT r3 = r.numerator(x.gamma());
  const RT d3 = r.denominator(x.gamma());

  std::pair<double, int> n1 = to_double_exp(r1);
  std::pair<double, int> v1 = to_double_exp(d1);
  double scale1 = std::ldexp(1.0, n1.second - v1.second);

  std::pair<double, int> n2 = to_double_exp(r2);
  std::pair<double, int> v2 = to_double_exp(d2);
  double scale2 = std::ldexp(1.0, n2.second - v2.second);

  std::pair<double, int> n3 = to_double_exp(r3);
  std::pair<double, int> v3 = to_double_exp(d3);
  double scale3 = std::ldexp(1.0, n3.second - v3.second);

  return ((n1.first / v1.first) * scale1) + 
         ((n2.first / v2.first) * scale2) *
         std::sqrt((n3.first / v3.first) * scale3);
}
开发者ID:mvdan,项目名称:geoc-viewer,代码行数:38,代码来源:MP_Float.cpp

示例5: getSuperInstance

SuperInstance*  Merger::getSuperInstance(Instance* src, Instance* dst, list<Connection*>* connections ){
    // Superinstance name
    stringstream id;
    id << "merger";
    id << index++;

    //Get property of instances
    Actor* srcAct = src->getActor();
    MoC* srcMoC = srcAct->getMoC();
    Actor* dstAct = dst->getActor();
    MoC* dstMoC = dstAct->getMoC();
    Pattern* srcPattern = ((CSDFMoC*)srcMoC)->getOutputPattern();
    Pattern* dstPattern = ((CSDFMoC*)dstMoC)->getInputPattern();

    map<Port*, Port*>* internPorts = new map<Port*, Port*>();

    // Calculate rate and set internal ports
    Rational rate;

    list<Connection*>::iterator it;
    for (it = connections->begin(); it != connections->end(); it++){
        Connection* connection = *it;

        // Get ports of the connection
        Port* srcPort = connection->getSourcePort();
        Port* dstPort = connection->getDestinationPort();

        // Get corresponding port in actor
        Port* srcActPort = srcAct->getOutput(srcPort->getName());
        Port* dstActPort = dstAct->getInput(dstPort->getName());

        // Verify that rate of the two instances are consistent
        Rational compareRate = getRational(srcPattern->getNumTokens(srcActPort), dstPattern->getNumTokens(dstActPort));
        if ( rate == 0){
            rate = compareRate;
        }else if (rate != compareRate){
            // This two instances can't be merged
            return NULL;
        }

        // Set internal ports of each instances
        srcPort->setInternal(true);
        dstPort->setInternal(true);
        internPorts->insert(pair<Port*, Port*>(srcPort, dstPort));
    }


    return new SuperInstance(Context, id.str() , src, rate.numerator(), dst, rate.denominator(), internPorts);
}
开发者ID:orcc,项目名称:jade,代码行数:49,代码来源:Merger.cpp

示例6: Token

Token
TokenEvalContext::evalExponent(const Token& lexpr, const Token& rexpr) const
{
  Token left = evalToken(lexpr);
  Token right = evalToken(rexpr);

  if (left.isInt()) {
    int value = left.intValue();

    if (right.isInt())
      return Token(left.srcpos(),
                   kInt, herschel::exponent(value, right.intValue()));
    else
      throw BadExpressionException(fromInt(__LINE__));
  }
  else if (left.isFloat()) {
    double value = left.floatValue();
    if (value == 0)
      throw DivisionByZeroException();

    if (right.isInt())
      return Token(left.srcpos(),
                   kFloat, herschel::exponent(value, right.intValue()));
    else
      throw BadExpressionException(fromInt(__LINE__));
  }
  else if (left.isRational()) {
    Rational value = left.rationalValue();
    if (value.numerator() == 0)
      throw DivisionByZeroException();

    if (right.isInt())
      return Token(left.srcpos(),
                   kRational, value.exponent(right.intValue()));
    else
      throw BadExpressionException(fromInt(__LINE__));
  }

  throw BadExpressionException(fromInt(__LINE__));
}
开发者ID:hvellyr,项目名称:herschel,代码行数:40,代码来源:tokeneval.cpp

示例7: main

int main( int argc, char** argv )
{
  // Command line options
  bool help_mode_enabled{ false };
  scalar end_time_override{ -1.0 };
  unsigned output_frequency{ 0 };
  std::string serialized_file_name;

  // Attempt to load command line options
  if( !parseCommandLineOptions( &argc, &argv, help_mode_enabled, end_time_override, output_frequency, serialized_file_name ) )
  {
    return EXIT_FAILURE;
  }

  // If the user requested help, print help and exit
  if( help_mode_enabled )
  {
    printUsage( argv[0] );
    return EXIT_SUCCESS;
  }

  // Check for impossible combinations of options
  #ifdef USE_HDF5
  if( g_output_forces && g_output_dir_name.empty() )
  {
    std::cerr << "Impulse output requires an output directory." << std::endl;
    return EXIT_FAILURE;
  }
  #endif

  #ifdef USE_PYTHON
  // Initialize the Python interpreter
  Py_SetProgramName( argv[0] );
  Py_Initialize();

  // Initialize a callback that will close down the interpreter
  atexit( exitCleanup );

  // Allow subsequent Python commands to use the sys module
  PythonTools::pythonCommand( "import sys" );

  // Prevent Python from intercepting the interrupt signal
  PythonTools::pythonCommand( "import signal" );
  PythonTools::pythonCommand( "signal.signal( signal.SIGINT, signal.SIG_DFL )" );

  // Initialize the callbacks
  PythonScripting::initializeCallbacks();
  #endif

  if( !serialized_file_name.empty() )
  {
    if( deserializeSystem( serialized_file_name ) == EXIT_FAILURE )
    {
      return EXIT_FAILURE;
    }
    return executeSimLoop();
  }

  // The user must provide the path to an xml scene file
  if( argc != optind + 1 )
  {
    std::cerr << "Invalid arguments. Must provide a single xml scene file name." << std::endl;
    return EXIT_FAILURE;
  }

  // Attempt to load the user-provided scene
  if( !loadXMLScene( std::string{ argv[optind] } ) )
  {
    return EXIT_FAILURE;
  }

  // Override the default end time with the requested one, if provided
  if( end_time_override > 0.0 )
  {
    g_end_time = end_time_override;
  }

  // Compute the data output rate
  assert( g_dt.positive() );
  // If the user provided an output frequency
  if( output_frequency != 0 )
  {
    const Rational<std::intmax_t> potential_steps_per_frame{ std::intmax_t( 1 ) / ( g_dt * std::intmax_t( output_frequency ) ) };
    if( !potential_steps_per_frame.isInteger() )
    {
      std::cerr << "Timestep and output frequency do not yield an integer number of timesteps for data output. Exiting." << std::endl;
      return EXIT_FAILURE;
    }
    g_steps_per_save = unsigned( potential_steps_per_frame.numerator() );
  }
  // Otherwise default to dumping every frame
  else
  {
    g_steps_per_save = 1;
  }
  assert( g_end_time > 0.0 );
  g_save_number_width = MathUtilities::computeNumDigits( 1 + unsigned( ceil( g_end_time / scalar( g_dt ) ) ) / g_steps_per_save );

  printCompileInfo( std::cout );
  std::cout << "Geometry count: " << g_sim.state().ngeo() << std::endl;
//.........这里部分代码省略.........
开发者ID:alecjacobson,项目名称:scisim,代码行数:101,代码来源:rigidbody3d_cli.cpp

示例8: Rational

Rational operator / ( const Rational & lhs, const Rational & rhs ) {
	return Rational(lhs.numerator() * rhs.denominator(), lhs.denominator() * rhs.numerator());
}
开发者ID:MeeSeongIm,项目名称:c_cpp_samples,代码行数:3,代码来源:overload_op_nonmem_fns_ratl.cpp

示例9:

const Rational<T> operator*(const Rational<T>& lhs, 
							const Rational<T>& rhs)
{
	return Rational<T>(lhs.numerator() * rhs.numerator(), lhs.denominator() * lhs.denominator());
}
开发者ID:huganle,项目名称:code,代码行数:5,代码来源:friendFunction.cpp

示例10: doMultiply

const Rational<T> doMultiply (const Rational<T>& lhs,
		const Rational<T>& rhs) {
	return Rational<T>(lhs.numerator()*rhs.numerator(),
			lhs.denominator()*rhs.denominator());
}
开发者ID:Caroline-Wendy,项目名称:Effective-Cpp,代码行数:5,代码来源:effectivecpp_46.cpp

示例11: NaturalOrder

int Rational::NaturalOrder(const Rational & i, const Rational & j) {
  Integer i1 = Integer::Multiplication(i.numerator(), j.denominator());
  Integer i2 = Integer::Multiplication(i.denominator(), j.numerator());
  return Integer::NaturalOrder(i1, i2);
}
开发者ID:Tilka,项目名称:epsilon,代码行数:5,代码来源:rational.cpp

示例12:

bool Rational::operator==(const Rational& r) const
{
    // todo: 1/2 == 2/4
    return n==r.numerator() && d==r.denominator();
}
开发者ID:tomi77,项目名称:libmathng,代码行数:5,代码来源:rational.cpp

示例13: Multiplication

Rational Rational::Multiplication(const Rational & i, const Rational & j) {
  Integer newNumerator = Integer::Multiplication(i.numerator(), j.numerator());
  Integer newDenominator = Integer::Multiplication(i.denominator(), j.denominator());
  return Rational(newNumerator, newDenominator);
}
开发者ID:Tilka,项目名称:epsilon,代码行数:5,代码来源:rational.cpp

示例14: main

int main()
{
	using namespace std;
	using numeric::Rational;

	Rational a { 1, 3 };
	Rational b { 3, 2 };

	cout << "Rational Number Class - Test Program\n"
			"------------------------------------\n"
		 << endl;
	
	Rational c = a + b;

	cout << a.numerator() << '/' << a.denominator() << " + "
		 << b.numerator() << '/' << b.denominator() << " = "
		 << c.numerator() << '/' << c.denominator() << endl;

	Rational d = a * c;

	cout << a.numerator() << '/' << a.denominator() << " * "
		 << c.numerator() << '/' << c.denominator() << " = "
		 << d.numerator() << '/' << d.denominator() << endl;

	Rational e = d - b;

	cout << d.numerator() << '/' << d.denominator() << " - "
		 << b.numerator() << '/' << b.denominator() << " = "
		 << e.numerator() << '/' << e.denominator() << endl;

	Rational f = e / a;

	cout << e.numerator() << '/' << e.denominator() << " / "
		 << a.numerator() << '/' << a.denominator() << " = "
		 << f.numerator() << '/' << f.denominator() << endl;

	cout.setf(ios::boolalpha);

	cout << a.numerator() << '/' << a.denominator() << " == "
		 << b.numerator() << '/' << b.denominator() << " ? "
		 << (a == b) << endl;

	cout << c.numerator() << '/' << c.denominator()
		 << " Positive? " << (c > Rational::ZERO) << endl;

	cout << e.numerator() << '/' << e.denominator()
		 << " Negative? " << (e < Rational::ZERO) << endl;

	return 0;
}
开发者ID:dcods22,项目名称:Rational,代码行数:50,代码来源:TestRational2.cpp

示例15: to_double

double to_double(const Rational& a) {
	return double(a.numerator())/double(a.denumerator());
}
开发者ID:thelastpolaris,项目名称:Programming-Principles-and-Practice-Using-C-,代码行数:3,代码来源:ex13.cpp


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