/// Does an unsigned compare int burns::compare(const vector<uint64>& x, const vector<uint64>& y) { // First try fractional uint64 xf = fractional(x); uint64 yf = fractional(y); if(xf + size() > xf && yf + size() > yf) { if(xf > yf + size()) return 1; if(xf + size() < yf) return -1; } // Try equality if(xf == yf && equals(x, y)) return 0; // Then compare mixed radix vector<uint64> xmr = x; vector<uint64> ymr = y; to_mixed_radix(xmr); to_mixed_radix(ymr); for(int i = 0; i < size(); i++) { if(xmr[i] > ymr[i]) return 1; if(xmr[i] < ymr[i]) return -1; } // Error return 0; }
/** * Serialize sign and value using radix point (no exponent). */ std::string NValue::createStringFromDecimal() const { assert(!isNull()); std::ostringstream buffer; TTInt scaledValue = getDecimal(); if (scaledValue.IsSign()) { buffer << '-'; } TTInt whole(scaledValue); TTInt fractional(scaledValue); whole /= NValue::kMaxScaleFactor; fractional %= NValue::kMaxScaleFactor; if (whole.IsSign()) { whole.ChangeSign(); } buffer << whole.ToString(10); buffer << '.'; if (fractional.IsSign()) { fractional.ChangeSign(); } std::string fractionalString = fractional.ToString(10); for (int ii = static_cast<int>(fractionalString.size()); ii < NValue::kMaxDecScale; ii++) { buffer << '0'; } buffer << fractionalString; return buffer.str(); }
int main(int argc, char *argv[]) { int ch; while((ch = getopt(argc, argv, "ife")) != -1) { switch(ch) { case 'i': integral(); break; case 'f': fractional(); break; case 'e': endian(); break; default: exit(1); } } }
/** * Set a decimal value from a serialized representation * This function does not handle scientific notation string, Java planner should convert that to plan string first. */ void NValue::createDecimalFromString(const std::string &txt) { if (txt.length() == 0) { throw SQLException(SQLException::volt_decimal_serialization_error, "Empty string provided"); } bool setSign = false; if (txt[0] == '-') { setSign = true; } /** * Check for invalid characters */ for (int ii = (setSign ? 1 : 0); ii < static_cast<int>(txt.size()); ii++) { if ((txt[ii] < '0' || txt[ii] > '9') && txt[ii] != '.') { char message[4096]; snprintf(message, 4096, "Invalid characters in decimal string: %s", txt.c_str()); throw SQLException(SQLException::volt_decimal_serialization_error, message); } } std::size_t separatorPos = txt.find( '.', 0); if (separatorPos == std::string::npos) { const std::string wholeString = txt.substr( setSign ? 1 : 0, txt.size()); const std::size_t wholeStringSize = wholeString.size(); if (wholeStringSize > 26) { throw SQLException(SQLException::volt_decimal_serialization_error, "Maximum precision exceeded. Maximum of 26 digits to the left of the decimal point"); } TTInt whole(wholeString); if (setSign) { whole.SetSign(); } whole *= kMaxScaleFactor; getDecimal() = whole; return; } if (txt.find( '.', separatorPos + 1) != std::string::npos) { throw SQLException(SQLException::volt_decimal_serialization_error, "Too many decimal points"); } const std::string wholeString = txt.substr( setSign ? 1 : 0, separatorPos - (setSign ? 1 : 0)); const std::size_t wholeStringSize = wholeString.size(); if (wholeStringSize > 26) { throw SQLException(SQLException::volt_decimal_serialization_error, "Maximum precision exceeded. Maximum of 26 digits to the left of the decimal point"); } TTInt whole(wholeString); std::string fractionalString = txt.substr( separatorPos + 1, txt.size() - (separatorPos + 1)); // remove trailing zeros while (fractionalString.size() > 0 && fractionalString[fractionalString.size() - 1] == '0') fractionalString.erase(fractionalString.size() - 1, 1); // check if too many decimal places if (fractionalString.size() > 12) { throw SQLException(SQLException::volt_decimal_serialization_error, "Maximum scale exceeded. Maximum of 12 digits to the right of the decimal point"); } while(fractionalString.size() < NValue::kMaxDecScale) { fractionalString.push_back('0'); } TTInt fractional(fractionalString); whole *= kMaxScaleFactor; whole += fractional; if (setSign) { whole.SetSign(); } getDecimal() = whole; }
void ItemLineEdit::silentSetId(const int pId) { if (DEBUG) qDebug("%s::silentSetId(%d) entered", qPrintable(objectName()), pId); XSqlQuery item; bool found = FALSE; _parsed = TRUE; if (_useValidationQuery) { item.prepare(_validationSql); item.bindValue(":item_id", pId); item.exec(); if (item.first()) found = TRUE; } else if (_useQuery) { item.prepare(_sql); item.exec(); found = (item.findFirst("item_id", pId) != -1); } else if (pId != -1) { QString pre( "SELECT DISTINCT item_number, item_descrip1, item_descrip2," " uom_name, item_type, item_config, item_fractional, item_upccode"); QStringList clauses; clauses = _extraClauses; clauses << "(item_id=:item_id)"; item.prepare(buildItemLineEditQuery(pre, clauses, QString::null, _type, false)); item.bindValue(":item_id", pId); item.exec(); found = item.first(); } if (found) { if (completer()) { disconnect(this, SIGNAL(textChanged(QString)), this, SLOT(sHandleCompleter())); static_cast<QSqlQueryModel* >(completer()->model())->setQuery(QSqlQuery()); } _itemNumber = item.value("item_number").toString(); _uom = item.value("uom_name").toString(); _itemType = item.value("item_type").toString(); _configured = item.value("item_config").toBool(); _fractional = item.value("item_fractional").toBool(); _upc = item.value("item_upccode").toString(); _id = pId; _valid = TRUE; setText(item.value("item_number").toString()); emit aliasChanged(""); emit typeChanged(_itemType); emit descrip1Changed(item.value("item_descrip1").toString()); emit descrip2Changed(item.value("item_descrip2").toString()); emit uomChanged(item.value("uom_name").toString()); emit configured(item.value("item_config").toBool()); emit fractional(item.value("item_fractional").toBool()); emit upcChanged(item.value("item_upccode").toString()); emit valid(TRUE); if (completer()) connect(this, SIGNAL(textChanged(QString)), this, SLOT(sHandleCompleter())); } else { _itemNumber = ""; _uom = ""; _itemType = ""; _id = -1; _upc = ""; _valid = FALSE; setText(""); emit aliasChanged(""); emit typeChanged(""); emit descrip1Changed(""); emit descrip2Changed(""); emit uomChanged(""); emit configured(FALSE); emit fractional(FALSE); emit upcChanged(""); emit valid(FALSE); } }
void ItemLineEdit::setItemNumber(const QString& pNumber) { if (DEBUG) qDebug("%s::setItemNumber(%s) entered", qPrintable(objectName()), qPrintable(pNumber)); XSqlQuery item; bool found = FALSE; _parsed = TRUE; if (pNumber == text()) return; if (!pNumber.isEmpty()) { if (_useValidationQuery) { item.prepare(_validationSql); item.bindValue(":item_number", pNumber); item.exec(); if (item.first()) found = TRUE; } else if (_useQuery) { item.prepare(_sql); item.exec(); found = (item.findFirst("item_number", pNumber) != -1); } else if (pNumber != QString::Null()) { QString pre( "SELECT DISTINCT item_id, item_number, item_descrip1, item_descrip2," " uom_name, item_type, item_config, item_fractional, item_upccode"); QStringList clauses; clauses = _extraClauses; clauses << "(item_number=:item_number OR item_upccode=:item_number)"; item.prepare(buildItemLineEditQuery(pre, clauses, QString::null, _type, false)); item.bindValue(":item_number", pNumber); item.exec(); if (item.size() > 1) { ParameterList params; params.append("search", pNumber); params.append("searchNumber"); params.append("searchUpc"); sSearch(params); return; } else found = item.first(); } } if (found) { _itemNumber = pNumber; _uom = item.value("uom_name").toString(); _itemType = item.value("item_type").toString(); _configured = item.value("item_config").toBool(); _fractional = item.value("item_fractional").toBool(); _id = item.value("item_id").toInt(); _upc = item.value("item_upccode").toInt(); _valid = TRUE; setText(item.value("item_number").toString()); emit aliasChanged(""); emit typeChanged(_itemType); emit descrip1Changed(item.value("item_descrip1").toString()); emit descrip2Changed(item.value("item_descrip2").toString()); emit uomChanged(item.value("uom_name").toString()); emit configured(item.value("item_config").toBool()); emit fractional(item.value("item_fractional").toBool()); emit upcChanged(item.value("item_upccode").toString()); emit valid(TRUE); } else { _itemNumber = ""; _uom = ""; _itemType = ""; _id = -1; _valid = FALSE; _upc = ""; setText(""); emit aliasChanged(""); emit typeChanged(""); emit descrip1Changed(""); emit descrip2Changed(""); emit uomChanged(""); emit configured(FALSE); emit fractional(FALSE); emit upcChanged(""); emit valid(FALSE); } }
sample_t Resampler::resample_() { // Index of first input sample in window. size_t ind_begin_prev; // Window lasts till that index. const size_t ind_end_prev = frame_size_; size_t ind_begin_cur; size_t ind_end_cur; const size_t ind_begin_next = 0; size_t ind_end_next; if ((qt_sample_ & FRACT_PART_MASK) < G_qt_epsilon) { qt_sample_ &= INTEGER_PART_MASK; } else if ((G_qt_one - (qt_sample_ & FRACT_PART_MASK)) < G_qt_epsilon) { qt_sample_ &= INTEGER_PART_MASK; qt_sample_ += G_qt_one; } ind_begin_prev = (qt_sample_ >= G_qt_half_window_len) ? frame_size_ : fixedpoint_to_size(qceil(qt_sample_ + (qt_frame_size_ - G_qt_half_window_len))); roc_panic_if(ind_begin_prev > frame_size_); ind_begin_cur = (qt_sample_ >= G_qt_half_window_len) ? fixedpoint_to_size(qceil(qt_sample_ - G_qt_half_window_len)) : 0; roc_panic_if(ind_begin_cur > frame_size_); ind_end_cur = ((qt_sample_ + G_qt_half_window_len) > qt_frame_size_) ? frame_size_ : fixedpoint_to_size(qfloor(qt_sample_ + G_qt_half_window_len)); roc_panic_if(ind_end_cur > frame_size_); ind_end_next = ((qt_sample_ + G_qt_half_window_len) > qt_frame_size_) ? fixedpoint_to_size(qfloor(qt_sample_ + G_qt_half_window_len - qt_frame_size_)) : 0; roc_panic_if(ind_end_next > frame_size_); // Counter inside window. // t_sinc = t_sample - ceil( t_sample - st_Nwindow + 1/st_Nwindow_interp ) fixedpoint_t qt_sinc_cur = qt_frame_size_ + qt_sample_ - qceil(qt_frame_size_ + qt_sample_ - G_qt_half_window_len); // sinc_table defined in positive half-plane, so at the begining of the window // qt_sinc_cur starts decreasing and after we cross 0 it will be increasing // till the end of the window. signed_fixedpoint_t qt_sinc_inc = -((signed_fixedpoint_t)G_qt_one); // Compute fractional part of time position at the begining. It wont change during // the run. float f_sinc_cur_fract = fractional(qt_sinc_cur << FRACT_BIT_TO_INDEX); sample_t accumulator = 0; size_t i; // Run through previous frame. for (i = ind_begin_prev; i < ind_end_prev; ++i) { accumulator += prev_frame_[i] * sinc(qt_sinc_cur, f_sinc_cur_fract); qt_sinc_cur += (fixedpoint_t)qt_sinc_inc; } // Run through current frame through the left windows side. qt_sinc_cur is decreasing. i = ind_begin_cur; accumulator += curr_frame_[i] * sinc(qt_sinc_cur, f_sinc_cur_fract); while (qt_sinc_cur >= G_qt_one) { ++i; qt_sinc_cur += (fixedpoint_t)qt_sinc_inc; accumulator += curr_frame_[i] * sinc(qt_sinc_cur, f_sinc_cur_fract); } ++i; roc_panic_if(i > frame_size_); // Run through right side of the window, increasing qt_sinc_cur. qt_sinc_inc = -qt_sinc_inc; // Crossing zero -- we just need to switch qt_sinc_cur. // -1 ------------ 0 ------------- +1 // ^ ^ // | | // -qt_sinc_cur -> +qt_sinc_cur <=> qt_sinc_cur = 1 - qt_sinc_cur qt_sinc_cur = G_qt_one - qt_sinc_cur; // qt_sinc_cur = -qt_sinc_cur + 1; f_sinc_cur_fract = fractional(qt_sinc_cur << FRACT_BIT_TO_INDEX); for (; i < ind_end_cur; ++i) { accumulator += curr_frame_[i] * sinc(qt_sinc_cur, f_sinc_cur_fract); qt_sinc_cur += (fixedpoint_t)qt_sinc_inc; } // Next frames run. for (i = ind_begin_next; i < ind_end_next; ++i) { accumulator += next_frame_[i] * sinc(qt_sinc_cur, f_sinc_cur_fract); qt_sinc_cur += (fixedpoint_t)qt_sinc_inc; } return accumulator; }
/** * Set a decimal value from a serialized representation * This function does not handle scientific notation string, Java planner should convert that to plan string first. */ void NValue::createDecimalFromString(const std::string &txt) { if (txt.length() == 0) { throw SQLException(SQLException::volt_decimal_serialization_error, "Empty string provided"); } bool setSign = false; if (txt[0] == '-') { setSign = true; } /** * Check for invalid characters */ for (int ii = (setSign ? 1 : 0); ii < static_cast<int>(txt.size()); ii++) { if ((txt[ii] < '0' || txt[ii] > '9') && txt[ii] != '.') { char message[4096]; snprintf(message, 4096, "Invalid characters in decimal string: %s", txt.c_str()); throw SQLException(SQLException::volt_decimal_serialization_error, message); } } std::size_t separatorPos = txt.find( '.', 0); if (separatorPos == std::string::npos) { const std::string wholeString = txt.substr( setSign ? 1 : 0, txt.size()); const std::size_t wholeStringSize = wholeString.size(); if (wholeStringSize > 26) { throw SQLException(SQLException::volt_decimal_serialization_error, "Maximum precision exceeded. Maximum of 26 digits to the left of the decimal point"); } TTInt whole(wholeString); if (setSign) { whole.SetSign(); } whole *= kMaxScaleFactor; getDecimal() = whole; return; } if (txt.find( '.', separatorPos + 1) != std::string::npos) { throw SQLException(SQLException::volt_decimal_serialization_error, "Too many decimal points"); } // This is set to 1 if we carry in the scale. int carryScale = 0; // This is set to 1 if we carry from the scale to the whole. int carryWhole = 0; // Start with the fractional part. We need to // see if we need to carry from it first. std::string fractionalString = txt.substr( separatorPos + 1, txt.size() - (separatorPos + 1)); // remove trailing zeros while (fractionalString.size() > 0 && fractionalString[fractionalString.size() - 1] == '0') fractionalString.erase(fractionalString.size() - 1, 1); // // If the scale is too large, then we will round // the number to the nearest 10**-12, and to the // furthest from zero if the number is equidistant // from the next highest and lowest. This is the // definition of the Java rounding mode HALF_UP. // // At some point we will read a rounding mode from the // Java side at Engine configuration time, or something // like that, and have a whole flurry of rounding modes // here. However, for now we have just the one. // if (fractionalString.size() > kMaxDecScale) { carryScale = ('5' <= fractionalString[kMaxDecScale]) ? 1 : 0; fractionalString = fractionalString.substr(0, kMaxDecScale); } else { while(fractionalString.size() < NValue::kMaxDecScale) { fractionalString.push_back('0'); } } TTInt fractional(fractionalString); // If we decided to carry above, then do it here. // The fractional string is set up so that it represents // 1.0e-12 * units. fractional += carryScale; if (TTInt((uint64_t)kMaxScaleFactor) <= fractional) { // We know fractional was < kMaxScaleFactor before // we rounded, since fractional is 12 digits and // kMaxScaleFactor is 13. So, if carrying makes // the fractional number too big, it must be eactly // too big. That is to say, the rounded fractional // number number has become zero, and we need to // carry to the whole number. fractional = 0; carryWhole = 1; } // Process the whole number string. const std::string wholeString = txt.substr( setSign ? 1 : 0, separatorPos - (setSign ? 1 : 0)); // We will check for oversize numbers below, so don't waste time // doing it now. TTInt whole(wholeString); whole += carryWhole; if (oversizeWholeDecimal(whole)) { throw SQLException(SQLException::volt_decimal_serialization_error, "Maximum precision exceeded. Maximum of 26 digits to the left of the decimal point"); } whole *= kMaxScaleFactor; whole += fractional; if (setSign) { whole.SetSign(); } getDecimal() = whole; }