You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2007/08/03 21:41:02 UTC
svn commit: r562561 - in
/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang: Double.cpp
Double.h Float.cpp Float.h
Author: tabish
Date: Fri Aug 3 12:41:01 2007
New Revision: 562561
URL: http://svn.apache.org/viewvc?view=rev&rev=562561
Log:
http://issues.apache.org/activemq/browse/AMQCPP-103
Adding in more Types wrappers
Modified:
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.cpp
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.h
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.cpp
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.h
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.cpp?view=diff&rev=562561&r1=562560&r2=562561
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.cpp Fri Aug 3 12:41:01 2007
@@ -25,6 +25,9 @@
////////////////////////////////////////////////////////////////////////////////
const double Double::MAX_VALUE = 1.7976931348623157e+308;
const double Double::MIN_VALUE = 2.2250738585072014e-308;
+const double Double::NaN = 0.0f / 0.0f;
+const double Double::POSITIVE_INFINITY = 1.0f / 0.0f;
+const double Double::NEGATIVE_INFINITY = -1.0f / 0.0f;
////////////////////////////////////////////////////////////////////////////////
Double::Double( double value ) {
@@ -33,22 +36,128 @@
////////////////////////////////////////////////////////////////////////////////
Double::Double( const std::string& value ) throw( exceptions::NumberFormatException ) {
- //TODO this->value = Double::parseFloat( value );
+ this->value = Double::parseDouble( value );
}
////////////////////////////////////////////////////////////////////////////////
int Double::compareTo( const Double& d ) const {
- // TODO
- return this->value < d.value ? -1 : this->value == d.value ? 0 : 1;
+ return Double::compare( this->value, d.value );
}
////////////////////////////////////////////////////////////////////////////////
int Double::compareTo( const double& d ) const {
- // TODO
- return this->value < d ? -1 : this->value == d ? 0 : 1;
+ return Double::compare( this->value, d );
}
////////////////////////////////////////////////////////////////////////////////
std::string Double::toString() const {
- return ""; //TODO Float::toString( this->value, 10 );
+ return Double::toString( this->value );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool Double::isInfinite() const {
+ return Double::isInfinite( this->value );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool Double::isNaN() const {
+ return Double::isNaN( this->value );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int Double::compare( double d1, double d2 ) {
+
+ long long l1, l2 = 0;
+ long NaNbits = Double::doubleToLongBits( Double::NaN );
+
+ if( ( l1 = Double::doubleToLongBits( d1 ) ) == NaNbits ) {
+ if( Double::doubleToLongBits( d2 ) == NaNbits ) {
+ return 0;
+ }
+ return 1;
+ }
+
+ if( ( l2 = Double::doubleToLongBits( d2 ) ) == NaNbits ) {
+ return -1;
+ }
+
+ if( d1 == d2 ) {
+ if( l1 == l2 ) {
+ return 0;
+ }
+
+ // check for -0
+ return l1 > l2 ? 1 : -1;
+ }
+
+ return d1 > d2 ? 1 : -1;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+long long Double::doubleToLongBits( double value ) {
+
+ long long longValue = 0;
+ memcpy( &longValue, &value, sizeof( double ) );
+
+ if( ( longValue & DOUBLE_EXPONENT_MASK ) == DOUBLE_EXPONENT_MASK ) {
+ if( longValue & DOUBLE_MANTISSA_MASK ) {
+ return DOUBLE_NAN_BITS;
+ }
+ }
+
+ return longValue;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+long long Double::doubleToRawLongBits( double value ) {
+
+ long long longValue = 0;
+ memcpy( &longValue, &value, sizeof( double ) );
+ return longValue;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool Double::isInfinite( double value ) {
+ return ( value == POSITIVE_INFINITY ) || ( value == NEGATIVE_INFINITY );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+bool Double::isNaN( double value ) {
+ return value != value;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+double Double::longBitsToDouble( long long bits ) {
+ double result = 0;
+ memcpy( &result, &bits, sizeof( long long ) );
+ return result;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+double Double::parseDouble( const std::string value )
+ throw ( exceptions::NumberFormatException ) {
+
+ return 0; // TODO
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::string Double::toHexString( double value ) {
+ return ""; //TODO
+}
+
+////////////////////////////////////////////////////////////////////////////////
+std::string Double::toString( double value ) {
+ return ""; //TODO
+}
+
+////////////////////////////////////////////////////////////////////////////////
+Double Double::valueOf( double value ) {
+ return Double( value );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+Double Double::valueOf( const std::string& value )
+ throw ( exceptions::NumberFormatException ) {
+
+ return valueOf( parseDouble( value ) );
}
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.h?view=diff&rev=562561&r1=562560&r2=562561
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Double.h Fri Aug 3 12:41:01 2007
@@ -45,6 +45,15 @@
/** The minimum value that the primitive type can hold */
static const double MIN_VALUE;
+ /** Constant for the Not a Number Value */
+ static const double NaN;
+
+ /** Constant for Positive Infinity */
+ static const double POSITIVE_INFINITY;
+
+ /** Constant for Negative Infinitiy */
+ static const double NEGATIVE_INFINITY;
+
public:
/**
@@ -143,7 +152,7 @@
* @return double the value of the receiver.
*/
virtual double doubleValue() const {
- return (double)this->value;
+ return this->value;
}
/**
@@ -151,7 +160,7 @@
* @return float the value of the receiver.
*/
virtual float floatValue() const {
- return this->value;
+ return (float)this->value;
}
/**
@@ -185,6 +194,216 @@
virtual long long longValue() const {
return (long long)this->value;
}
+
+ /**
+ * @returns true if the double is equal to positive infinity.
+ */
+ bool isInfinite() const;
+
+ /**
+ * @returns true if the double is equal to NaN.
+ */
+ bool isNaN() const;
+
+ public: // Statics
+
+ /**
+ * Compares the two specified double values. The sign of the integer value
+ * returned is the same as that of the integer that would be returned by the
+ * call:
+ * new Double(d1).compareTo(new Double(d2))
+ * @param d1 - the first double to compare
+ * @param d2 - the second double to compare
+ * @returns the value 0 if d1 is numerically equal to d2; a value less than
+ * 0 if d1 is numerically less than d2; and a value greater than 0 if d1 is
+ * numerically greater than d2.
+ */
+ static int compare( double d1, double d2 );
+
+ /**
+ * Returns a representation of the specified floating-point value according
+ * to the IEEE 754 floating-point "double format" bit layout.
+ *
+ * Bit 63 (the bit that is selected by the mask 0x8000000000000000L)
+ * represents the sign of the floating-point number. Bits 62-52 (the bits
+ * that are selected by the mask 0x7ff0000000000000L) represent the exponent.
+ * Bits 51-0 (the bits that are selected by the mask 0x000fffffffffffffL)
+ * represent the significand (sometimes called the mantissa) of the
+ * floating-point number.
+ *
+ * If the argument is positive infinity, the result is 0x7ff0000000000000L.
+ * If the argument is negative infinity, the result is 0xfff0000000000000L.
+ * If the argument is NaN, the result is 0x7ff8000000000000L.
+ *
+ * In all cases, the result is a long integer that, when given to the
+ * longBitsToDouble(long) method, will produce a floating-point value the
+ * same as the argument to doubleToLongBits (except all NaN values are
+ * collapsed to a single "canonical" NaN value).
+ * @param value - double to be converted
+ * @returns the long long bits that make up the double
+ */
+ static long long doubleToLongBits( double value );
+
+ /**
+ * Returns a representation of the specified floating-point value according
+ * to the IEEE 754 floating-point "double format" bit layout, preserving
+ * Not-a-Number (NaN) values.
+ *
+ * Bit 63 (the bit that is selected by the mask 0x8000000000000000LL)
+ * represents the sign of the floating-point number. Bits 62-52 (the bits
+ * that are selected by the mask 0x7ff0000000000000L) represent the exponent.
+ * Bits 51-0 (the bits that are selected by the mask 0x000fffffffffffffL)
+ * represent the significand (sometimes called the mantissa) of the
+ * floating-point number.
+ *
+ * If the argument is positive infinity, the result is 0x7ff0000000000000LL.
+ * If the argument is negative infinity, the result is 0xfff0000000000000LL.
+ * If the argument is NaN, the result is the long integer representing the
+ * actual NaN value. Unlike the doubleToLongBits method, doubleToRawLongBits
+ * does not collapse all the bit patterns encoding a NaN to a single
+ * "canonical" NaN value.
+ *
+ * In all cases, the result is a long integer that, when given to the
+ * longBitsToDouble(long) method, will produce a floating-point value the
+ * same as the argument to doubleToRawLongBits.
+ * @param value - double to be converted
+ * @returns the long long bits that make up the double
+ */
+ static long long doubleToRawLongBits( double value );
+
+ /**
+ * @param value - The double to check.
+ * @returns true if the double is equal to infinity.
+ */
+ static bool isInfinite( double value );
+
+ /**
+ * @param value - The double to check.
+ * @returns true if the double is equal to NaN.
+ */
+ static bool isNaN( double value );
+
+ /**
+ * Returns the double value corresponding to a given bit representation.
+ * The argument is considered to be a representation of a floating-point
+ * value according to the IEEE 754 floating-point "double format" bit layout.
+ *
+ * If the argument is 0x7ff0000000000000L, the result is positive infinity.
+ * If the argument is 0xfff0000000000000L, the result is negative infinity.
+ * If the argument is any value in the range 0x7ff0000000000001L through
+ * 0x7fffffffffffffffL or in the range 0xfff0000000000001L through
+ * 0xffffffffffffffffL, the result is a NaN. No IEEE 754 floating-point
+ * operation provided by C++ can distinguish between two NaN values of the
+ * same type with different bit patterns. Distinct values of NaN are only
+ * distinguishable by use of the Double.doubleToRawLongBits method.
+ * @param bits - the long long bits to convert to double
+ * @return the double converted from the bits
+ */
+ static double longBitsToDouble( long long bits );
+
+ /**
+ * Returns a new double initialized to the value represented by the
+ * specified string, as performed by the valueOf method of class Double.
+ * @param value - The string to parse to an double
+ * @returns a double parsed from the passed string
+ * @throws NumberFormatException
+ */
+ static double parseDouble( const std::string value )
+ throw ( exceptions::NumberFormatException );
+
+ /**
+ * Returns a hexadecimal string representation of the double argument. All
+ * characters mentioned below are ASCII characters.
+ *
+ * * If the argument is NaN, the result is the string "NaN".
+ * * Otherwise, the result is a string that represents the sign and magnitude
+ * (absolute value) of the argument. If the sign is negative, the first
+ * character of the result is '-'; if the sign is positive, no sign
+ * character appears in the result. As for the magnitude m:
+ * o If m is infinity, it is represented by the string "Infinity"; thus,
+ * positive infinity produces the result "Infinity" and negative
+ * infinity produces the result "-Infinity".
+ * o If m is zero, it is represented by the string "0x0.0p0"; thus,
+ * negative zero produces the result "-0x0.0p0" and positive zero
+ * produces the result "0x0.0p0".
+ * o If m is a double value with a normalized representation, substrings
+ * are used to represent the significand and exponent fields. The
+ * significand is represented by the characters "0x1." followed by a
+ * lowercase hexadecimal representation of the rest of the
+ * significand as a fraction. Trailing zeros in the hexadecimal
+ * representation are removed unless all the digits are zero, in which
+ * case a single zero is used. Next, the exponent is represented by
+ * "p" followed by a decimal string of the unbiased exponent as if
+ * produced by a call to Integer.toString on the exponent value.
+ * o If m is a double value with a subnormal representation, the
+ * significand is represented by the characters "0x0." followed by a
+ * hexadecimal representation of the rest of the significand as a
+ * fraction. Trailing zeros in the hexadecimal representation are
+ * removed. Next, the exponent is represented by "p-126". Note that
+ * there must be at least one nonzero digit in a subnormal significand.
+ *
+ * @param value - The double to convert to a string
+ * @returns the Hex formatted double string.
+ */
+ static std::string toHexString( double value );
+
+ /**
+ * Returns a string representation of the double argument. All characters
+ * mentioned below are ASCII characters.
+ *
+ * If the argument is NaN, the result is the string "NaN".
+ * Otherwise, the result is a string that represents the sign and magnitude
+ * (absolute value) of the argument. If the sign is negative, the first
+ * character of the result is '-'; if the sign is positive, no
+ * sign character appears in the result. As for the magnitude m:
+ * o If m is infinity, it is represented by the characters "Infinity"; thus,
+ * positive infinity produces the result "Infinity" and negative infinity
+ * produces the result "-Infinity".
+ * o If m is zero, it is represented by the characters "0.0"; thus, negative
+ * zero produces the result "-0.0" and positive zero produces the result
+ * "0.0".
+ * o If m is greater than or equal to 10-3 but less than 107, then it is
+ * represented as the integer part of m, in decimal form with no leading
+ * zeroes, followed by '.', followed by one or more decimal digits
+ * representing the fractional part of m.
+ * o If m is less than 10-3 or greater than or equal to 107, then it is
+ * represented in so-called "computerized scientific notation." Let n be
+ * the unique integer such that 10n <= m < 10n+1; then let a be the
+ * mathematically exact quotient of m and 10n so that 1 <= a < 10.
+ * The magnitude is then represented as the integer part of a, as a
+ * single decimal digit, followed by '.', followed by decimal digits
+ * representing the fractional part of a, followed by the letter 'E',
+ * followed by a representation of n as a decimal integer, as produced
+ * by the method Integer.toString(int).
+ * @param value - The double to convert to a string
+ * @returns the formatted double string.
+ */
+ static std::string toString( double value );
+
+ /**
+ * Returns a Double instance representing the specified double value.
+ * @param value - double to wrap
+ * @returns new Double instance wrapping the primitive value
+ */
+ static Double valueOf( double value );
+
+ /**
+ * Returns a Double instance that wraps a primtive double which is parsed
+ * from the string value passed.
+ * @param value - the string to parse
+ * @returns a new Double instance wrapping the double parsed from value
+ * @throws NumberFormatException on error.
+ */
+ static Double valueOf( const std::string& value )
+ throw ( exceptions::NumberFormatException );
+
+ private:
+
+ static const long long DOUBLE_SIGN_MASK = 0x8000000000000000LL;
+ static const long long DOUBLE_EXPONENT_MASK = 0x7FF0000000000000LL;
+ static const long long DOUBLE_MANTISSA_MASK = 0x000FFFFFFFFFFFFFLL;
+ static const long long DOUBLE_NAN_BITS =
+ DOUBLE_EXPONENT_MASK | 0x0008000000000000LL;
};
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.cpp?view=diff&rev=562561&r1=562560&r2=562561
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.cpp Fri Aug 3 12:41:01 2007
@@ -41,24 +41,22 @@
////////////////////////////////////////////////////////////////////////////////
Float::Float( const std::string& value ) throw( exceptions::NumberFormatException ) {
- //TODO this->value = Float::parseFloat( value );
+ this->value = Float::parseFloat( value );
}
////////////////////////////////////////////////////////////////////////////////
int Float::compareTo( const Float& f ) const {
- // TODO
- return this->value < f.value ? -1 : this->value == f.value ? 0 : 1;
+ return Float::compare( this->value, f.value );
}
////////////////////////////////////////////////////////////////////////////////
int Float::compareTo( const float& f ) const {
- // TODO
- return this->value < f ? -1 : this->value == f ? 0 : 1;
+ return Float::compare( this->value, f );
}
////////////////////////////////////////////////////////////////////////////////
std::string Float::toString() const {
- return ""; //TODO Float::toString( this->value, 10 );
+ return Float::toString( this->value );
}
////////////////////////////////////////////////////////////////////////////////
@@ -72,6 +70,35 @@
}
////////////////////////////////////////////////////////////////////////////////
+int Float::compare( float f1, float f2 ) {
+
+ int i1, i2 = 0;
+ long NaNbits = Float::floatToIntBits( Float::NaN );
+
+ if( ( i1 = Float::floatToIntBits( f1 ) ) == NaNbits ) {
+ if( Float::floatToIntBits( f2 ) == NaNbits ) {
+ return 0;
+ }
+ return 1;
+ }
+
+ if( ( i2 = Float::floatToIntBits( f2 ) ) == NaNbits ) {
+ return -1;
+ }
+
+ if( f1 == f2 ) {
+ if( i1 == i2 ) {
+ return 0;
+ }
+
+ // check for -0
+ return i1 > i2 ? 1 : -1;
+ }
+
+ return f1 > f2 ? 1 : -1;
+}
+
+////////////////////////////////////////////////////////////////////////////////
int Float::floatToIntBits( float value ) {
int intValue = 0;
@@ -94,6 +121,14 @@
int intValue = 0;
memcpy( &intValue, &value, sizeof( float ) );
return intValue;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+float Float::intBitsToFloat( int bits ) {
+
+ float floatValue = 0;
+ memcpy( &floatValue, &bits, sizeof( int ) );
+ return floatValue;
}
////////////////////////////////////////////////////////////////////////////////
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.h?view=diff&rev=562561&r1=562560&r2=562561
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/lang/Float.h Fri Aug 3 12:41:01 2007
@@ -213,6 +213,18 @@
public: // Statics
/**
+ * Compares the two specified double values. The sign of the integer value
+ * returned is the same as that of the integer that would be returned by the
+ * call: Float( f1 ).compareTo( Float( f2) )
+ * @param f1 - the first double to compare
+ * @param f2 - the second double to compare
+ * @returns the value 0 if d1 is numerically equal to f2; a value less than
+ * 0 if f1 is numerically less than f2; and a value greater than 0 if f1 is
+ * numerically greater than f2.
+ */
+ static int compare( float f1, float f2 );
+
+ /**
* Returns a representation of the specified floating-point value according
* to the IEEE 754 floating-point "single format" bit layout.
*
@@ -282,7 +294,7 @@
/**
* @param value - The float to check.
- * @returns true if the float is equal to positive infinity.
+ * @returns true if the float is equal to infinity.
*/
static bool isInfinite( float value );