ooRexx  4.2.0-source
About: ooRexx (Open Object Rexx) is a free implementation of Object Rexx. Object Rexx is an enhancement of the classic Rexx interpreter; a full-featured programming language with a human-oriented syntax.
  Fossies Dox: ooRexx-4.2.0-source.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

Numerics.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.ibm.com/developerworks/oss/CPLv1.0.htm */
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 */
40 /* */
41 /* Utility class to manage the various sorts of numeric conversions required */
42 /* by Rexx. These conversions are all just static methods. */
43 /* */
44 /******************************************************************************/
45 #ifndef Included_Numerics
46 #define Included_Numerics
47 
48 
49 class NumericSettings // "global" numeric settings */
50 {
51  public:
53  size_t digits; /* numeric digits setting */
54  size_t fuzz; /* numeric fuzz setting */
55  bool form; /* numeric form setting */
56 }; /* global activation settings */
57 
58 
59 class Numerics
60 {
61 public:
66  static const size_t DEFAULT_DIGITS;
67  // a digits setting for full range integer conversion
68  static const size_t ARGUMENT_DIGITS;
69  // the digits setting used internally for function/method arguments to allow
70  // for the full range
71  static const size_t SIZE_DIGITS;
72  static const size_t MAX_STRINGSIZE;
73 
74  // max numeric digits value for explicit 64-bit conversions
75  static const size_t DIGITS64;
76  static const bool FORM_SCIENTIFIC;
77  static const bool FORM_ENGINEERING;
78 
79  static const size_t DEFAULT_FUZZ;
80  /* default numeric form setting */
81  static const bool DEFAULT_FORM;
82 
83  static const wholenumber_t validMaxWhole[]; // table of maximum values per digits setting
84 
87  static RexxObject *int64ToObject(int64_t v);
88  static RexxObject *uint64ToObject(uint64_t v);
89  static RexxObject *uintptrToObject(uintptr_t v);
90  static RexxObject *intptrToObject(intptr_t v);
91 
92  static bool objectToWholeNumber(RexxObject *o, wholenumber_t &result, wholenumber_t max, wholenumber_t min);
93  static bool objectToStringSize(RexxObject *o, stringsize_t &result, stringsize_t max);
94  static bool objectToSignedInteger(RexxObject *o, ssize_t &result, ssize_t max, ssize_t min);
95  static bool objectToUnsignedInteger(RexxObject *o, size_t &result, size_t max);
96  static bool objectToInt64(RexxObject *o, int64_t &result);
97  static bool objectToUnsignedInt64(RexxObject *o, uint64_t &result);
98  static bool objectToUintptr(RexxObject *source, uintptr_t &result);
99  static bool objectToIntptr(RexxObject *source, intptr_t &result);
100  static RexxObject *int64Object(RexxObject *source);
101 
102  static size_t formatWholeNumber(wholenumber_t integer, char *dest);
103  static size_t formatStringSize(stringsize_t integer, char *dest);
104  static size_t formatInt64(int64_t integer, char *dest);
105  static size_t formatUnsignedInt64(uint64_t integer, char *dest);
106 
107  static size_t normalizeWholeNumber(wholenumber_t integer, char *dest);
108 
109  static size_t digits() { return settings->digits; }
110  static size_t fuzz() { return settings->fuzz; }
111  static bool form() { return settings->form; }
112  static void setCurrentSettings(NumericSettings *s) { settings = s; }
115  static inline wholenumber_t abs(wholenumber_t n) { return n < 0 ? -n : n; }
116  static inline wholenumber_t minVal(wholenumber_t n1, wholenumber_t n2) { return n2 > n1 ? n1 : n2; }
117  static inline stringsize_t minVal(stringsize_t n1, stringsize_t n2) { return n2 > n1 ? n1 : n2; }
118  static inline wholenumber_t maxVal(wholenumber_t n1, wholenumber_t n2) { return n2 > n1 ? n2 : n1; }
119  static inline stringsize_t maxVal(stringsize_t n1, stringsize_t n2) { return n2 > n1 ? n2 : n1; }
120  static inline wholenumber_t maxValueForDigits(size_t d)
121  {
122  if (d > ARGUMENT_DIGITS)
123  {
124  return validMaxWhole[ARGUMENT_DIGITS - 1];
125  }
126  else
127  {
128  return validMaxWhole[d - 1];
129  }
130  }
131 
132  static inline wholenumber_t multiplierForExponent(size_t e)
133  {
134  return validMaxWhole[e - 1];
135  }
136 
137  static RexxString *pointerToString(void *);
138 
139 
140 protected:
141 
144 };
145 
146 
147 inline size_t number_digits() { return Numerics::digits(); }
148 inline size_t number_fuzz() { return Numerics::fuzz(); }
149 inline bool number_form() { return Numerics::form(); }
150 inline size_t number_fuzzydigits() { return number_digits() - number_fuzz(); }
151 #endif
152 
Numerics::objectToInt64
static bool objectToInt64(RexxObject *o, int64_t &result)
Definition: Numerics.cpp:409
Numerics::int64Object
static RexxObject * int64Object(RexxObject *source)
Definition: Numerics.cpp:447
Numerics::objectToStringSize
static bool objectToStringSize(RexxObject *o, stringsize_t &result, stringsize_t max)
Definition: Numerics.cpp:312
Numerics::defaultSettings
static NumericSettings defaultSettings
Definition: Numerics.hpp:143
Numerics::formatStringSize
static size_t formatStringSize(stringsize_t integer, char *dest)
Definition: Numerics.cpp:698
NumericSettings
Definition: Numerics.hpp:49
Numerics::setDefaultSettings
static NumericSettings * setDefaultSettings()
Definition: Numerics.hpp:113
Numerics::formatWholeNumber
static size_t formatWholeNumber(wholenumber_t integer, char *dest)
Definition: Numerics.cpp:572
Numerics::getDefaultSettings
static NumericSettings * getDefaultSettings()
Definition: Numerics.hpp:114
Numerics::objectToSignedInteger
static bool objectToSignedInteger(RexxObject *o, ssize_t &result, ssize_t max, ssize_t min)
Definition: Numerics.cpp:269
Numerics::maxVal
static wholenumber_t maxVal(wholenumber_t n1, wholenumber_t n2)
Definition: Numerics.hpp:118
Numerics::validMaxWhole
static const wholenumber_t validMaxWhole[]
Definition: Numerics.hpp:83
Numerics::MIN_EXPONENT
static const wholenumber_t MIN_EXPONENT
Definition: Numerics.hpp:65
Numerics::int64ToObject
static RexxObject * int64ToObject(int64_t v)
Definition: Numerics.cpp:140
Numerics::MAX_STRINGSIZE
static const size_t MAX_STRINGSIZE
Definition: Numerics.hpp:72
Numerics::wholenumberToObject
static RexxObject * wholenumberToObject(wholenumber_t v)
Definition: Numerics.cpp:182
Numerics::MAX_WHOLENUMBER
static const wholenumber_t MAX_WHOLENUMBER
Definition: Numerics.hpp:62
Numerics::objectToUnsignedInteger
static bool objectToUnsignedInteger(RexxObject *o, size_t &result, size_t max)
Definition: Numerics.cpp:361
Numerics::FORM_SCIENTIFIC
static const bool FORM_SCIENTIFIC
Definition: Numerics.hpp:76
Numerics::uintptrToObject
static RexxObject * uintptrToObject(uintptr_t v)
Definition: Numerics.cpp:844
Numerics::DIGITS64
static const size_t DIGITS64
Definition: Numerics.hpp:75
Numerics::SIZE_DIGITS
static const size_t SIZE_DIGITS
Definition: Numerics.hpp:71
NumericSettings::NumericSettings
NumericSettings()
Definition: Numerics.cpp:124
Numerics::objectToUintptr
static bool objectToUintptr(RexxObject *source, uintptr_t &result)
Definition: Numerics.cpp:526
Numerics::pointerToString
static RexxString * pointerToString(void *)
Definition: Numerics.cpp:888
Numerics::uint64ToObject
static RexxObject * uint64ToObject(uint64_t v)
Definition: Numerics.cpp:161
Numerics::ARGUMENT_DIGITS
static const size_t ARGUMENT_DIGITS
Definition: Numerics.hpp:68
Numerics::formatUnsignedInt64
static size_t formatUnsignedInt64(uint64_t integer, char *dest)
Definition: Numerics.cpp:804
Numerics::objectToWholeNumber
static bool objectToWholeNumber(RexxObject *o, wholenumber_t &result, wholenumber_t max, wholenumber_t min)
Definition: Numerics.cpp:226
Numerics::minVal
static stringsize_t minVal(stringsize_t n1, stringsize_t n2)
Definition: Numerics.hpp:117
Numerics::digits
static size_t digits()
Definition: Numerics.hpp:109
Numerics::DEFAULT_FORM
static const bool DEFAULT_FORM
Definition: Numerics.hpp:81
Numerics::setCurrentSettings
static void setCurrentSettings(NumericSettings *s)
Definition: Numerics.hpp:112
number_form
bool number_form()
Definition: Numerics.hpp:149
number_fuzz
size_t number_fuzz()
Definition: Numerics.hpp:148
Numerics::formatInt64
static size_t formatInt64(int64_t integer, char *dest)
Definition: Numerics.cpp:739
Numerics::stringsizeToObject
static RexxObject * stringsizeToObject(stringsize_t v)
Definition: Numerics.cpp:203
Numerics::fuzz
static size_t fuzz()
Definition: Numerics.hpp:110
Numerics::DEFAULT_DIGITS
static const size_t DEFAULT_DIGITS
Definition: Numerics.hpp:66
Numerics::FORM_ENGINEERING
static const bool FORM_ENGINEERING
Definition: Numerics.hpp:77
Numerics::maxVal
static stringsize_t maxVal(stringsize_t n1, stringsize_t n2)
Definition: Numerics.hpp:119
NumericSettings::digits
size_t digits
Definition: Numerics.hpp:53
Numerics::minVal
static wholenumber_t minVal(wholenumber_t n1, wholenumber_t n2)
Definition: Numerics.hpp:116
Numerics::intptrToObject
static RexxObject * intptrToObject(intptr_t v)
Definition: Numerics.cpp:866
wholenumber_t
ssize_t wholenumber_t
Definition: rexx.h:229
Numerics::normalizeWholeNumber
static size_t normalizeWholeNumber(wholenumber_t integer, char *dest)
Definition: Numerics.cpp:637
stringsize_t
size_t stringsize_t
Definition: rexx.h:228
number_digits
size_t number_digits()
Definition: Numerics.hpp:147
NumericSettings::form
bool form
Definition: Numerics.hpp:55
Numerics::maxValueForDigits
static wholenumber_t maxValueForDigits(size_t d)
Definition: Numerics.hpp:120
Numerics::DEFAULT_FUZZ
static const size_t DEFAULT_FUZZ
Definition: Numerics.hpp:79
number_fuzzydigits
size_t number_fuzzydigits()
Definition: Numerics.hpp:150
Numerics::objectToUnsignedInt64
static bool objectToUnsignedInt64(RexxObject *o, uint64_t &result)
Definition: Numerics.cpp:483
Numerics::MIN_WHOLENUMBER
static const wholenumber_t MIN_WHOLENUMBER
Definition: Numerics.hpp:63
Numerics::settings
static NumericSettings * settings
Definition: Numerics.hpp:142
Numerics::MAX_EXPONENT
static const wholenumber_t MAX_EXPONENT
Definition: Numerics.hpp:64
Numerics::abs
static wholenumber_t abs(wholenumber_t n)
Definition: Numerics.hpp:115
Numerics::objectToIntptr
static bool objectToIntptr(RexxObject *source, intptr_t &result)
Definition: Numerics.cpp:549
NumericSettings::fuzz
size_t fuzz
Definition: Numerics.hpp:54
Numerics
Definition: Numerics.hpp:59
RexxObject
Definition: ObjectClass.hpp:311
Numerics::form
static bool form()
Definition: Numerics.hpp:111
RexxString
Definition: StringClass.hpp:119
Numerics::multiplierForExponent
static wholenumber_t multiplierForExponent(size_t e)
Definition: Numerics.hpp:132