Rational operator^(Rational num1, int power) { int c = pow(num1.getB(), power); int d = pow(num1.getA(), power); return Rational(d, c); }
Boolean* Rational::JudgeLessThan(Number* obj){ Rational* toCheck = SCAST_RATIONAL(this->sub(obj->toExact())); return new Boolean(toCheck->sgn() < 0 ); }
void bvisit(const Rational &x) { *numer_ = x.get_num(); *denom_ = x.get_den(); }
// globale Funktionen const Rational add(const Rational& a, const Rational& b) { Rational r = a; r.add(b); return r; }
#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)); } }
// print rational r followed by a newline void prrat(Rational r) { cout << r.n() << "/" << r.d() << endl; }
const Rational div(const Rational& z, const Rational& n) { Rational r = z; r.div(n); return r; }
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; } }
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; }
Rational abs(const Rational& r) { return r.abs(); }
Rational pow(const Rational& r, const long exponent) { return r.pow(exponent); }
std::size_t hash_value(const Rational& r) { return r.hash(); }
// 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; }
// 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; }
Rational Rational::add( const Rational & other ) const{ return Rational((getNumerator() * other.getDenominator() + other.getNumerator() * getDenominator()), (getDenominator() * other.getDenominator())); }
Rational operator*(const Rational& a, const Rational& b) { int n = a.numerator()*b.numerator(); int d = a.denumerator()*b.denumerator(); return Rational {n,d}; }
bool operator <( Rational const & a , Rational const & b ) { return a.to_double() < b.to_double(); }
bool operator==(Rational a, Rational b) { if (b.numerator() == a.numerator() && b.denumerator() == a.denumerator()) return true; return false; }
const Rational mult(const Rational& a, const Rational& b) { Rational r = a; r.mult(b); return r; }
void operator<<(ostream& os, const Rational& n) { os << n.numerator() << "/" << n.denumerator() << endl; }
void Rational::div(const Rational& n) { Rational r = n; r.kehrwert(); mult(r); }
double to_double(const Rational& a) { return double(a.numerator())/double(a.denumerator()); }
const Rational sub(const Rational& a, const Rational& b) { Rational r = a; r.sub(b); return r; }
void Stream::setSampleAspectRatio(const Rational &aspectRatio) { RAW_SET2(isValid(), sample_aspect_ratio, aspectRatio.getValue()); }
Number* Rational::getMin(Number* obj){ Rational* toCheck = SCAST_RATIONAL(this->sub(obj->toExact())); return new Rational(toCheck->sgn()<0 ? (*this) :(*(SCAST_RATIONAL(obj)))); }
void Stream::setAverageFrameRate(const Rational &frameRate) { RAW_SET2(isValid(), avg_frame_rate, frameRate.getValue()); }
flint::fmpqxx URatPSeriesFlint::convert(const Rational &x) { return convert(x.as_mpq()); }
void Stream::setTimeBase(const Rational &timeBase) { RAW_SET2(isValid(), time_base, timeBase.getValue()); }
bool operator==(Rational& r1, Rational& r2){ if( r1.getN() == r2.getN() && r1.getD() == r2.getD() ){ return true; } return false; }
Rational operator*(Rational num1, Rational num2) { int c = num1.getB() * num2.getB(); int d = num1.getA() * num2.getA(); return Rational(d, c); }