Ejemplo n.º 1
0
Numeric* Integer::operator /(Numeric* c)const{
    Integer* entier = dynamic_cast<Integer *>(c);
    if(entier==NULL)
    {
        Real* reel=dynamic_cast<Real*>(c);
        if(reel==NULL){
            Rational *rationnel=dynamic_cast<Rational *>(c);
            Rational *result=new Rational(Integer(_value*rationnel->getDenominator().getValue()),Integer(rationnel->getNumerator().getValue()));
            result->simplification();
            if(result->getDenominator().getValue()==1 || result->getNumerator().getValue()==0){
                Integer* resE = new Integer(result->getNumerator().getValue());
                delete result;
                return resE;
            }
            return result;
        }
        if(reel->getValue() ==0)
            throw QString("Can't divide by 0 !");
        return new Real(((double)_value)/reel->getValue());
    }
    if(entier->getValue() ==0)
        throw QString("Can't divide by 0 !");
    Rational* res = new Rational(*this,*entier);
    res->simplification();
    if(res->getDenominator().getValue()==1 || res->getNumerator().getValue()==0){
        Integer* resE = new Integer(res->getNumerator().getValue());
        delete res;
        return resE;
    }
    return res;
}
Ejemplo n.º 2
0
bool operator==(Rational const & a, Rational const & b)
{
	if (a.getNumerator() != b.getNumerator()) return false;
	if (b.getDenominator() != b.getDenominator()) return false;

	return true;
}
Ejemplo n.º 3
0
Rational operator+(Rational const & a, Rational const & b)
{
	Rational rat;
	rat.setNumerator((a.getNumerator()*b.getDenominator()) + (b.getNumerator() * a.getDenominator()));
	rat.setDenominator(a.getDenominator() * b.getDenominator());

	return rat;
}
bool  Rational::operator >=(const Rational& secondnum) const{
    BigInt num1 = this->getNumerator();
    BigInt num2 = secondnum.getNumerator();
    BigInt deno1 = this->getDenomniator();
    BigInt deno2 = secondnum.getDenomniator();

    normalize(num1, deno1);
    normalize(num2, deno2);

    if(num1 < 0 && deno1 < 0){
        num1 = -num1;
        deno1 = -deno1;
    }
    else if(num1 > 0 && deno1 < 0){
        num1 = -num1;
        deno1 = -deno1;
    }
    if(num2 < 0 && deno2 < 0){
        num2 = -num2;
        deno2 = -deno2;
    }
    else if(num2 > 0 && deno2 < 0){
        num2 = -num2;
        deno2 = -deno2;
    }

    if(num1 * deno2 >= num2 * deno1){
        return 1;
    }
    else{
        return 0;
    }

}
Ejemplo n.º 5
0
void TheoryArith::printRational(ExprStream& os, const Rational& r,
                                bool printAsReal)
{
  // Print rational
  if (r.isInteger()) {
    if (r < 0) {
      if (os.lang() == SPASS_LANG) {
        os << "-" << (-r).toString();
        if (printAsReal) os << ".0";
      } else {
        os << "(" << push;
        if (os.lang() == SMTLIB_LANG) {
          os << "~";
        }
        else {
          os << "-";
        }
        os << space << (-r).toString();
        if (printAsReal) os << ".0";
        os << push << ")";
      }
    }
    else {
      os << r.toString();
      if (printAsReal) os << ".0";
    }
  }
  else {
    os << "(" << push << "/ ";
    Rational tmp = r.getNumerator();
    if (tmp < 0) {
      if (os.lang() == SPASS_LANG) {
        os << "-" << (-tmp).toString();
        if (printAsReal) os << ".0";
      } else {
        os << "(" << push;
        if (os.lang() == SMTLIB_LANG) {
          os << "~";
        }
        else {
          os << "-";
        }
        os << space << (-tmp).toString();
        if (printAsReal) os << ".0";
        os << push << ")";
      }
    }
    else {
      os << tmp.toString();
      if (printAsReal) os << ".0";
    }
    os << space;
    tmp = r.getDenominator();
    DebugAssert(tmp > 0 && tmp.isInteger(), "Unexpected rational denominator");
    os << tmp.toString();
    if (printAsReal) os << ".0";
    os << push << ")";
  }
}
Ejemplo n.º 6
0
Timestamp::Timestamp(uint s, uint frames, const Rational &newFramerate) {
	assert(newFramerate > 0);

	uint fr;
	if (newFramerate.getDenominator() == 1) {
		fr = newFramerate.getNumerator();
	} else {
		Rational time = newFramerate.getInverse() * frames;
		frames = time.getNumerator();
		fr = time.getDenominator();
	}

	_secs = s + (frames / fr);
	_framerateFactor = 1000 / gcd<uint>(1000, fr);
	_framerate = fr * _framerateFactor;
	_numFrames = (frames % fr) * _framerateFactor;
}
Ejemplo n.º 7
0
Archivo: 13.1.cpp Proyecto: xiammu/acm
Rational Rational::add(Rational &secondRational)
{
  cout<<numerator<<"vjnfiehb"<<endl;
  long n=numerator*secondRational.getDenominator()+denominator*secondRational.getNumerator();
  long d=denominator*secondRational.getDenominator();
  //cout<<"n = "<<n<<' '<<"d = "<<d<<endl;
  return Rational(n, d);
}
const Rational  operator -(const Rational& firstnum, const Rational& secondnum) {
    BigInt denominator = firstnum.getDenomniator() * secondnum.getDenomniator();
    BigInt first_numerator = firstnum.getNumerator() * secondnum.getDenomniator();
    BigInt second_numerator = secondnum.getNumerator() * firstnum.getDenomniator();
    BigInt allnumerator = first_numerator - second_numerator;
    
    normalize(allnumerator, denominator);

    if(denominator < 0 && allnumerator < 0){
        denominator = -denominator;
        allnumerator = -allnumerator;
    }
    else if(denominator < 0 && allnumerator > 0 ){
        denominator = -denominator;
        allnumerator = - allnumerator;
    }

    return Rational(allnumerator, denominator);
}
Ejemplo n.º 9
0
int main()
{
    Rational oneEighth(1, 8);
    Rational oneHalf(1,2);

    Rational result = oneEighth * oneHalf;
    cout << result.getNumerator() << " " << result.getDenominator() << endl;
    result = result * oneEighth;
    cout << result.getNumerator() << " " << result.getDenominator() << endl;

    result = oneHalf * 2;//int转换成了Ratinal对象了
    cout << result.getNumerator() << " " << result.getDenominator() << endl;

    result = 2 * oneHalf;
    cout << result.getNumerator() << " " << result.getDenominator() << endl;


    return 0;
}
const Rational  operator -(const Rational& num) {
    BigInt pos_num = num.getNumerator();
    BigInt deno = num.getDenomniator();
    
    normalize(pos_num, deno);
    
    BigInt neg_num = -pos_num;
    
    return Rational(neg_num, deno);
}
Ejemplo n.º 11
0
 const Rational operator +(const Rational& r1, const Rational& r2){
     //If the parameters are const, then the get() function must be suffix const
     BigInt n1(r1.getNumerator());
     BigInt n2(r2.getNumerator());
     BigInt d1(r1.getDenominator());
     BigInt d2(r2.getDenominator());
     BigInt gcd(GCD(d1, d2));
     gcd.abs();
     if(!n1.isNeg() && !n2.isNeg()){
         BigInt n(n1 * (d2 / gcd) + n2 * (d1 / gcd));
         BigInt d(d1 * d2 / gcd);
         return Rational(n, d);
     }
     else if(n1.isNeg() && n2.isNeg()){
         n1 = -n1;
         d1 = -d1;
         n2 = -n2;
         d2 = -d2;
         BigInt n(n1 * (d2 / gcd) + n2 * (d1 / gcd));
         BigInt d(d1 * d2 / gcd);
         return Rational(n, d);
     }
     else if(!n1.isNeg() && n2.isNeg()){
         n2 = -n2;
         d2 = -d2;
         BigInt n(n1 * (d2 / gcd) + n2 * (d1 / gcd));
         BigInt d(d1 * d2 / gcd);
         return Rational(n, d);
     }
     else if(n1.isNeg() && !n2.isNeg()){
         n1 = -n1;
         d1 = -d1;
         BigInt n(n2 * (d1 / gcd) + n1 * (d2 / gcd));
         BigInt d(d1 * d2 / gcd);
         return Rational(n, d);
     }
 }
