本文整理汇总了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);
}
示例2: SCAST_RATIONAL
Boolean* Rational::JudgeLessThan(Number* obj){
Rational* toCheck = SCAST_RATIONAL(this->sub(obj->toExact()));
return new Boolean(toCheck->sgn() < 0 );
}
示例3: bvisit
void bvisit(const Rational &x)
{
*numer_ = x.get_num();
*denom_ = x.get_den();
}
示例4: add
// globale Funktionen
const Rational add(const Rational& a, const Rational& b) {
Rational r = a;
r.add(b);
return r;
}
示例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));
}
}
示例6: prrat
// print rational r followed by a newline
void prrat(Rational r) {
cout << r.n() << "/" << r.d() << endl;
}
示例7: div
const Rational div(const Rational& z, const Rational& n) {
Rational r = z;
r.div(n);
return r;
}
示例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;
}
}
示例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;
}
//.........这里部分代码省略.........
示例10: abs
Rational abs(const Rational& r)
{
return r.abs();
}
示例11: pow
Rational pow(const Rational& r, const long exponent)
{
return r.pow(exponent);
}
示例12: hash_value
std::size_t hash_value(const Rational& r)
{
return r.hash();
}
示例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;
}
示例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;
}
示例15: Rational
Rational Rational::add( const Rational & other ) const{
return Rational((getNumerator() * other.getDenominator() + other.getNumerator() * getDenominator()), (getDenominator() * other.getDenominator()));
}