// this convert miliseconds since midnight into hour/minute/second void MiliMidnightToHMS(unsigned DIM_INT32 ms, unsigned char& hour, unsigned char& minute, unsigned char& second) { hour=0; minute=0; second=0; if (ms == 0) return; int h, m, s, fms, mc, msc; divMod(ms, SECS_PER_MIN * MSECS_PER_SEC, mc, msc); divMod(mc, MINS_PER_HOUR, h, m); divMod(msc, MSECS_PER_SEC, s, fms); hour = h; minute = m; second = s; }
void datetime_decodeTime(DateTime time, int* h, int* m, int* s) // Input: time = decimal fraction of a day // Output: h = hour of day (0-24) // m = minute of hour (0-60) // s = second of minute (0-60) // Purpose: decodes DateTime value to hour:minute:second. { int secs; int mins; secs = (int)(floor((time - floor(time))*SecsPerDay + 0.5)); divMod(secs, 60, &mins, s); divMod(mins, 60, h, m); }
BigInt* BigInt::div(BigInt& value) { DivModData temp = divMod(value); this->copy(*(temp.first)); delete temp.first; delete temp.second; return this; }
/** * Fast approach for continued division<br/> * <b>Note:</b> This method has a overhead that can make the naïve algorithm faster, * however I have decreased the overhead and it appears to be best always the use this method * * @param remainder The integer without any factor of the factor, the result * @param integer The integer to divide * @param factor The integer with which to divide * @param output The buffer to write the factor to once for every time the factor divides the integer * @param outputPtr The buffer's pointer * @param rootOrder The root order * @return The number of this the factor is dividable */ int contDiv(Bignum remainder, Bignum integer, Bignum factor, Buffer output, long* outputPtr, int rootOrder) { //Just like n↑m by squaring, excepted this is division String string = bignumToString(factor); long ropt, i; int times = 0; //number of times the integer is dividable int ptimes = 1; //Partial number of times the integer is dividable Bignum n; mpz_init_set(n, integer); //Intger to divide Bignum q; mpz_init(q); //Quotient Bignum r; mpz_init(r); //Remainder Bignum x; mpz_init_set(x, factor); //Factor Bignum* divStack = malloc(sizeof(Bignum) * 6); //lb log₃ 2¹⁰⁰ ≈ 6 mpz_init_set(*divStack++, factor); for (;;) { mpz_mul(x, x, x); if (mpz_cmp(x, n) <= 0) { mpz_init_set(*divStack++, x); ptimes <<= 1; } else { ropt = rootOrder * ptimes; while (ptimes) { divMod(q, r, n, *--divStack); if (equals(r, 0)) { for (i = 0; i < ropt; i++) { appendToBuffer(output, *outputPtr, string); appendToBuffer(output, (*outputPtr) + 1, "\n"); (*outputPtr) += 2; } times |= ptimes; mpz_set(n, q); } ropt >>= 1; ptimes >>= 1; } mpz_set(remainder, n); return times; } } }
GFx& GFx::setIntegerValue(const Z& src) { // if( src >= this->_gfGenerator.getOrder() ){ // std::ostringstream oss; // oss << "Integer '" << src << "' too big for a GFx with order '" << this->_gfGenerator.getOrder() << "'"; // throw Errors::TooBig( oss.str() ); // } const Z& p(_gfGenerator.getCharacteristic()); this->_data.clear(); Z_p q(p,false),r(p,false); //don't check for the primality of p divMod(src % this->_gfGenerator.getOrder(), p, &q, &r); this->_data.push_back(r); while( q >= p ){ divMod(Z(q), p, &q, &r); this->_data.push_back(r); } // q < p this->_data.push_back(q); this->_eraseLeadingZeros(); return *this; }
BigInt* BigInt::mod(BigInt& value) { if (value.cmp(*TWO) == 0) { if (*(innerData->begin()) % 2 == 1) { this->copy(*ONE); } else { this->copy(*ZERO); } return this; } DivModData temp = divMod(value); this->copy(*(temp.second)); delete temp.first; delete temp.second; return this; }
void datetime_decodeDate(DateTime date, int* year, int* month, int* day) // Input: date = encoded date/time value // Output: year = 4-digit year // month = month of year (1-12) // day = day of month // Purpose: decodes DateTime value to year-month-day. { int D1, D4, D100, D400; int y, m, d, i, k, t; D1 = 365; //365 D4 = D1 * 4 + 1; //1461 D100 = D4 * 25 - 1; //36524 D400 = D100 * 4 + 1; //146097 t = (int)(floor (date)) + DateDelta; if (t <= 0) { *year = 0; *month = 1; *day = 1; } else { t--; y = 1; while (t >= D400) { t -= D400; y += 400; } divMod(t, D100, &i, &d); if (i == 4) { i--; d += D100; } y += i*100; divMod(d, D4, &i, &d); y += i*4; divMod(d, D1, &i, &d); if (i == 4) { i--; d += D1; } y += i; k = isLeapYear(y); m = 1; for (;;) { i = DaysPerMonth[k][m-1]; if (d < i) break; d -= i; m++; } *year = y; *month = m; *day = d + 1; } }