Ejemplo n.º 12
0
void NatE::simplify(){
	this->exponent->simplify();
	this->coefficient->simplify();
	if (this->coefficient->getType() == "Rational"){
		Rational* newCo = dynamic_cast<Rational*>(this->coefficient);
		if (newCo->getNumerator()->getType() == "NatE"){
			NatE* newPi = dynamic_cast<NatE*>(newCo->getNumerator());
			Add* add = new Add();
			this->exponent = add->evaluate(this->exponent, newPi->getExponent());
			newCo->setNumerator(newPi->getCoefficient());
			newCo->simplify();
			this->coefficient = newCo;
		}
		else if (newCo->getDenominator()->getType() == "NatE"){
			NatE* newPi = dynamic_cast<NatE*>(newCo->getDenominator());
			Subtract* sub = new Subtract();
			this->exponent = sub->evaluate(this->exponent, newPi->getExponent());
			newCo->setDenominator(newPi->getCoefficient());
			newCo->simplify();
			this->coefficient = newCo;
		}

	}
}
Ejemplo n.º 13
0
//basic operators
bool operator<(Rational const & a, Rational const & b)
{
	return (a.getNumerator() * b.getDenominator()) < (b.getNumerator() * a.getDenominator());
}
Ejemplo n.º 14
0
int main() 
{ 
	Rational p; // p uses the default constructor 
	Rational q(1, 2); // q uses the other constructor 
	Rational b(1); // uses constructor to initialize numerator and defaults the denominator to 1

	cout << b << endl;
	p.setNumerator(1); // set p to be 1/4 
	p.setDenominator(4); 
	p= p + q;
	p= p * q;

	//print out p and q
	p.streamInsert(cout);

	cout << "p is " << p.getNumerator() << "/" << p.getDenominator() << endl; 
	cout << "q is " << q.getNumerator() << "/" << q.getDenominator() << endl; 
	
	Rational r; 
	Rational s; 
	cout << "Enter a rational number (a/b): "; 
	cin >> r; 
	cout << "Enter a rational number (a/b): "; 
	cin >> s; 

	cout << "You entered the rational numbers " << r << " and " << s << endl;
	
	//Confirm +,-,*, and / works with rationals.
	Rational sum = r + s;
	Rational product = r * s;
	Rational difference = r - s;
	Rational divide = r/s;

	// Test greater than, greater than or equal to, and isEqual
	cout << "Changing value of r and s..." << endl;
	r.setNumerator(3);
	r.setDenominator(4);
	
	s.setNumerator(1);
	s. setDenominator(4);

	if (r >= s)
	{
		cout << r << " is greater than or equal to " << s << endl;
		if (r > s)
		{
			cout << "okay..." << r << " is actually greater than " << s << endl;
		}
		else if (r== s)
		{
			cout << "okay..." << r << " is actually equal to " <<  s << endl;
		}
	}
	else
	{
		cout << "Oops, I enter an incorrect rational to test greater than or equal to!" << endl;
	}

	// Test less than, less than or equal to, and isEqual(again)
	cout << "Changing value of r and s again..." << endl;
	r.setNumerator(1);
	r.setDenominator(4);

	s.setNumerator(3);
	s. setDenominator(4);

	if (r <= s)
	{
		cout << r << " is less than or equal to " << s << endl;
		if (r < s)
		{
			cout << "okay..." << r << " is actually less than " << s << endl;
		}
		else if (r== s)
		{
			cout << "okay..." << r  << " is actually equal to " << s << endl;
		}
	}
	else
	{
		cout << "Oops, I enter an incorrect rational to test less than or equal to!" << endl;
	}
	
	// Test the ability to cout new ADT.
	cout << r << " + " << s << " = " << sum << endl;
	cout << r << " * " << s << " = " << product << endl;
	cout << r << " - " << s << " = " << difference << endl;
	cout << r << " / " << s << " = " << divide << endl;
	
	// Test cloning or coping Rational ADT
	Rational t(s); 
	double tFloat = t.convertToFloatingPoint();
	cout << "s copied the rational " << s << " to t " << t << "." << endl;
	cout << "t in decimal equals: " << tFloat << endl; 

	// Test reducing rational **extra credit***
	r.setNumerator(12);
	r.setDenominator(4);

	s.setNumerator(6);
	s. setDenominator(8);
	
	r.reduce();
	s.reduce();

	cout << "r is now reduced to " << r << endl;
	cout << "s is now reduced to " << s << endl;
	cin.ignore();
	cin.get();
	return 0; 
}
Ejemplo n.º 15
0
const Rational operator*(const Rational & lhs, const Rational & rhs)
{
    Rational ra(lhs.getNumerator()*rhs.getNumerator(), lhs.getDenominator()*rhs.getDenominator());
    return ra;
}
Ejemplo n.º 16
0
Rational Rational::subtraction(const Rational& num)
{
    int newDen = lcm(num.getDenominator(), _den);
    Rational x(_neu * newDen / _den - num.getNumerator() * newDen / num.getDenominator(), newDen);
    return x;
}
Ejemplo n.º 17
0
Rational Rational::add( const Rational & other ) const{
	return Rational((getNumerator() * other.getDenominator() + other.getNumerator() * getDenominator()), (getDenominator() * other.getDenominator()));
}
Ejemplo n.º 18
0
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;
  }
  } // End case

  // Difference logic OK
  return ok;
}
Ejemplo n.º 19
0
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;
    }
}
Ejemplo n.º 20
0
Rational Rational::division(const Rational& num)
{
    return Rational(_neu * num.getDenominator(), _den * num.getNumerator());
}
Ejemplo n.º 21
0
 void testSimplify()
 {
     Rational r = Rational(2,4);
     r.simplify();
     cout << "Should be (1,2) and was (" << r.getNumerator() << ", " << r.getDenominator() << ").";
 }
Ejemplo n.º 22
0
 const Rational operator -(const Rational& r){
     BigInt n(r.getNumerator() * BigInt("-1"));
     return Rational(n, r.getDenominator());
 }