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


C++ Rational类代码示例

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


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

示例1: pow

Rational operator^(Rational num1, int power)
{
    int c = pow(num1.getB(), power);
    int d = pow(num1.getA(), power);
    return Rational(d, c);
}
开发者ID:keirsan,项目名称:TeamFirstFpmiLaba,代码行数:6,代码来源:Rational.cpp

示例2: SCAST_RATIONAL

Boolean* Rational::JudgeLessThan(Number* obj){
    Rational* toCheck = SCAST_RATIONAL(this->sub(obj->toExact()));
    return new Boolean(toCheck->sgn() < 0 );
}
开发者ID:yuchenlin,项目名称:SchemeCalc,代码行数:4,代码来源:rational.cpp

示例3: bvisit

 void bvisit(const Rational &x)
 {
     *numer_ = x.get_num();
     *denom_ = x.get_den();
 }
开发者ID:isuruf,项目名称:symengine,代码行数:5,代码来源:numer_denom.cpp

示例4: add

// globale Funktionen
const Rational add(const Rational& a, const Rational& b) {
   Rational r = a;
   r.add(b);
   return r;
}
开发者ID:AnarNFT,项目名称:books-code,代码行数:6,代码来源:rational.cpp

示例5: SCENARIO

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN

#include <Rational.h>
#include <doctest.h>

#include <limits>

