forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmathlib.cpp
More file actions
598 lines (530 loc) · 17.3 KB
/
mathlib.cpp
File metadata and controls
598 lines (530 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mathlib.h"
#include "errorlogger.h"
#include <string>
#include <sstream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <limits>
MathLib::bigint MathLib::toLongNumber(const std::string &str)
{
// hexadecimal numbers:
if (isHex(str)) {
if (str[0] == '-') {
bigint ret = 0;
std::istringstream istr(str);
istr >> std::hex >> ret;
return ret;
} else {
unsigned long long ret = 0;
std::istringstream istr(str);
istr >> std::hex >> ret;
return (bigint)ret;
}
}
// octal numbers:
if (isOct(str)) {
bigint ret = 0;
std::istringstream istr(str);
istr >> std::oct >> ret;
return ret;
}
// binary numbers:
if (isBin(str)) {
bigint ret = 0;
for (std::string::size_type i = str[0] == '0'?2:3; i < str.length(); i++) {
ret <<= 1;
if (str[i] == '1')
ret |= 1;
}
if (str[0] == '-')
ret = -ret;
return ret;
}
if (str.find_first_of("eE") != std::string::npos)
return static_cast<bigint>(std::atof(str.c_str()));
bigint ret = 0;
std::istringstream istr(str);
istr >> ret;
return ret;
}
double MathLib::toDoubleNumber(const std::string &str)
{
if (isHex(str))
return static_cast<double>(toLongNumber(str));
// nullcheck
else if (isNullValue(str))
return 0.0;
// otherwise, convert to double
std::istringstream istr(str);
double ret;
istr >> ret;
return ret;
}
template<> std::string MathLib::toString(double value)
{
std::ostringstream result;
result.precision(12);
result << value;
if (result.str() == "-0")
return "0.0";
if (result.str().find(".") == std::string::npos)
return result.str() + ".0";
return result.str();
}
bool MathLib::isFloat(const std::string &s)
{
// every number that contains a . is a float
if (s.find("." , 0) != std::string::npos)
return true;
// scientific notation
return (s.find("E-", 0) != std::string::npos
|| s.find("e-", 0) != std::string::npos);
}
bool MathLib::isNegative(const std::string &s)
{
// remember position
std::string::size_type n = 0;
// eat up whitespace
while (std::isspace(s[n])) ++n;
// every negative number has a negative sign
return (s[n] == '-');
}
bool MathLib::isPositive(const std::string &s)
{
return !MathLib::isNegative(s);
}
/*! \brief Does the string represent an octal number?
* In case leading or trailing white space is provided, the function
* returns false.
* Additional information can be found here:
* http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html
*
* \param[in] s The string to check. In case the string is empty, the function returns false.
* \return Return true in case a octal number is provided and false otherwise.
**/
bool MathLib::isOct(const std::string& s)
{
enum {START, PLUSMINUS, OCTAL_PREFIX, DIGITS} state = START;
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
switch (state) {
case START:
if (*it == '+' || *it == '-')
state = PLUSMINUS;
else if (*it == '0')
state = OCTAL_PREFIX;
else
return false;
break;
case PLUSMINUS:
if (*it == '0')
state = OCTAL_PREFIX;
else
return false;
break;
case OCTAL_PREFIX:
if (isOctalDigit(*it))
state = DIGITS;
else
return false;
break;
case DIGITS:
if (isOctalDigit(*it))
state = DIGITS;
else
return isValidSuffix(it,s.end());
break;
default:
return false;
}
}
return state == DIGITS;
}
bool MathLib::isHex(const std::string& s)
{
// return false, in case an empty string is provided
if (s.empty())
return false;
enum {START, PLUSMINUS, HEX_PREFIX, DIGIT, DIGITS} state = START;
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
switch (state) {
case START:
if (*it == '+' || *it == '-')
state = PLUSMINUS;
else if (*it == '0')
state = HEX_PREFIX;
else
return false;
break;
case PLUSMINUS:
if (*it == '0')
state = HEX_PREFIX;
else
return false;
break;
case HEX_PREFIX:
if (*it == 'x' || *it == 'X')
state = DIGIT;
else
return false;
break;
case DIGIT:
if (isxdigit(*it))
state = DIGITS;
else
return false;
break;
case DIGITS:
if (isxdigit(*it))
state = DIGITS;
else
return isValidSuffix(it,s.end());
break;
default:
return false;
}
}
return state == DIGITS;
}
bool MathLib::isValidSuffix(std::string::const_iterator it, std::string::const_iterator end)
{
enum {START, SUFFIX_U, SUFFIX_UL, SUFFIX_ULL, SUFFIX_L, SUFFIX_LU, SUFFIX_LL, SUFFIX_LLU} state = START;
for (; it != end; ++it) {
switch (state) {
case START:
if (*it == 'u' || *it == 'U')
state = SUFFIX_U;
else if (*it == 'l' || *it == 'L')
state = SUFFIX_L;
else
return false;
break;
case SUFFIX_U:
if (*it == 'l' || *it == 'L')
state = SUFFIX_UL; // UL
else
return false;
break;
case SUFFIX_UL:
if (*it == 'l' || *it == 'L')
state = SUFFIX_ULL; // ULL
else
return false;
break;
case SUFFIX_L:
if (*it == 'u' || *it == 'U')
state = SUFFIX_LU; // LU
else if (*it == 'l' || *it == 'L')
state = SUFFIX_LL; // LL
else
return false;
break;
case SUFFIX_LU:
return false;
break;
case SUFFIX_LL:
if (*it == 'u' || *it == 'U')
state = SUFFIX_LLU; // LLU
else
return false;
break;
default:
return false;
}
}
return (state == SUFFIX_U) || (state == SUFFIX_L)
|| (state == SUFFIX_UL) || (state == SUFFIX_LU) || (state == SUFFIX_LL)
|| (state == SUFFIX_ULL) || (state == SUFFIX_LLU);
}
/*! \brief Does the string represent a binary number?
* In case leading or trailing white space is provided, the function
* returns false.
* Additional information can be found here:
* http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html
*
* \param[in] s The string to check. In case the string is empty, the function returns false.
* \return Return true in case a binary number is provided and false otherwise.
**/
bool MathLib::isBin(const std::string& s)
{
enum {START, PLUSMINUS, GNU_BIN_PREFIX, DIGIT, DIGITS} state = START;
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it) {
switch (state) {
case START:
if (*it == '+' || *it == '-')
state = PLUSMINUS;
else if (*it == '0')
state = GNU_BIN_PREFIX;
else
return false;
break;
case PLUSMINUS:
if (*it == '0')
state = GNU_BIN_PREFIX;
else
return false;
break;
case GNU_BIN_PREFIX:
if (*it == 'b' || *it == 'B')
state = DIGIT;
else
return false;
break;
case DIGIT:
if (*it == '0' || *it == '1')
state = DIGITS;
else
return false;
break;
case DIGITS:
if (*it == '0' || *it == '1')
state = DIGITS;
else
return isValidSuffix(it,s.end());
break;
default:
return false;
}
}
return state == DIGITS;
}
bool MathLib::isInt(const std::string & s)
{
// perform prechecks:
// ------------------
// first check, if a point is found, it is an floating point value
if (s.find(".", 0) != std::string::npos) return false;
// check for scientific notation e.g. NumberE-Number this is obvious an floating point value
else if (s.find("E-", 0) != std::string::npos || s.find("e-", 0) != std::string::npos) return false;
// prechecking has nothing found,...
// gather information
enum Representation {
eScientific = 0, // NumberE+Number or NumberENumber
eOctal, // starts with 0
eHex, // starts with 0x
eDefault // Numbers with a (possible) trailing u or U or l or L for unsigned or long datatypes
};
// create an instance
Representation Mode = eDefault;
// remember position
unsigned long n = 0;
// eat up whitespace
while (std::isspace(s[n])) ++n;
// determine type
if (s.find("E", 0) != std::string::npos) {
Mode = eScientific;
} else if (isHex(s)) {
Mode = eHex;
} else if (isOct(s)) {
Mode = eOctal;
}
// check sign
if (s[n] == '-' || s[n] == '+') ++n;
// check scientific notation
if (Mode == eScientific) {
// check digits
while (std::isdigit(s[n])) ++n;
// check scientific notation
if (std::tolower(s[n]) == 'e') {
++n;
// check positive exponent
if (s[n] == '+') ++n;
// floating pointer number e.g. 124E-2
if (s[n] == '-') return false;
// check digits of the exponent
while (std::isdigit(s[n])) ++n;
}
}
// check hex notation
else if (Mode == eHex) {
++n; // 0
++n; // x
while (std::isxdigit(s[n]))
++n;
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
}
// check octal notation
else if (Mode == eOctal) {
++n; // 0
while (isOctalDigit(s[n]))
++n;
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
} else if (Mode == eDefault) {
// starts with digit
bool bStartsWithDigit=false;
while (std::isdigit(s[n])) {
bStartsWithDigit=true;
++n;
};
while (std::tolower(s[n]) == 'u' || std::tolower(s[n]) == 'l') ++n; // unsigned or long (long)
if (!bStartsWithDigit)
return false;
}
// eat up whitespace
while (std::isspace(s[n]))
++n;
// if everything goes good, we are at the end of the string and no digits/character
// is here --> return true, but if something was found e.g. 12E+12AA return false
return (n >= s.length());
}
std::string MathLib::add(const std::string & first, const std::string & second)
{
if (MathLib::isInt(first) && MathLib::isInt(second)) {
return toString(toLongNumber(first) + toLongNumber(second));
}
double d1 = toDoubleNumber(first);
double d2 = toDoubleNumber(second);
int count = 0;
while (d1 > 100000.0 * d2 && toString(d1+d2)==first && ++count<5)
d2 *= 10.0;
while (d2 > 100000.0 * d1 && toString(d1+d2)==second && ++count<5)
d1 *= 10.0;
return toString(d1 + d2);
}
std::string MathLib::subtract(const std::string &first, const std::string &second)
{
if (MathLib::isInt(first) && MathLib::isInt(second)) {
return toString(toLongNumber(first) - toLongNumber(second));
}
if (first == second)
return "0.0" ;
double d1 = toDoubleNumber(first);
double d2 = toDoubleNumber(second);
int count = 0;
while (d1 > 100000.0 * d2 && toString(d1-d2)==first && ++count<5)
d2 *= 10.0;
while (d2 > 100000.0 * d1 && toString(d1-d2)==second && ++count<5)
d1 *= 10.0;
return toString(d1 - d2);
}
std::string MathLib::divide(const std::string &first, const std::string &second)
{
if (MathLib::isInt(first) && MathLib::isInt(second)) {
const bigint a = toLongNumber(first);
const bigint b = toLongNumber(second);
if (a == std::numeric_limits<bigint>::min())
throw InternalError(0, "Internal Error: Division overflow");
if (b == 0)
throw InternalError(0, "Internal Error: Division by zero");
return toString(toLongNumber(first) / b);
} else if (second == "0.0") {
if (first=="0.0" || first=="+0.0")
return "nan.0";
if (first=="-0.0")
return "-nan.0";
return (first[0] == '-') ? "-inf.0" : "inf.0";
}
return toString(toDoubleNumber(first) / toDoubleNumber(second));
}
std::string MathLib::multiply(const std::string &first, const std::string &second)
{
if (MathLib::isInt(first) && MathLib::isInt(second)) {
return toString(toLongNumber(first) * toLongNumber(second));
}
return toString(toDoubleNumber(first) * toDoubleNumber(second));
}
std::string MathLib::mod(const std::string &first, const std::string &second)
{
if (MathLib::isInt(first) && MathLib::isInt(second)) {
bigint b = toLongNumber(second);
if (b == 0)
throw InternalError(0, "Internal Error: Division by zero");
return toString(toLongNumber(first) % b);
}
return toString(std::fmod(toDoubleNumber(first),toDoubleNumber(second)));
}
std::string MathLib::calculate(const std::string &first, const std::string &second, char action)
{
switch (action) {
case '+':
return MathLib::add(first, second);
case '-':
return MathLib::subtract(first, second);
case '*':
return MathLib::multiply(first, second);
case '/':
return MathLib::divide(first, second);
case '%':
return MathLib::mod(first, second);
case '&':
return MathLib::toString(MathLib::toLongNumber(first) & MathLib::toLongNumber(second));
case '|':
return MathLib::toString(MathLib::toLongNumber(first) | MathLib::toLongNumber(second));
case '^':
return MathLib::toString(MathLib::toLongNumber(first) ^ MathLib::toLongNumber(second));
default:
throw InternalError(0, std::string("Unexpected action '") + action + "' in MathLib::calculate(). Please report this to Cppcheck developers.");
}
}
std::string MathLib::sin(const std::string &tok)
{
return toString(std::sin(toDoubleNumber(tok)));
}
std::string MathLib::cos(const std::string &tok)
{
return toString(std::cos(toDoubleNumber(tok)));
}
std::string MathLib::tan(const std::string &tok)
{
return toString(std::tan(toDoubleNumber(tok)));
}
std::string MathLib::abs(const std::string &tok)
{
return toString(std::abs(toDoubleNumber(tok)));
}
bool MathLib::isEqual(const std::string &first, const std::string &second)
{
// this conversion is needed for formatting
// e.g. if first=0.1 and second=1.0E-1, the direct comparison of the strings would fail
return toString(toDoubleNumber(first)) == toString(toDoubleNumber(second));
}
bool MathLib::isNotEqual(const std::string &first, const std::string &second)
{
return !isEqual(first, second);
}
bool MathLib::isGreater(const std::string &first, const std::string &second)
{
return toDoubleNumber(first) > toDoubleNumber(second);
}
bool MathLib::isGreaterEqual(const std::string &first, const std::string &second)
{
return toDoubleNumber(first) >= toDoubleNumber(second);
}
bool MathLib::isLess(const std::string &first, const std::string &second)
{
return toDoubleNumber(first) < toDoubleNumber(second);
}
bool MathLib::isLessEqual(const std::string &first, const std::string &second)
{
return toDoubleNumber(first) <= toDoubleNumber(second);
}
bool MathLib::isNullValue(const std::string &str)
{
return (str == "-0" || str == "0" || str == "+0"
|| str == "-0.0" || str == "0.0" || str == "+0.0"
|| str == "-0." || str == "+0."
|| str == "-0E-00" || str == "-0E+00" || str == "+0E+00" || str == "+0E-00"
|| str == "-0e-00" || str == "-0e+00" || str == "+0e+00" || str == "+0e-00"
|| str == "-0E-0");
}
bool MathLib::isOctalDigit(char c)
{
return (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7');
}