NumberStringClass.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Kernel NumberStringClass.hpp */
40 /* */
41 /* Primitive NumberString Class Definitions */
42 /* */
43 /******************************************************************************/
44 #ifndef Included_RexxNumberString
45 #define Included_RexxNumberString
46 
47 #include "Numerics.hpp"
48 #include "NumberStringMath.hpp"
49 
50 /* Define char data used in OKNUMSTR */
51 #define ch_BLANK ' ' /* Define a Blank character. */
52 #define ch_MINUS '-' /* Define the MINUS character */
53 #define ch_PLUS '+' /* Define the PLUS character. */
54 #define ch_PERIOD '.' /* Define the DOT/PERIOD character. */
55 #define ch_ZERO '0' /* Define the Zero character. */
56 #define ch_ONE '1' /* Define the One character. */
57 #define ch_FIVE '5' /* Define the Five character. */
58 #define ch_NINE '9' /* Define the Nine character. */
59 #define ch_TAB '\t' /* Define the alternate whitespace char */
60 
61 #define NumFormScientific 0x00000001 /* Define Numeric form setting at Object*/
62  /* creation time. */
63 #define NumberRounded 0x00000010 /* Indicate the number was rounded once */
64  /* at NumDigits, avoid double rounding */
65 
66 #define OVERFLOWSPACE 2 /* space for numeric buffer overflow */
67 
68 #define SetNumberStringZero() \
69  this->number[0] = '\0'; /* Make value a zero.*/ \
70  this->length = 1; /* Length is 1 */ \
71  this->sign = 0; /* Make sign Zero. */ \
72  this->exp = 0; /* exponent is zero. */
73 
74 
75 #define NumberStringRound(s,d) s->roundUp(s,d)
76 
78  public:
79  inline RexxNumberStringBase() { ; }
80  void mathRound(char *);
81  char *stripLeadingZeros(char *);
82  char * adjustNumber(char *, char *, size_t, size_t);
83 
84  RexxString *stringObject; /* converted string value */
85  short NumFlags; /* Flags for use by the Numberstring met*/
86  short sign; /* sign for this number (-1 is neg) */
87  size_t NumDigits; /* Maintain a copy of digits setting of */
88  /* From when object was created */
90  size_t length;
91  };
92 
94  public:
95  void *operator new(size_t, size_t);
96  inline void *operator new(size_t size, void *ptr) {return ptr;}
97  inline void operator delete(void *) { ; }
98  inline void operator delete(void *, size_t) { }
99  inline void operator delete(void *, void *) { }
100 
101 
102  RexxNumberString(size_t) ;
103  RexxNumberString(size_t, size_t) ;
104  inline RexxNumberString(RESTORETYPE restoreType) { ; };
105  virtual HashCode getHashValue();
106  void live(size_t);
107  void liveGeneral(int reason);
108  void flatten(RexxEnvelope *);
109 
110  bool numberValue(wholenumber_t &result, size_t precision);
111  bool numberValue(wholenumber_t &result);
112  bool unsignedNumberValue(uwholenumber_t &result, size_t precision);
113  bool unsignedNumberValue(uwholenumber_t &result);
114  bool doubleValue(double &result);
115  inline RexxNumberString *numberString() { return this; }
116  RexxInteger *integerValue(size_t);
118  RexxArray *makeArray();
122  bool truthValue(int);
123  virtual bool logicalValue(logical_t &);
124 
125  bool isEqual(RexxObject *);
127 
129  wholenumber_t comp(RexxObject *, RexxString *alternativeOperator=OREF_NULL, RexxInteger **alternativeOperatorResultPtr=NULL);
142  RexxObject *hashCode();
143 
145  void setString(RexxString *);
146  void roundUp(int);
148  RexxString *formatInternal(size_t, size_t, size_t, size_t, RexxNumberString *, size_t, bool);
156  RexxObject *truncInternal(size_t);
157  RexxObject *floor();
159  RexxObject *ceiling();
161  RexxObject *round();
164  bool isInstanceOf(RexxClass *);
168  inline RexxNumberString *checkNumber(size_t digits)
169  {
170  if (this->length > digits) // is the length larger than digits()?
171  {
172  // need to allocate a new number, but
173  // we chop to digits + 1
174  return this->prepareOperatorNumber(digits + 1, digits, NOROUND);
175  }
176  return this; // no adjustment required
177  }
178 
179  RexxNumberString *prepareNumber(size_t, bool);
180  RexxNumberString *prepareOperatorNumber(size_t, size_t, bool);
181  void adjustPrecision(char *, size_t);
182  void adjustPrecision();
183  inline void checkPrecision() { if (length > NumDigits) adjustPrecision(); }
184  inline void setNumericSettings(size_t digits, bool form)
185  {
186  this->NumDigits = digits;
187  if (form == Numerics::FORM_SCIENTIFIC)
188  this->NumFlags |= NumFormScientific;
189  else
190  this->NumFlags &= ~NumFormScientific;
191  }
192 
193  inline void setupNumber()
194  {
195  /* inherit the current numeric settings */
197  /* check for any required rounding */
198  checkPrecision();
199  }
200  bool createUnsignedValue(const char *thisnum, stringsize_t intlength, int carry, wholenumber_t exponent, uwholenumber_t maxValue, uwholenumber_t &result);
201  bool createUnsignedInt64Value(const char *thisnum, stringsize_t intlength, int carry, wholenumber_t exponent, uint64_t maxValue, uint64_t &result);
202  bool checkIntegerDigits(stringsize_t numDigits, stringsize_t &numberLength, wholenumber_t &numberExponent, bool &carry);
203  bool int64Value(int64_t *result, stringsize_t numDigits);
204  bool unsignedInt64Value(uint64_t *result, stringsize_t numDigits);
205  void formatInt64(int64_t integer);
206  void formatUnsignedInt64(uint64_t integer);
207 
208  RexxNumberString *addSub(RexxNumberString *, unsigned int, size_t);
217  RexxNumberString *Division(RexxNumberString *, unsigned int);
219  RexxInteger *Sign();
220  RexxObject *notOp();
221  RexxNumberString *Max(RexxObject **, size_t, size_t);
222  RexxNumberString *Min(RexxObject **, size_t, size_t);
223  RexxNumberString *maxMin(RexxObject **, size_t, size_t, unsigned int);
227  RexxString *d2xD2c(RexxObject *, bool);
234  void formatUnsignedNumber(size_t);
235  int format(const char *, size_t);
236  inline void setZero() {
237  this->number[0] = '\0'; /* Make value a zero.*/
238  this->length = 1; /* Length is 1 */
239  this->sign = 0; /* Make sign Zero. */
240  this->exp = 0; /* exponent is zero. */
241  }
242 
244 
245  static RexxNumberString *newInstanceFromDouble(double);
246  static RexxNumberString *newInstanceFromDouble(double, size_t);
247  static RexxNumberString *newInstanceFromFloat(float);
252  static RexxNumberString *newInstance(const char *, stringsize_t);
253 
254 
255  static void createInstance();
257 
258  static size_t highBits(size_t);
259  static void subtractNumbers( RexxNumberString *larger, const char *largerPtr, wholenumber_t aLargerExp,
260  RexxNumberString *smaller, const char *smallerPtr, wholenumber_t aSmallerExp,
261  RexxNumberString *result, char **resultPtr);
262  static char *addMultiplier( char *, size_t, char *, int);
263  static char *subtractDivisor(char *data1, size_t length1, char *data2, size_t length2, char *result, int Mult);
264  static char *multiplyPower(char *leftPtr, RexxNumberStringBase *left, char *rightPtr, RexxNumberStringBase *right, char *OutPtr, size_t OutLen, size_t NumberDigits);
265  static char *dividePower(char *AccumPtr, RexxNumberStringBase *Accum, char *Output, size_t NumberDigits);
266  static char *addToBaseSixteen(int, char *, char *);
267  static char *addToBaseTen(int, char *, char *);
268  static char *multiplyBaseSixteen(char *, char *);
269  static char *multiplyBaseTen(char *, char *);
270 
271  char number[4];
272 };
273 
274 void AdjustPrecision(RexxNumberString *, char *, int);
275 
277 {
278  return RexxNumberString::newInstance(s, l);
279 }
280 
282 {
284 }
285 
286 inline RexxNumberString *new_numberstringFromStringsize(stringsize_t n) // no need of distinction between 'size in byte' and 'size in char'
287 {
289 }
290 
292 {
294 }
295 
297 {
299 }
300 
302 {
304 }
305 
306 inline RexxNumberString *new_numberstringFromDouble(double n, size_t p)
307 {
309 }
310 
312 {
314 }
315 
316 #endif
RexxNumberString * new_numberstring(const char *s, stringsize_t l)
RexxNumberString * new_numberstringFromWholenumber(wholenumber_t n)
RexxNumberString * new_numberstringFromUint64(uint64_t n)
RexxNumberString * new_numberstringFromDouble(double n)
RexxNumberString * new_numberstringFromInt64(int64_t n)
void AdjustPrecision(RexxNumberString *, char *, int)
RexxNumberString * new_numberstringFromStringsize(stringsize_t n)
#define NumFormScientific
RexxNumberString * new_numberstringFromFloat(float n)
#define NOROUND
bool number_form()
Definition: Numerics.hpp:157
size_t number_digits()
Definition: Numerics.hpp:155
RESTORETYPE
Definition: ObjectClass.hpp:80
RexxObject *(RexxObject::* PCPPM)()
size_t HashCode
Definition: ObjectClass.hpp:77
#define OREF_NULL
Definition: RexxCore.h:60
static const bool FORM_SCIENTIFIC
Definition: Numerics.hpp:76
char * stripLeadingZeros(char *)
char * adjustNumber(char *, char *, size_t, size_t)
void formatUnsignedInt64(uint64_t integer)
RexxInteger * strictGreaterThan(RexxObject *)
static char * addToBaseTen(int, char *, char *)
wholenumber_t comp(RexxObject *, RexxString *alternativeOperator=OREF_NULL, RexxInteger **alternativeOperatorResultPtr=NULL)
static RexxNumberString * newInstanceFromFloat(float)
RexxObject * xorOp(RexxObject *)
RexxNumberString * checkNumber(size_t digits)
RexxInteger * isLessThan(RexxObject *)
void formatUnsignedNumber(size_t)
RexxInteger * strictEqual(RexxObject *)
void flatten(RexxEnvelope *)
static char * dividePower(char *AccumPtr, RexxNumberStringBase *Accum, char *Output, size_t NumberDigits)
RexxObject * truncInternal(size_t)
static RexxNumberString * newInstance(const char *, stringsize_t)
RexxNumberString * prepareNumber(size_t, bool)
static char * addMultiplier(char *, size_t, char *, int)
RexxInteger * strictLessThan(RexxObject *)
RexxInteger * isGreaterThan(RexxObject *)
bool createUnsignedInt64Value(const char *thisnum, stringsize_t intlength, int carry, wholenumber_t exponent, uint64_t maxValue, uint64_t &result)
static RexxNumberString * newInstanceFromDouble(double)
bool createUnsignedValue(const char *thisnum, stringsize_t intlength, int carry, wholenumber_t exponent, uwholenumber_t maxValue, uwholenumber_t &result)
RexxString * concatBlank(RexxObject *)
RexxString * makeString()
RexxNumberString * multiply(RexxObject *)
RexxObject * roundInternal()
RexxObject * getRealValue(RexxActivation *)
RexxNumberString * minus(RexxObject *)
RexxString * primitiveMakeString()
RexxNumberString * Max(RexxObject **, size_t, size_t)
void formatNumber(wholenumber_t)
RexxObject * floorInternal()
bool int64Value(int64_t *result, stringsize_t numDigits)
void liveGeneral(int reason)
bool unsignedNumberValue(uwholenumber_t &result, size_t precision)
static void createInstance()
static RexxNumberString * newInstanceFromUint64(uint64_t)
RexxString * stringValue()
RexxString * formatRexx(RexxObject *, RexxObject *, RexxObject *, RexxObject *)
RexxObject * ceilingInternal()
bool unsignedInt64Value(uint64_t *result, stringsize_t numDigits)
RexxInteger * hasMethod(RexxString *)
RexxObject * evaluate(RexxActivation *, RexxExpressionStack *)
RexxInteger * integerValue(size_t)
bool checkIntegerDigits(stringsize_t numDigits, stringsize_t &numberLength, wholenumber_t &numberExponent, bool &carry)
RexxClass * classObject()
static char * multiplyBaseSixteen(char *, char *)
wholenumber_t strictComp(RexxObject *)
RexxInteger * Sign()
static RexxClass * classInstance
RexxNumberString * Min(RexxObject **, size_t, size_t)
RexxString * d2xD2c(RexxObject *, bool)
RexxNumberString * numberString()
RexxNumberString * Division(RexxNumberString *, unsigned int)
void formatInt64(int64_t integer)
RexxNumberString * abs()
void setString(RexxString *)
static char * multiplyPower(char *leftPtr, RexxNumberStringBase *left, char *rightPtr, RexxNumberStringBase *right, char *OutPtr, size_t OutLen, size_t NumberDigits)
RexxNumberString * clone()
RexxString * d2c(RexxObject *)
RexxInteger * strictLessOrEqual(RexxObject *)
RexxMethod * instanceMethod(RexxString *)
RexxObject * ceiling()
RexxObject * hashCode()
RexxObject * andOp(RexxObject *)
RexxNumberString * integerDivide(RexxObject *)
static RexxNumberString * newInstanceFromStringsize(stringsize_t)
RexxString * concat(RexxObject *)
RexxObject * getValue(RexxActivation *context)
RexxNumberString(RESTORETYPE restoreType)
RexxObject * orOp(RexxObject *)
RexxNumberString * Multiply(RexxNumberString *)
RexxArray * makeArray()
RexxNumberString * maxMin(RexxObject **, size_t, size_t, unsigned int)
RexxObject * unknown(RexxString *, RexxArray *, RexxDirectory *)
bool numberValue(wholenumber_t &result, size_t precision)
RexxObject * trunc(RexxObject *)
RexxInteger * strictNotEqual(RexxObject *)
static PCPPM operatorMethods[]
bool isInstanceOf(RexxClass *)
RexxNumberString * power(RexxObject *)
RexxNumberString * remainder(RexxObject *)
RexxObject * isInteger()
RexxInteger * strictGreaterOrEqual(RexxObject *)
static char * subtractDivisor(char *data1, size_t length1, char *data2, size_t length2, char *result, int Mult)
virtual HashCode getHashValue()
RexxNumberString * prepareOperatorNumber(size_t, size_t, bool)
static RexxNumberString * newInstanceFromWholenumber(wholenumber_t)
RexxInteger * equal(RexxObject *)
RexxInteger * isGreaterOrEqual(RexxObject *)
RexxString * d2x(RexxObject *)
virtual wholenumber_t compareTo(RexxObject *)
bool isEqual(RexxObject *)
int format(const char *, size_t)
RexxInteger * isLessOrEqual(RexxObject *)
static void subtractNumbers(RexxNumberString *larger, const char *largerPtr, wholenumber_t aLargerExp, RexxNumberString *smaller, const char *smallerPtr, wholenumber_t aSmallerExp, RexxNumberString *result, char **resultPtr)
RexxObject * operatorNot(RexxObject *)
void setNumericSettings(size_t digits, bool form)
virtual bool logicalValue(logical_t &)
static size_t highBits(size_t)
static RexxNumberString * newInstanceFromInt64(int64_t)
static char * addToBaseSixteen(int, char *, char *)
RexxNumberString * plus(RexxObject *)
RexxNumberString * divide(RexxObject *)
RexxString * formatInternal(size_t, size_t, size_t, size_t, RexxNumberString *, size_t, bool)
RexxSupplier * instanceMethods(RexxClass *)
bool doubleValue(double &result)
static char * multiplyBaseTen(char *, char *)
RexxInteger * notEqual(RexxObject *)
RexxNumberString * addSub(RexxNumberString *, unsigned int, size_t)
size_t logical_t
Definition: rexx.h:231
ssize_t wholenumber_t
Definition: rexx.h:230
size_t stringsize_t
Definition: rexx.h:228
size_t uwholenumber_t
Definition: rexx.h:229
signed __int64 int64_t
unsigned __int64 uint64_t