示例#1
0
void test_some_math()
{
      // GCD tests
      assert(gcd(54,24) == gcd(12,90) && gcd(13,11) == 1);
      // LCM tests
      assert(lcm(114920, 14300) == 6320600);
      // Binomial coefficient test
      assert(binomial(10,2) == 45);
      // Test add
      assert(add(3,5) == 8 && add(7,3) == 10);
      // Test odd or even numbers
      assert(!is_even(1) && is_even(2) && !is_even(77) && is_even(500));
      // Test if bit n is set
      assert(!isbitNset(22, 3) && isbitNset(22,4));
      // Test set bit
      assert(setbitN(22,3) == 30 && setbitN(2,0) == 3);
      // Test unset bit
      assert(unsetbitN(30,3) == 22 && unsetbitN(3,0) == 2);
      // Test toggle
      assert(togglebitN(togglebitN(22,3),3) == 22);
      // Test modulo (power of two)
      assert(modulo(7,2) == 1 && modulo(6666,2) == 0 && modulo(64,8) == 0);
      // Test negate nibbles
      assert(neg_nibbles(173,0) == 160 && neg_nibbles(429,1) == 256);;
      // Test powX
      assert(powX(7,11) == 1977326743 && pow(2,8) == 256 && pow(123123,0) == 1);
}
示例#2
0
/*
 * This function is only meant to be called from compose. It puts in a value
 * for y, and then merges it with the x value
 */
PolyXY compoY(const PolyXY& q, const PolyXY& p)
{
    PolyXY poly;
    PolyXY temp(p);
    Variable x,y;
    termXY *qptr = q.start;
    termXY *pos=NULL;
    termXY *pptr = p.start;
    int qdegree = 0;
    poly.start = NULL;

    while (qptr!=NULL) {
        qdegree = qptr->ny;
        // Multiply out y term
        if(qdegree > 0) {
            temp = p;
            for(int i = 1; i < qdegree; i++)
                temp = temp * p;// Multiply by x term if it exists
            if(qptr->nx > 0)
                temp = temp * powX(x,qptr->nx);

            temp = temp * qptr->an;
            poly += temp;
        } else {
            // Multiply by x term if it exists
            if(qptr->nx > 0) {
                // poly = poly + powX(x,qptr->nx);
                temp = powX(x,qptr->nx);
                temp = temp * qptr->an;
                poly = poly + temp;
            } else
                poly = poly + qptr->an;
        }

        qptr = qptr->next;
        pptr = p.start;
    }

    return poly;
}