SCENARIO( "convert a rational to a double") {
  //      CHECK (275 == doctest::Approx(actual));
  GIVEN ("a valid rational number") {
    WHEN("the Rational is 0") {
      Rational<int> r {0};
      THEN( "the value should convert to 0" ) {
        REQUIRE (r.to_double() == 0);
        //REQUIRE (double(r) == 0);
      }
    }
    WHEN("the rational is 1/2") {
      Rational<int> r {1,2};
      THEN( "the value should convert to 0.5" ) {
        REQUIRE (r.to_double() == doctest::Approx(0.5));
        //REQUIRE (double(r) == doctest::Approx(0.5));
      }
    }
    WHEN("the rational is {-22,7}") {
      Rational<int> r {-22,7};
      THEN( "the value should convert to -pi" ) {
        REQUIRE (r.to_double() == doctest::Approx(-3.14).epsilon(0.01));
        //REQUIRE (double(r) == doctest::Approx(-3.14).epsilon(0.01));
      }
    }
开发者ID:DaveParillo,项目名称:cisc187,代码行数:31,代码来源:step5.cpp

示例6: prrat

// print rational r followed by a newline
void prrat(Rational r) {
  cout << r.n() << "/" << r.d() << endl;
}
开发者ID:dray92,项目名称:Programming-Questions,代码行数:4,代码来源:calc.cpp

示例7: div

const Rational div(const Rational& z, const Rational& n) {
   Rational r = z;
   r.div(n);
   return r;
}
开发者ID:AnarNFT,项目名称:books-code,代码行数:5,代码来源:rational.cpp

示例8: setEditRate

void WaveAudioEssenceReader::setEditRate(const Rational& rate)
{
    _editRate = rate;

    //WaveAudioEssenceDescriptor* waed =
    //    dynamic_cast<WaveAudioEssenceDescriptor*>(_descriptor);
    //if (waed == 0)
    //{
    //    /// \todo report error
    //    return;
    //}

    //waed->setSampleRate(rate);

    // A call to set the edit rate will also set the container duration.

    // Calculate frame and buffer sizes
    _baseSampleCount = 0; // reset so we can detect error
    if (rate == Rational(30000, 1001))
    {
        if (_fmtChunk->nSamplesPerSec == 48000)
        {
            _baseSampleCount = 1600;
            _additionalSampleCounts = 5;
            _additionalSampleCount[0] = 2;
            _additionalSampleCount[1] = 1;
            _additionalSampleCount[2] = 2;
            _additionalSampleCount[3] = 1;
            _additionalSampleCount[4] = 2;
        }
        /// \todo warn if audio data isn't 48kHz
    }
    else
    {
        _baseSampleCount = (UInt32)(
            _fmtChunk->nSamplesPerSec *
            (UInt64) rate.getDenominator() /
            (UInt64) rate.getNumerator());
        _additionalSampleCounts = 0;
    }

    _combinedDataLength = 0;
    if (_additionalSampleCounts > 0)
    {
        // Calculate the average buffer size
        for (UInt32 i = 0; i < _additionalSampleCounts; ++i)
        {
            _combinedDataLength +=
                (_baseSampleCount + _additionalSampleCount[i]) *
                (_fmtChunk->wBitsPerSample / 8) *
                _fmtChunk->nChannels;
        }
    }
    else
    {
        _combinedDataLength =
            _baseSampleCount *
            (_fmtChunk->wBitsPerSample / 8) *
            _fmtChunk->nChannels;
    }

    if (_baseSampleCount == 0 || _combinedDataLength == 0)
    {
        // We've failed to determine the size of an edit unit
        error(ESS_ERROR_FailedToDetermineFrameSize);
        return;
    }

    // Update the duration for the essence descriptor
    if (_additionalSampleCounts > 0)
    {
        Length duration = ((_dataLength * _additionalSampleCounts) / _combinedDataLength);

        // we have to do the rounding manually as ceil() takes a double
        // and VC6 can't handle unsigned int64 to double...
        Length rem = (_dataLength * _additionalSampleCounts) % _combinedDataLength;
        if (rem != 0)
            duration++; // round up
        
        //waed->setContainerDuration(duration);
		_containerDuration = duration;
    }
    else
    {
        Length duration = _dataLength / _combinedDataLength;

        // we have to do the rounding manually as ceil() takes a double
        // and VC6 can't handle unsigned int64 to double...
        Length rem = _dataLength % _combinedDataLength;
        if (rem != 0)
            duration++; // round up 

        //waed->setContainerDuration(duration);
		_containerDuration = duration;
    }
}
开发者ID:dcsch,项目名称:asiotest,代码行数:96,代码来源:WaveAudioEssenceReader.cpp

示例9: switch

bool IDLAssertion::parse(TNode node, int c, bool negated) {

  // Only unit coefficients allowed
  if (c != 1 && c != -1) {
    return false;
  }

  // Assume we're ok
  bool ok = true;

  // The kind of the node
  switch(node.getKind()) {

  case kind::NOT:
    // We parse the negation
    ok = parse(node[0], c, true);
    // Setup the kind
    if (ok) {
      d_op = negateOp(d_op);
    }
    break;

  case kind::EQUAL:
  case kind::LT:
  case kind::LEQ:
  case kind::GT:
  case kind::GEQ: {
    // All relation operators are parsed on both sides
    d_op = node.getKind();
    ok = parse(node[0], c, negated);
    if (ok) {
      ok = parse(node[1],-c, negated);
    }
    break;
  }

  case kind::CONST_RATIONAL: {
    // Constants
    Rational m = node.getConst<Rational>();
    if (m.isIntegral()) {
      d_c +=  m.getNumerator() * (-c);
    } else {
      ok = false;
    }
    break;
  }
  case kind::MULT: {
    // Only unit multiplication of variables
    if (node.getNumChildren() == 2 && node[0].isConst()) {
      Rational a = node[0].getConst<Rational>();
      if (a == 1 || a == -1) {
        ok = parse(node[1], c * a.sgn(), negated);
      } else {
        ok = false;
      }
    } else {
      ok = false;
    }
    break;
  }

  case kind::PLUS: {
    for(unsigned i = 0; i < node.getNumChildren(); ++i) {
      ok = parse(node[i], c, negated);
      if(!ok) {
        break;
      }
    }
    break;
  }

  case kind::MINUS: {
    ok = parse(node[0], c, negated);
    if (ok) {
      ok = parse(node[1], -c, negated);
    }
    break;
  }

  case kind::UMINUS: {
    ok = parse(node[0], -c, negated);
    break;
  }

  default: {
    if (c > 0) {
      if (d_x.isNull()) {
        d_x = node;
      } else {
        ok = false;
      }
    } else {
      if (d_y.isNull()) {
        d_y = node;
      } else {
        ok = false;
      }
    }
    break;
  }
//.........这里部分代码省略.........
开发者ID:g2graman,项目名称:CVC4,代码行数:101,代码来源:idl_assertion.cpp

示例10: abs

Rational abs(const Rational& r)
{
  return r.abs();
}
开发者ID:FrancisRussell,项目名称:excafe,代码行数:4,代码来源:rational.cpp

示例11: pow

Rational pow(const Rational& r, const long exponent)
{
  return r.pow(exponent);
}
开发者ID:FrancisRussell,项目名称:excafe,代码行数:4,代码来源:rational.cpp

示例12: hash_value

std::size_t hash_value(const Rational& r)
{
  return r.hash();
}
开发者ID:FrancisRussell,项目名称:excafe,代码行数:4,代码来源:rational.cpp

示例13: subtract

// Pre:
// 	1) a >= b
// Constructively returns a-b
Rational subtract(Rational a, Rational b) {
	Rational c ("0");
	int i = 0;
	while(i < a.getLength()) {
		if(a.getDigit(i) >= b.getDigit(i))
			c.setDigit(i,a.getDigit(i)-b.getDigit(i));
		else {
			c.setDigit(i,a.getDigit(i)+10-b.getDigit(i));
			a.setDigit(i+1,a.getDigit(i+1)-1);
		}	
		i++;
	}	
	while(i > 1 && c.getDigit(i-1) == 0)
		i--;
	c.setLength(i);
	return c;
}
开发者ID:flippedAben,项目名称:comprog,代码行数:20,代码来源:julka.cpp

示例14: add

// Constructively returns a+b
Rational add(Rational a, Rational b) {
	int max = ((a.getLength() > b.getLength()) ? a.getLength() : b.getLength());
	int i = 0;
	Rational c ("0");
	c.setLength(max);
	while(i < max) {
		c.setDigit(i, c.getDigit(i) + a.getDigit(i) + b.getDigit(i));
		if(c.getDigit(i) > 9) {
			c.setDigit(i, c.getDigit(i)%10);
			c.setDigit(i+1, c.getDigit(i+1)+1);
			if(i == max-1)
				c.setLength(c.getLength()+1);
		}
		i++;
	}
	return c;
}
开发者ID:flippedAben,项目名称:comprog,代码行数:18,代码来源:julka.cpp

示例15: Rational

Rational Rational::add( const Rational & other ) const{
	return Rational((getNumerator() * other.getDenominator() + other.getNumerator() * getDenominator()), (getDenominator() * other.getDenominator()));
}
开发者ID:kamranmoghbel,项目名称:cs540,代码行数:3,代码来源:h2.Rational.cpp


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