You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by pe...@apache.org on 2001/07/24 15:58:12 UTC
cvs commit: xml-xerces/c/src/util XMLDouble.cpp XMLDouble.hpp XMLBigDecimal.cpp XMLBigDecimal.hpp XMLBigInteger.cpp XMLBigInteger.hpp
peiyongz 01/07/24 06:58:12
Modified: c/src/util XMLBigDecimal.cpp XMLBigDecimal.hpp
XMLBigInteger.cpp XMLBigInteger.hpp
Added: c/src/util XMLDouble.cpp XMLDouble.hpp
Log:
XMLDouble and related supporting methods from XMLBigInteger/XMLBigDecimal
Revision Changes Path
1.6 +39 -0 xml-xerces/c/src/util/XMLBigDecimal.cpp
Index: XMLBigDecimal.cpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/util/XMLBigDecimal.cpp,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- XMLBigDecimal.cpp 2001/06/07 20:55:21 1.5
+++ XMLBigDecimal.cpp 2001/07/24 13:58:11 1.6
@@ -56,6 +56,9 @@
/*
* $Log: XMLBigDecimal.cpp,v $
+ * Revision 1.6 2001/07/24 13:58:11 peiyongz
+ * XMLDouble and related supporting methods from XMLBigInteger/XMLBigDecimal
+ *
* Revision 1.5 2001/06/07 20:55:21 tng
* Fix no newline at the end warning. By Pei Yong Zhang.
*
@@ -83,6 +86,7 @@
#include <util/NumberFormatException.hpp>
#include <util/TransService.hpp>
#include <util/Janitor.hpp>
+#include <math.h>
/**
* Constructs a BigDecimal from a string containing an optional (plus | minus)
@@ -122,6 +126,33 @@
fIntVal = new XMLBigInteger(*(toCopy.getValue()));
}
+XMLBigDecimal::XMLBigDecimal(const XMLBigDecimal& toCopy, const int addExponent)
+:fIntVal(0)
+,fScale(toCopy.getScale())
+{
+ //invoke XMLBigInteger' copy ctor
+ fIntVal = new XMLBigInteger(*(toCopy.getValue()));
+
+ if ( addExponent > 0 )
+ {
+ if (fScale >= (unsigned int)addExponent)
+ {
+ fScale -= addExponent;
+ }
+ else
+ {
+ fIntVal->multiply(addExponent - fScale);
+ fScale = 0;
+ }
+
+ }
+ else // addExponent <= 0
+ {
+ fScale += abs(addExponent);
+ }
+
+}
+
/***
*
* Leading and trailing whitespaces are allowed, and trimmed
@@ -327,4 +358,12 @@
// De-allocate the memory allocated by XMLBigInteger
delete[] tmpBuf;
return retBuf;
+}
+
+//
+//
+//
+double XMLBigDecimal::doubleValue() const
+{
+ return (double)(fIntVal->intValue()) * (double) pow(10.0, (double) getScale() * -1);
}
1.5 +5 -1 xml-xerces/c/src/util/XMLBigDecimal.hpp
Index: XMLBigDecimal.hpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/util/XMLBigDecimal.hpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- XMLBigDecimal.hpp 2001/05/18 20:17:56 1.4
+++ XMLBigDecimal.hpp 2001/07/24 13:58:11 1.5
@@ -55,7 +55,7 @@
*/
/*
- * $Id: XMLBigDecimal.hpp,v 1.4 2001/05/18 20:17:56 tng Exp $
+ * $Id: XMLBigDecimal.hpp,v 1.5 2001/07/24 13:58:11 peiyongz Exp $
*/
#ifndef XML_BIGDECIMAL_HPP
@@ -84,6 +84,8 @@
XMLBigDecimal(const XMLBigDecimal& toCopy);
+ XMLBigDecimal(const XMLBigDecimal& toCopy, const int addExponent);
+
static void parseBigDecimal(const XMLCh* const strValue
, XMLCh* const retValue
, unsigned int& scaleValue);
@@ -109,6 +111,8 @@
unsigned int getScale() const;
unsigned int getTotalDigit() const;
+
+ double doubleValue() const;
/**
* Return string representation of the decimal value.
1.6 +13 -0 xml-xerces/c/src/util/XMLBigInteger.cpp
Index: XMLBigInteger.cpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/util/XMLBigInteger.cpp,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- XMLBigInteger.cpp 2001/05/31 17:50:26 1.5
+++ XMLBigInteger.cpp 2001/07/24 13:58:11 1.6
@@ -56,6 +56,9 @@
/*
* $Log: XMLBigInteger.cpp,v $
+ * Revision 1.6 2001/07/24 13:58:11 peiyongz
+ * XMLDouble and related supporting methods from XMLBigInteger/XMLBigDecimal
+ *
* Revision 1.5 2001/05/31 17:50:26 knoaman
* small modification to XMLBigInteger::toString.
*
@@ -343,3 +346,13 @@
return retBuf;
}
+
+//
+//
+//
+int XMLBigInteger::intValue() const
+{
+ unsigned int retVal;
+ XMLString::textToBin(fMagnitude, retVal);
+ return retVal * getSign();
+}
\ No newline at end of file
1.5 +3 -1 xml-xerces/c/src/util/XMLBigInteger.hpp
Index: XMLBigInteger.hpp
===================================================================
RCS file: /home/cvs/xml-xerces/c/src/util/XMLBigInteger.hpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- XMLBigInteger.hpp 2001/05/18 20:17:58 1.4
+++ XMLBigInteger.hpp 2001/07/24 13:58:11 1.5
@@ -55,7 +55,7 @@
*/
/*
- * $Id: XMLBigInteger.hpp,v 1.4 2001/05/18 20:17:58 tng Exp $
+ * $Id: XMLBigInteger.hpp,v 1.5 2001/07/24 13:58:11 peiyongz Exp $
*/
#ifndef XML_BIGINTEGER_HPP
@@ -123,6 +123,8 @@
* the value of this number is negative, zero or positive).
*/
int getSign() const;
+
+ int intValue() const;
private:
1.1 xml-xerces/c/src/util/XMLDouble.cpp
Index: XMLDouble.cpp
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 2001, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/*
* $Id: XMLDouble.cpp,v 1.1 2001/07/24 13:58:11 peiyongz Exp $
* $Log: XMLDouble.cpp,v $
* Revision 1.1 2001/07/24 13:58:11 peiyongz
* XMLDouble and related supporting methods from XMLBigInteger/XMLBigDecimal
*
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#include <util/XMLDouble.hpp>
#include <util/PlatformUtils.hpp>
#include <util/XMLString.hpp>
#include <util/XMLUniDefs.hpp>
#include <util/NumberFormatException.hpp>
#include <util/TransService.hpp>
#include <util/Janitor.hpp>
#include <math.h>
#include <iostream.h>
//
// The special values positive and negative zero,
// positive and negative infinity and not-a-number
// have lexical representations 0, -0, INF, -INF and NaN, respectively
//
const XMLCh XMLDouble::fgNegINFString[] =
{
chDash, chLatin_I, chLatin_N, chLatin_F, chNull
};
const XMLCh XMLDouble::fgNegZeroString[] =
{
chDash, chDigit_0, chNull
};
const XMLCh XMLDouble::fgPosZeroString[] =
{
chDigit_0, chNull
};
const XMLCh XMLDouble::fgPosINFString[] =
{
chLatin_I, chLatin_N, chLatin_F, chNull
};
const XMLCh XMLDouble::fgNaNString[] =
{
chLatin_N, chLatin_a, chLatin_N, chNull
};
XMLCh fgEString[] =
{
chLatin_E, chNull
};
XMLCh fgZeroString[] =
{
chDigit_0, chNull
};
/***
* Algo:
*
* . Check for special cases
* . construct fMantissa, fExponent (optional)
* . No checking on the value space
* .
***/
XMLDouble::XMLDouble(const XMLCh* const strValue)
:fMantissa(0)
,fExponent(0)
,fType(Normal)
,fValue(0)
{
if ((!strValue) || (!*strValue))
ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_emptyString);
// char *p1 = XMLString::transcode(strValue);
XMLCh* tmpStrValue = XMLString::replicate(strValue);
ArrayJanitor<XMLCh> janTmpName(tmpStrValue);
XMLString::trim(tmpStrValue);
// char *p2 = XMLString::transcode(tmpStrValue);
if (XMLString::compareString(tmpStrValue, fgNegINFString) == 0)
{
fType = LiteralType::NegINF;
return;
}
else if (XMLString::compareString(tmpStrValue, fgNegZeroString) == 0)
{
fType = LiteralType::NegZero;
return;
}
else if (XMLString::compareString(tmpStrValue, fgPosZeroString) == 0)
{
fType = LiteralType::PosZero;
return;
}
else if (XMLString::compareString(tmpStrValue, fgPosINFString) == 0)
{
fType = LiteralType::PosINF;
return;
}
else if (XMLString::compareString(tmpStrValue, fgNaNString) == 0)
{
fType = LiteralType::NaN;
return;
}
//
// Normal case
//
int ePos = 0;
int tmpStrLen = XMLString::stringLen(tmpStrValue);
if ((( ePos = XMLString::indexOf(tmpStrValue, chLatin_E)) != -1 ) ||
(( ePos = XMLString::indexOf(tmpStrValue, chLatin_e)) != -1 ) )
{
XMLCh* tmpMantissa = new XMLCh [ePos+1];
XMLString::subString(tmpMantissa, tmpStrValue, 0, ePos);
ArrayJanitor<XMLCh> janMantissa(tmpMantissa);
fMantissa = new XMLBigDecimal(tmpMantissa);
//
// 1234.56E78
//
if ( ePos < tmpStrLen - 1)
{
XMLCh* tmpExponent = new XMLCh [tmpStrLen - ePos];
XMLString::subString(tmpExponent, tmpStrValue, ePos + 1, tmpStrLen);
ArrayJanitor<XMLCh> janExponent(tmpExponent);
fExponent = new XMLBigInteger(tmpExponent);
}
//
// 1234.56E
//
else
{
fExponent = new XMLBigInteger(fgZeroString);
}
}
else
{
fMantissa = new XMLBigDecimal(tmpStrValue);
fExponent = new XMLBigInteger(fgZeroString);
}
fValue = fMantissa->doubleValue() * pow(10.0, fExponent->intValue());
}
XMLDouble::XMLDouble(const XMLDouble& toCopy)
:fMantissa(0)
,fExponent(0)
,fType(Normal)
,fValue(0)
{
fMantissa = new XMLBigDecimal(*(toCopy.fMantissa));
fExponent = new XMLBigInteger(*(toCopy.fExponent));
fType = toCopy.fType;
fValue = toCopy.fValue;
}
//
// Add the 'E' as necessary
// The caller needs to de-allocate the memory allocated by this function
// Deallocate the memory allocated by XMLBigInteger
//
XMLCh* XMLDouble::toString() const
{
switch (fType)
{
case NegINF:
return XMLString::replicate(fgNegINFString);
case NegZero:
return XMLString::replicate(fgNegZeroString);
case PosZero:
return XMLString::replicate(fgPosZeroString);
case PosINF:
return XMLString::replicate(fgPosINFString);
case NaN:
return XMLString::replicate(fgNaNString);
case Normal:
XMLCh *ret_mantissa;
ret_mantissa = fMantissa->toString();
if (fExponent)
{
XMLCh *ret_exponent = fExponent->toString();
XMLCh *ret_val = new XMLCh [ XMLString::stringLen(ret_mantissa) +
XMLString::stringLen(ret_exponent) + 2];
*ret_val = 0;
XMLString::catString(ret_val, ret_mantissa);
XMLString::catString(ret_val, fgEString);
XMLString::catString(ret_val, ret_exponent);
delete [] ret_mantissa;
delete [] ret_exponent;
return ret_val;
}
else
return ret_mantissa;
break;
default:
ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_emptyString);
//internal error
};
return (XMLCh*) 0;
}
//
//
//
int XMLDouble::compareValues(const XMLDouble* const lValue
, const XMLDouble* const rValue)
{
//
// case#1: lValue normal
// rValue normal
//
if ((!lValue->isSpecialValue()) &&
(!rValue->isSpecialValue()) )
{
//
// if we use fValue to compare two
// sequences "12.3456E4" and "1234.56E2",
// they are _NOT_ the same. so we ask
// BigDecimal to compare.
//
XMLBigDecimal ldv = XMLBigDecimal(*(lValue->fMantissa), lValue->fExponent->intValue());
XMLBigDecimal rdv = XMLBigDecimal(*(rValue->fMantissa), rValue->fExponent->intValue());
return XMLBigDecimal::compareValues(&ldv, &rdv);
}
//
// case#2: lValue special
// rValue special
//
else
if ((lValue->isSpecialValue()) &&
(rValue->isSpecialValue()) )
{
if (lValue->fType == rValue->fType)
return 0;
else
return (lValue->fType > rValue->fType ? 1 : -1);
}
//
// case#3: lValue special
// rValue normal
//
else
if ((lValue->isSpecialValue()) &&
(!rValue->isSpecialValue()) )
{
return compareSpecial(lValue, rValue);
}
//
// case#4: lValue normal
// rValue special
//
else
{
return (-1) * compareSpecial(rValue, lValue);
}
return 0;
}
int XMLDouble::compareSpecial(const XMLDouble* const specialValue
, const XMLDouble* const normalValue)
{
switch (specialValue->fType)
{
case NegINF:
return -1;
case NegZero:
case PosZero:
return (normalValue->doubleValue() > 0 ? -1 : 1);
case PosINF:
return 1;
case NaN:
return 1;
default:
ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_emptyString);
//internal error
}
}
1.1 xml-xerces/c/src/util/XMLDouble.hpp
Index: XMLDouble.hpp
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 2001, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/*
* $Id: XMLDouble.hpp,v 1.1 2001/07/24 13:58:11 peiyongz Exp $
* $Log: XMLDouble.hpp,v $
* Revision 1.1 2001/07/24 13:58:11 peiyongz
* XMLDouble and related supporting methods from XMLBigInteger/XMLBigDecimal
*
*/
#ifndef XML_DOUBLE_HPP
#define XML_DOUBLE_HPP
#include <util/XercesDefs.hpp>
#include <util/XMLBigDecimal.hpp>
/***
* 3.2.5.1 Lexical representation
*
* double values have a lexical representation consisting of a mantissa followed,
* optionally, by the character "E" or "e", followed by an exponent.
*
* The exponent �must� be an integer.
* The mantissa must be a decimal number.
* The representations for exponent and mantissa must follow the lexical rules
* for integer and decimal.
*
* If the "E" or "e" and the following exponent are omitted,
* an exponent value of 0 is assumed.
***/
class XMLUTIL_EXPORT XMLDouble
{
public:
/**
* Constructs a newly allocated <code>XMLDouble</code> object that
* represents the value represented by the string.
*
* @param the <code>String</code> to be converted to an
* <code>XMLDouble</code>.
* @exception NumberFormatException if the <code>String</code> does not
* contain a parsable XMLDouble.
*/
enum LiteralType
{
NegINF,
NegZero,
PosZero,
PosINF,
NaN,
SpecialTypeNum = 5,
Normal,
};
XMLDouble(const XMLCh* const strValue);
~XMLDouble();
XMLDouble(const XMLDouble& toCopy);
double doubleValue() const;
/**
* Return string representation of the decimal value.
* A decimal point will be included as necessary,
* the caller of this method is responsible for the
* de-allocation of the memory.
*/
XMLCh* toString() const;
/**
* Compares this object to the specified object.
* The result is <code>true</code> if and only if the argument is not
* <code>null</code> and is an <code>XMLDouble</code> object that contains
* the same <code>int</code> value as this object.
*
* @param obj the object to compare with.
* @return <code>true</code> if the objects are the same;
* <code>false</code> otherwise.
*/
bool operator==(const XMLDouble& toCompare) const;
static int compareValues(const XMLDouble* const lValue
, const XMLDouble* const rValue);
static const XMLCh fgNegINFString[];
static const XMLCh fgNegZeroString[];
static const XMLCh fgPosZeroString[];
static const XMLCh fgPosINFString[];
static const XMLCh fgNaNString[];
private:
bool isSpecialValue() const;
static int compareSpecial(const XMLDouble* const specialValue
, const XMLDouble* const normalValue);
// -----------------------------------------------------------------------
// Private data members
//
// fMantissa
// the XMLBigDecimal holding the value of mantissa.
//
// fExponent
// the XMLBigInteger holding the value of exponent.
//
// -----------------------------------------------------------------------
XMLBigDecimal* fMantissa;
XMLBigInteger* fExponent;
LiteralType fType;
double fValue;
};
inline XMLDouble::~XMLDouble()
{
if (fMantissa)
delete fMantissa;
if (fExponent)
delete fExponent;
}
inline double XMLDouble::doubleValue() const
{
return fValue;
}
inline bool XMLDouble::operator==(const XMLDouble& toCompare) const
{
return ( XMLBigInteger::compareValues(this->fExponent, toCompare.fExponent) == 0 ? true : false);
}
inline bool XMLDouble::isSpecialValue() const
{
return (fType < SpecialTypeNum);
}
#endif
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org