Library for big numbers from http://www.ttmath.org/

Dependents:   PIDHeater82 Conceptcontroller_v_1_0 AlarmClockApp COG4050_adxl355_tilt ... more

TTMath is a small library which allows one to perform arithmetic operations with big unsigned integer, big signed integer and big floating point numbers. It provides standard mathematical operations like adding, subtracting, multiplying, dividing.

TTMath is BSD Licensed (new/modified BSD)

For more information about ttmath see http://www.ttmath.org/

Committer:
stevep
Date:
Tue Jul 30 18:43:48 2013 +0000
Revision:
0:04a9f72bbca7
v0.9.3 of ttmath

Who changed what in which revision?

UserRevisionLine numberNew contents of line
stevep 0:04a9f72bbca7 1 /*
stevep 0:04a9f72bbca7 2 * This file is a part of TTMath Bignum Library
stevep 0:04a9f72bbca7 3 * and is distributed under the (new) BSD licence.
stevep 0:04a9f72bbca7 4 * Author: Tomasz Sowa <t.sowa@ttmath.org>
stevep 0:04a9f72bbca7 5 */
stevep 0:04a9f72bbca7 6
stevep 0:04a9f72bbca7 7 /*
stevep 0:04a9f72bbca7 8 * Copyright (c) 2006-2010, Tomasz Sowa
stevep 0:04a9f72bbca7 9 * All rights reserved.
stevep 0:04a9f72bbca7 10 *
stevep 0:04a9f72bbca7 11 * Redistribution and use in source and binary forms, with or without
stevep 0:04a9f72bbca7 12 * modification, are permitted provided that the following conditions are met:
stevep 0:04a9f72bbca7 13 *
stevep 0:04a9f72bbca7 14 * * Redistributions of source code must retain the above copyright notice,
stevep 0:04a9f72bbca7 15 * this list of conditions and the following disclaimer.
stevep 0:04a9f72bbca7 16 *
stevep 0:04a9f72bbca7 17 * * Redistributions in binary form must reproduce the above copyright
stevep 0:04a9f72bbca7 18 * notice, this list of conditions and the following disclaimer in the
stevep 0:04a9f72bbca7 19 * documentation and/or other materials provided with the distribution.
stevep 0:04a9f72bbca7 20 *
stevep 0:04a9f72bbca7 21 * * Neither the name Tomasz Sowa nor the names of contributors to this
stevep 0:04a9f72bbca7 22 * project may be used to endorse or promote products derived
stevep 0:04a9f72bbca7 23 * from this software without specific prior written permission.
stevep 0:04a9f72bbca7 24 *
stevep 0:04a9f72bbca7 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
stevep 0:04a9f72bbca7 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
stevep 0:04a9f72bbca7 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
stevep 0:04a9f72bbca7 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
stevep 0:04a9f72bbca7 29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
stevep 0:04a9f72bbca7 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
stevep 0:04a9f72bbca7 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
stevep 0:04a9f72bbca7 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
stevep 0:04a9f72bbca7 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
stevep 0:04a9f72bbca7 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
stevep 0:04a9f72bbca7 35 * THE POSSIBILITY OF SUCH DAMAGE.
stevep 0:04a9f72bbca7 36 */
stevep 0:04a9f72bbca7 37
stevep 0:04a9f72bbca7 38 #ifndef headerfilettmathmisc
stevep 0:04a9f72bbca7 39 #define headerfilettmathmisc
stevep 0:04a9f72bbca7 40
stevep 0:04a9f72bbca7 41
stevep 0:04a9f72bbca7 42 /*!
stevep 0:04a9f72bbca7 43 \file ttmathmisc.h
stevep 0:04a9f72bbca7 44 \brief some helpful functions
stevep 0:04a9f72bbca7 45 */
stevep 0:04a9f72bbca7 46
stevep 0:04a9f72bbca7 47
stevep 0:04a9f72bbca7 48 #include <string>
stevep 0:04a9f72bbca7 49
stevep 0:04a9f72bbca7 50
stevep 0:04a9f72bbca7 51 namespace ttmath
stevep 0:04a9f72bbca7 52 {
stevep 0:04a9f72bbca7 53
stevep 0:04a9f72bbca7 54 /*!
stevep 0:04a9f72bbca7 55 some helpful functions
stevep 0:04a9f72bbca7 56 */
stevep 0:04a9f72bbca7 57 class Misc
stevep 0:04a9f72bbca7 58 {
stevep 0:04a9f72bbca7 59 public:
stevep 0:04a9f72bbca7 60
stevep 0:04a9f72bbca7 61
stevep 0:04a9f72bbca7 62 /*
stevep 0:04a9f72bbca7 63 *
stevep 0:04a9f72bbca7 64 * AssignString(result, str)
stevep 0:04a9f72bbca7 65 * result = str
stevep 0:04a9f72bbca7 66 *
stevep 0:04a9f72bbca7 67 */
stevep 0:04a9f72bbca7 68
stevep 0:04a9f72bbca7 69 /*!
stevep 0:04a9f72bbca7 70 result = str
stevep 0:04a9f72bbca7 71 */
stevep 0:04a9f72bbca7 72 static void AssignString(std::string & result, const char * str)
stevep 0:04a9f72bbca7 73 {
stevep 0:04a9f72bbca7 74 result = str;
stevep 0:04a9f72bbca7 75 }
stevep 0:04a9f72bbca7 76
stevep 0:04a9f72bbca7 77
stevep 0:04a9f72bbca7 78 #ifndef TTMATH_DONT_USE_WCHAR
stevep 0:04a9f72bbca7 79
stevep 0:04a9f72bbca7 80 /*!
stevep 0:04a9f72bbca7 81 result = str
stevep 0:04a9f72bbca7 82 */
stevep 0:04a9f72bbca7 83 static void AssignString(std::wstring & result, const char * str)
stevep 0:04a9f72bbca7 84 {
stevep 0:04a9f72bbca7 85 result.clear();
stevep 0:04a9f72bbca7 86
stevep 0:04a9f72bbca7 87 for( ; *str ; ++str )
stevep 0:04a9f72bbca7 88 result += *str;
stevep 0:04a9f72bbca7 89 }
stevep 0:04a9f72bbca7 90
stevep 0:04a9f72bbca7 91
stevep 0:04a9f72bbca7 92 /*!
stevep 0:04a9f72bbca7 93 result = str
stevep 0:04a9f72bbca7 94 */
stevep 0:04a9f72bbca7 95 static void AssignString(std::wstring & result, const std::string & str)
stevep 0:04a9f72bbca7 96 {
stevep 0:04a9f72bbca7 97 return AssignString(result, str.c_str());
stevep 0:04a9f72bbca7 98 }
stevep 0:04a9f72bbca7 99
stevep 0:04a9f72bbca7 100
stevep 0:04a9f72bbca7 101 /*!
stevep 0:04a9f72bbca7 102 result = str
stevep 0:04a9f72bbca7 103 */
stevep 0:04a9f72bbca7 104 static void AssignString(std::string & result, const wchar_t * str)
stevep 0:04a9f72bbca7 105 {
stevep 0:04a9f72bbca7 106 result.clear();
stevep 0:04a9f72bbca7 107
stevep 0:04a9f72bbca7 108 for( ; *str ; ++str )
stevep 0:04a9f72bbca7 109 result += static_cast<char>(*str);
stevep 0:04a9f72bbca7 110 }
stevep 0:04a9f72bbca7 111
stevep 0:04a9f72bbca7 112
stevep 0:04a9f72bbca7 113 /*!
stevep 0:04a9f72bbca7 114 result = str
stevep 0:04a9f72bbca7 115 */
stevep 0:04a9f72bbca7 116 static void AssignString(std::string & result, const std::wstring & str)
stevep 0:04a9f72bbca7 117 {
stevep 0:04a9f72bbca7 118 return AssignString(result, str.c_str());
stevep 0:04a9f72bbca7 119 }
stevep 0:04a9f72bbca7 120
stevep 0:04a9f72bbca7 121 #endif
stevep 0:04a9f72bbca7 122
stevep 0:04a9f72bbca7 123
stevep 0:04a9f72bbca7 124 /*
stevep 0:04a9f72bbca7 125 *
stevep 0:04a9f72bbca7 126 * AddString(result, str)
stevep 0:04a9f72bbca7 127 * result += str
stevep 0:04a9f72bbca7 128 *
stevep 0:04a9f72bbca7 129 */
stevep 0:04a9f72bbca7 130
stevep 0:04a9f72bbca7 131
stevep 0:04a9f72bbca7 132 /*!
stevep 0:04a9f72bbca7 133 result += str
stevep 0:04a9f72bbca7 134 */
stevep 0:04a9f72bbca7 135 static void AddString(std::string & result, const char * str)
stevep 0:04a9f72bbca7 136 {
stevep 0:04a9f72bbca7 137 result += str;
stevep 0:04a9f72bbca7 138 }
stevep 0:04a9f72bbca7 139
stevep 0:04a9f72bbca7 140
stevep 0:04a9f72bbca7 141 #ifndef TTMATH_DONT_USE_WCHAR
stevep 0:04a9f72bbca7 142
stevep 0:04a9f72bbca7 143 /*!
stevep 0:04a9f72bbca7 144 result += str
stevep 0:04a9f72bbca7 145 */
stevep 0:04a9f72bbca7 146 static void AddString(std::wstring & result, const char * str)
stevep 0:04a9f72bbca7 147 {
stevep 0:04a9f72bbca7 148 for( ; *str ; ++str )
stevep 0:04a9f72bbca7 149 result += *str;
stevep 0:04a9f72bbca7 150 }
stevep 0:04a9f72bbca7 151
stevep 0:04a9f72bbca7 152 #endif
stevep 0:04a9f72bbca7 153
stevep 0:04a9f72bbca7 154
stevep 0:04a9f72bbca7 155 /*
stevep 0:04a9f72bbca7 156 this method omits any white characters from the string
stevep 0:04a9f72bbca7 157 char_type is char or wchar_t
stevep 0:04a9f72bbca7 158 */
stevep 0:04a9f72bbca7 159 template<class char_type>
stevep 0:04a9f72bbca7 160 static void SkipWhiteCharacters(const char_type * & c)
stevep 0:04a9f72bbca7 161 {
stevep 0:04a9f72bbca7 162 // 13 is at the end in a DOS text file (\r\n)
stevep 0:04a9f72bbca7 163 while( (*c==' ' ) || (*c=='\t') || (*c==13 ) || (*c=='\n') )
stevep 0:04a9f72bbca7 164 ++c;
stevep 0:04a9f72bbca7 165 }
stevep 0:04a9f72bbca7 166
stevep 0:04a9f72bbca7 167
stevep 0:04a9f72bbca7 168
stevep 0:04a9f72bbca7 169
stevep 0:04a9f72bbca7 170 /*!
stevep 0:04a9f72bbca7 171 this static method converts one character into its value
stevep 0:04a9f72bbca7 172
stevep 0:04a9f72bbca7 173 for example:
stevep 0:04a9f72bbca7 174 1 -> 1
stevep 0:04a9f72bbca7 175 8 -> 8
stevep 0:04a9f72bbca7 176 A -> 10
stevep 0:04a9f72bbca7 177 f -> 15
stevep 0:04a9f72bbca7 178
stevep 0:04a9f72bbca7 179 this method don't check whether c is correct or not
stevep 0:04a9f72bbca7 180 */
stevep 0:04a9f72bbca7 181 static uint CharToDigit(uint c)
stevep 0:04a9f72bbca7 182 {
stevep 0:04a9f72bbca7 183 if(c>='0' && c<='9')
stevep 0:04a9f72bbca7 184 return c-'0';
stevep 0:04a9f72bbca7 185
stevep 0:04a9f72bbca7 186 if(c>='a' && c<='z')
stevep 0:04a9f72bbca7 187 return c-'a'+10;
stevep 0:04a9f72bbca7 188
stevep 0:04a9f72bbca7 189 return c-'A'+10;
stevep 0:04a9f72bbca7 190 }
stevep 0:04a9f72bbca7 191
stevep 0:04a9f72bbca7 192
stevep 0:04a9f72bbca7 193 /*!
stevep 0:04a9f72bbca7 194 this method changes a character 'c' into its value
stevep 0:04a9f72bbca7 195 (if there can't be a correct value it returns -1)
stevep 0:04a9f72bbca7 196
stevep 0:04a9f72bbca7 197 for example:
stevep 0:04a9f72bbca7 198 c=2, base=10 -> function returns 2
stevep 0:04a9f72bbca7 199 c=A, base=10 -> function returns -1
stevep 0:04a9f72bbca7 200 c=A, base=16 -> function returns 10
stevep 0:04a9f72bbca7 201 */
stevep 0:04a9f72bbca7 202 static sint CharToDigit(uint c, uint base)
stevep 0:04a9f72bbca7 203 {
stevep 0:04a9f72bbca7 204 if( c>='0' && c<='9' )
stevep 0:04a9f72bbca7 205 c=c-'0';
stevep 0:04a9f72bbca7 206 else
stevep 0:04a9f72bbca7 207 if( c>='a' && c<='z' )
stevep 0:04a9f72bbca7 208 c=c-'a'+10;
stevep 0:04a9f72bbca7 209 else
stevep 0:04a9f72bbca7 210 if( c>='A' && c<='Z' )
stevep 0:04a9f72bbca7 211 c=c-'A'+10;
stevep 0:04a9f72bbca7 212 else
stevep 0:04a9f72bbca7 213 return -1;
stevep 0:04a9f72bbca7 214
stevep 0:04a9f72bbca7 215
stevep 0:04a9f72bbca7 216 if( c >= base )
stevep 0:04a9f72bbca7 217 return -1;
stevep 0:04a9f72bbca7 218
stevep 0:04a9f72bbca7 219
stevep 0:04a9f72bbca7 220 return sint(c);
stevep 0:04a9f72bbca7 221 }
stevep 0:04a9f72bbca7 222
stevep 0:04a9f72bbca7 223
stevep 0:04a9f72bbca7 224
stevep 0:04a9f72bbca7 225 /*!
stevep 0:04a9f72bbca7 226 this method converts a digit into a char
stevep 0:04a9f72bbca7 227 digit should be from <0,F>
stevep 0:04a9f72bbca7 228 (we don't have to get a base)
stevep 0:04a9f72bbca7 229
stevep 0:04a9f72bbca7 230 for example:
stevep 0:04a9f72bbca7 231 1 -> 1
stevep 0:04a9f72bbca7 232 8 -> 8
stevep 0:04a9f72bbca7 233 10 -> A
stevep 0:04a9f72bbca7 234 15 -> F
stevep 0:04a9f72bbca7 235 */
stevep 0:04a9f72bbca7 236 static uint DigitToChar(uint digit)
stevep 0:04a9f72bbca7 237 {
stevep 0:04a9f72bbca7 238 if( digit < 10 )
stevep 0:04a9f72bbca7 239 return digit + '0';
stevep 0:04a9f72bbca7 240
stevep 0:04a9f72bbca7 241 return digit - 10 + 'A';
stevep 0:04a9f72bbca7 242 }
stevep 0:04a9f72bbca7 243
stevep 0:04a9f72bbca7 244
stevep 0:04a9f72bbca7 245 }; // struct Misc
stevep 0:04a9f72bbca7 246
stevep 0:04a9f72bbca7 247 }
stevep 0:04a9f72bbca7 248
stevep 0:04a9f72bbca7 249
stevep 0:04a9f72bbca7 250 #endif
stevep 0:04a9f72bbca7 251