/** * 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(); }
bool SBBTA252::parseStatus() { // Declare variables. char *msg = Serial::buf_recv; char address[2]; int rpm, // ranges from -4500 to 4500 current, // Amps * 10 motor_temp, controller_temp, voltage, water_detect, status, faults; address[0] = msg[1]; address[1] = msg[2]; rpm = getDecimal(msg, 3, 8); current = getDecimal(msg, 10, 12); motor_temp = getDecimal(msg, 14, 16); controller_temp = getDecimal(msg, 18, 20); voltage = getDecimal(msg, 22, 24); water_detect = getDecimal(msg, 26, 28); status = getDecimal(msg, 30, 32); faults = getDecimal(msg, 34, 36); // check for what status and fault messages are here: char checksum[2]; int cs = hex2dec(address) + rpm + current + motor_temp + controller_temp + voltage + water_detect + status + faults; cs = cs % 256; checksum[0] = dec2hex(cs / 16); checksum[1] = dec2hex(cs % 16); if (checksum[0] != msg[38] || checksum[1] != msg[39]) { ROS_ERROR("We received a bad status message. Checksums didn't match."); return false; } else { /* ROS_ERROR("msg=%s.", msg); ROS_ERROR("rpm = %d.", rpm); ROS_ERROR("current = %d.", current); ROS_ERROR("motor_temp = %d.", motor_temp); ROS_ERROR("controller_temp = %d.", controller_temp); ROS_ERROR("voltage = %d.", voltage); ROS_ERROR("water_detect = %d.", water_detect); ROS_ERROR("status = %d.", status); ROS_ERROR("faults = %d.", faults); */ return true; } } // end parseStatus()
//operators implementation bool _NativeFrameworkDSVariant::operator==(_NativeFrameworkDSVariant compareWith){ if(compareWith.getType() == this -> getType()) switch(getType()){ case LONG: return compareWith.getLong() == getLong() ; case DOUBLE: return compareWith.getDecimal() == getDecimal(); case CHAR: return compareWith.getChar() == getChar(); case STRING: return compareWith.getString() == getString(); case C_STR: return compareWith.getString() == getString(); case BOOLEAN: return compareWith.getBool() == getBool(); } }
string fractionToDecimal(int numerator, int denominator) { string result=""; bool sign=numerator<0 != denominator<0; if (numerator==0) sign=false; long numer=abs(static_cast<long> (numerator)); long denom=abs(static_cast<long> (denominator)); if (numer%denom) { result+=to_string(numer/denom); result=sign?("-"+result):result; result+="."; numer=numer%denom; long rcb=getrcb(numer,denom); numer=numer/rcb; denom=denom/rcb; result+=getDecimal(numer,denom); } else { result+=to_string(numer/denom); result=sign?("-"+result):result; } return result; }
double _NativeFrameworkDSVariant::getFloat(){ return getDecimal(); }
bool _NativeFrameworkDSDecimal::operator>=(double number){ return getDecimal() >= number; }
bool _NativeFrameworkDSDecimal::operator!=(double doubleValue){ return getDecimal() != doubleValue; }
bool _NativeFrameworkDSDecimal::operator!=(_NativeFrameworkDSDecimal doubleInstance){ return doubleInstance.getDecimal() != getDecimal(); }
bool _NativeFrameworkDSDecimal::operator==(double doubleValue){ return doubleValue == getDecimal(); }
/** * 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; }
_NativeFrameworkDSDecimal _NativeFrameworkDSDecimal::operator/(double value){ return _NativeFrameworkDSDecimal((double)(getDecimal() / value)); }
_NativeFrameworkDSDecimal _NativeFrameworkDSDecimal::operator/(long value){ return _NativeFrameworkDSDecimal(getDecimal() / value); }
_NativeFrameworkDSDecimal _NativeFrameworkDSDecimal::operator--(){ return _NativeFrameworkDSDecimal(getDecimal()-1); }
_NativeFrameworkDSDecimal _NativeFrameworkDSDecimal::operator++(){ return _NativeFrameworkDSDecimal(getDecimal()+1); }
_NativeFrameworkDSDecimal _NativeFrameworkDSDecimal::operator/(_NativeFrameworkDSDecimal selfInstance){ return _NativeFrameworkDSDecimal(getDecimal() / selfInstance.getDecimal()); }
// appending string -> V1.1 _NativeFrameworkDSString _NativeFrameworkDSDecimal::operator+(const char *cString){ ostringstream out; out << getDecimal(); return _NativeFrameworkDSString(out.str() + string(cString)); }
_NativeFrameworkDSString _NativeFrameworkDSDecimal::operator+(_NativeFrameworkDSString stringInstance){ return _NativeFrameworkDSString("") + getDecimal() + stringInstance; }
void AP_UnixDialog_Lists::loadXPDataIntoLocal(void) { // // This function reads the various memeber variables and loads them into // into the dialog variables. // // // Block all signals while setting these things // XAP_GtkSignalBlocker b1( G_OBJECT(m_oAlignList_adj), m_iAlignListSpinID); XAP_GtkSignalBlocker b2( G_OBJECT(m_oIndentAlign_adj), m_iIndentAlignSpinID); XAP_GtkSignalBlocker b3( G_OBJECT(m_wDecimalEntry), m_iDecimalEntryID); XAP_GtkSignalBlocker b4( G_OBJECT(m_wDelimEntry), m_iDelimEntryID ); // // HACK to effectively block an update during this method // m_bDontUpdate = true; UT_DEBUGMSG(("loadXP newListType = %d \n",getNewListType())); gtk_spin_button_set_value(GTK_SPIN_BUTTON(m_wAlignListSpin),getfAlign()); float indent = getfAlign() + getfIndent(); gtk_spin_button_set_value(GTK_SPIN_BUTTON( m_wIndentAlignSpin),indent); if( (getfIndent() + getfAlign()) < 0.0) { setfIndent( - getfAlign()); gtk_spin_button_set_value(GTK_SPIN_BUTTON( m_wIndentAlignSpin), 0.0); } // // Code to work out which is active Font // if(getFont() == "NULL") { gtk_combo_box_set_active(m_wFontOptions, 0 ); } else { size_t i = 0; for(std::vector<std::string>::const_iterator iter = m_glFonts.begin(); iter != m_glFonts.end(); ++iter, ++i) { if(*iter == getFont()) break; } if(i < m_glFonts.size()) { gtk_combo_box_set_active(m_wFontOptions, i + 1 ); } else { gtk_combo_box_set_active(m_wFontOptions, 0 ); } } gtk_spin_button_set_value(GTK_SPIN_BUTTON(m_wStartSpin),static_cast<float>(getiStartValue())); gtk_entry_set_text( GTK_ENTRY(m_wDecimalEntry), getDecimal().c_str()); gtk_entry_set_text( GTK_ENTRY(m_wDelimEntry), getDelim().c_str()); // // Now set the list type and style FL_ListType save = getNewListType(); if(getNewListType() == NOT_A_LIST) { styleChanged(0); setNewListType(save); gtk_combo_box_set_active(m_wListTypeBox, 0); gtk_combo_box_set_active(m_wListStyleBox, 0); } else if(IS_BULLETED_LIST_TYPE(getNewListType()) ) { styleChanged(1); setNewListType(save); gtk_combo_box_set_active(m_wListTypeBox, 1); gtk_combo_box_set_active(m_wListStyleBox, (gint) (getNewListType() - BULLETED_LIST)); } else { styleChanged(2); setNewListType(save); gtk_combo_box_set_active(m_wListTypeBox, 2); if(getNewListType() < OTHER_NUMBERED_LISTS) { gtk_combo_box_set_active(m_wListStyleBox, getNewListType()); } else { gint iMenu = static_cast<gint>(getNewListType()) - OTHER_NUMBERED_LISTS + BULLETED_LIST -1 ; gtk_combo_box_set_active(m_wListStyleBox,iMenu); } } // // HACK to allow an update during this method // m_bDontUpdate = false; }
/** * 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; }