-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarty_bcd_decimal.h
689 lines (454 loc) · 25.9 KB
/
marty_bcd_decimal.h
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
#pragma once
#include <string>
#include <cstdint>
#include <cmath>
#include <utility>
#include <limits>
#include <iostream>
#include <exception>
#include <stdexcept>
#include <cstdio>
#define MARTY_DECIMAL_H__079F0131_B01B_44ED_BF0F_8DFECAB67FD7__
#ifndef MARTY_DECIMAL_DEFAULT_DIVISION_PRECISION
#define MARTY_DECIMAL_DEFAULT_DIVISION_PRECISION 18
#endif
#include "marty_bcd.h"
#include "marty_relops_impl.h"
#if defined(QSTRING_H)
#define MARTY_DECIMAL_QT_USED
#endif
#if !defined(MARTY_DECIMAL_DISABLE_MODERN_CPP_SUPPORT)
#include <functional>
#endif
//----------------------------------------------------------------------------
namespace marty
{
class Decimal
{
public:
//------------------------------
enum class RoundingMethod
{
// https://en.wikipedia.org/wiki/Rounding
roundingInvalid = -1, // (RoundingMethod)
roundingNone = 0,
// Directed rounding to an integer methods
roundDown = 1, // roundFloor, roundTowardNegInf
roundFloor = roundDown ,
roundTowardNegInf = roundDown , // round towards negative infinity
roundUp , // roundCeil, roundTowardsPosInf
roundCeil = roundUp ,
roundTowardsPosInf = roundUp , // round towards positive infinity
roundTowardsZero , // roundAwayFromInf, roundTrunc
roundAwayFromInf = roundTowardsZero ,
roundTrunc = roundTowardsZero ,
roundTowardsInf , // roundAwayFromZero
roundAwayFromZero = roundTowardsInf ,
// Rounding to the nearest integer
roundHalfUp , // roundHalfTowardsPositiveInf
roundHalfTowardsPositiveInf = roundHalfUp ,
roundHalfDown , // roundHalfTowardsNegativeInf
roundHalfTowardsNegativeInf = roundHalfDown ,
roundHalfTowardsZero , // roundHalfAwayFromInf
roundHalfAwayFromInf = roundHalfTowardsZero ,
roundHalfTowardsInf , // roundHalfAwayFromZero, roundMath
roundHalfAwayFromZero = roundHalfTowardsInf ,
roundMath = roundHalfTowardsInf ,
roundHalfToEven , // roundBankers, roundBanking, roundConvergent, roundStatistician, roundStatistic, roundDutch, roundGaussian
roundBankers = roundHalfToEven ,
roundBanking = roundHalfToEven ,
roundConvergent = roundHalfToEven ,
roundStatistician = roundHalfToEven ,
roundStatistic = roundHalfToEven ,
roundDutch = roundHalfToEven ,
roundGaussian = roundHalfToEven ,
roundHalfToOdd
};
//------------------------------
static
const char* getRoundingMethodName( RoundingMethod m );
//------------------------------
void assignFromString( const char *pStr );
void assignFromString( const std::string &str );
bool assignFromStringNoThrow( const char *pStr );
bool assignFromStringNoThrow( const std::string &str );
static Decimal fromString( const char *pStr ) { Decimal res; res.assignFromString(pStr); return res; }
static Decimal fromString( const std::string &str ) { Decimal res; res.assignFromString(str ); return res; }
#if defined(MARTY_DECIMAL_QT_USED)
static Decimal fromString( const QString &str ) { return fromString(str.toStdString()); }
#endif
//----------------------------------------------------------------------------
protected:
void assignFromIntImpl( std::int64_t i, int precision = 0 );
void assignFromIntImpl( std::uint64_t u, int precision = 0 );
void assignFromDoubleImpl( double d, int precision = 15 );
//----------------------------------------------------------------------------
public:
//------------------------------
#define MARTY_BCD_DECIMAL_CLASS_IMPLEMENT_FROM_INT_METHODS( castType, intType ) \
void assignFromInt( intType i, int precision = 0 ) { assignFromIntImpl( (castType)i ); m_precision = precision; } \
static Decimal fromInt( intType i, int precision = 0 ) { Decimal res; res.assignFromInt(i, precision); return res; }
MARTY_BCD_DECIMAL_CLASS_IMPLEMENT_FROM_INT_METHODS( std::int64_t , std::int64_t )
MARTY_BCD_DECIMAL_CLASS_IMPLEMENT_FROM_INT_METHODS( std::int64_t , int )
MARTY_BCD_DECIMAL_CLASS_IMPLEMENT_FROM_INT_METHODS( std::uint64_t, std::uint64_t )
MARTY_BCD_DECIMAL_CLASS_IMPLEMENT_FROM_INT_METHODS( std::uint64_t, unsigned )
//------------------------------
#define MARTY_BCD_DECIMAL_CLASS_IMPLEMENT_FROM_FLOAT_METHODS( castType, floatType, defPrecision ) \
void assignFromFloat( floatType f, int precision = defPrecision ) { assignFromDoubleImpl( (castType)f, precision ); } \
static Decimal fromFloat( floatType f, int precision = defPrecision ) { Decimal res; res.assignFromFloat(f, precision); return res; }
MARTY_BCD_DECIMAL_CLASS_IMPLEMENT_FROM_FLOAT_METHODS( double, double, std::numeric_limits<double>::digits10 /* 15 */ )
MARTY_BCD_DECIMAL_CLASS_IMPLEMENT_FROM_FLOAT_METHODS( double, float , std::numeric_limits<float> ::digits10 /* 6 */ )
//------------------------------
const char* toString( char *pBuf, std::size_t bufSize, int precision = -1, char dot = 0 ) const;
std::string toString( int precision = -1, char dot = 0 ) const;
const char* to_string( char *pBuf, std::size_t bufSize, int precision = -1, char dot = 0 ) const { return toString(pBuf, bufSize, precision, dot); }
std::string to_string( int precision = -1, char dot = 0 ) const { return toString(precision, dot); }
#if defined(MARTY_DECIMAL_QT_USED)
QString toQString( int precision = -1, char dot = 0 ) const { return QString::fromStdString(toString(precision, dot)); }
#endif
#if 0
int toInt() const { return (int)(m_num/m_denum.denum()); }
unsigned toUnsigned() const { return (unsigned)(m_num/m_denum.denum()); }
std::int64_t toInt64() const { return (m_num/m_denum.denum()); }
std::uint64_t toUnsigned64() const { return (std::uint64_t)(m_num/m_denum.denum()); }
//------------------------------
#endif
double toDouble() const;
float toFloat () const { return (float)toDouble(); }
double to_double() const;
float to_float () const { return (float)toDouble(); }
//----------------------------------------------------------------------------
protected:
template<typename IntType>
IntType toIntImpl() const
{
MARTY_DECIMAL_ASSERT_FAIL("marty::Decimal::toInt: not implemented for this type");
}
std::uint64_t getAsUint64() const
{
return bcd::rawToInt( m_number, m_precision );
}
// template<>
// std::int64_t toIntImpl<std::int64_t>() const;
// {
// if (m_sign==0)
// return 0;
// else if (m_sign>0)
// return (std::int64_t)getAsUint64();
// else
// return -(std::int64_t)getAsUint64();
// }
// template<>
// std::uint64_t toIntImpl<std::uint64_t>() const;
// {
// return (std::uint64_t)toIntImpl<std::int64_t>();
// }
//----------------------------------------------------------------------------
public:
//------------------------------
bool checkIsExact( const std::string &strDecimal ) const;
bool checkIsExact( const char *pStrDecimal ) const;
//------------------------------
//------------------------------
Decimal( ) : m_number(), m_sign(0), m_precision(0) {}
Decimal( const Decimal &d ) : m_number(d.m_number), m_sign(d.m_sign), m_precision(d.m_precision) {}
Decimal( int v, int precision = 0 ) { assignFromInt( v, precision ); }
Decimal( unsigned v, int precision = 0 ) { assignFromInt( v, precision ); }
Decimal( std::int64_t v, int precision = 0 ) { assignFromInt( v, precision ); }
Decimal( std::uint64_t v, int precision = 0 ) { assignFromInt( v, precision ); }
Decimal( double d, int precision = 12 ) { assignFromFloat( d, precision ); }
Decimal( float d, int precision = 6 ) { assignFromFloat( d, precision ); }
Decimal( const char *pStr ) { assignFromString( pStr ); }
Decimal( const std::string &str ) { assignFromString( str ); }
#if defined(MARTY_DECIMAL_QT_USED)
Decimal( const QString &str ) { assignFromString( str.toStdString() ); }
#endif
//------------------------------
//------------------------------
int compare( const Decimal &rightOp ) const;
MARTY_IMPLEMENT_RELATIONAL_OPERATORS_BY_COMPARE( Decimal )
//------------------------------
//------------------------------
void swap( Decimal &d2 );
Decimal& operator=( Decimal d2 );
//------------------------------
//----------------------------------------------------------------------------
Decimal& add( const Decimal &d );
Decimal& sub( const Decimal &d );
Decimal& mul( const Decimal &d );
Decimal& div( const Decimal &d, int precision = -1, unsigned numSignificantDigits = (unsigned)-1 );
Decimal mod_helper_raw_div( const Decimal &d ) const; //!< Возвращает частное (с одним макс знаком после запятой, по модулю, без учёта знака)
Decimal mod_helper( const Decimal &d ) const; //!< Возвращает целое (в виде Decimal), сколько раз 'd' влезает в текущее (по модулю) - для реализации получения частного и остатка от деления, кто как пожелает на свой вкус
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
Decimal operator + ( Decimal d2 ) const { Decimal res = *this; return res.add(d2); }
Decimal operator - ( Decimal d2 ) const { Decimal res = *this; return res.sub(d2); }
Decimal operator - ( ) const { Decimal res = *this; if (res.m_sign!=0) res.m_sign = -res.m_sign; return res; }
Decimal operator * ( Decimal d2 ) const { Decimal res = *this; return res.mul(d2); }
//Decimal divide( Decimal devider, precision_t resultPrecision ) const;
Decimal operator / ( Decimal d2 ) const { Decimal res = *this; return res.div(d2, m_divisionPrecision); }
// Decimal operator % ( Decimal d2 ) const;
Decimal& operator += ( Decimal d2 ) { return add(d2); }
Decimal& operator -= ( Decimal d2 ) { return sub(d2); }
Decimal& operator *= ( Decimal d2 ) { return mul(d2); }
Decimal& operator /= ( Decimal d2 ) { return div(d2, m_divisionPrecision); }
// Decimal& operator %= ( Decimal d2 );
//----------------------------------------------------------------------------
#define MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE( integralType ) \
\
Decimal operator + ( integralType i ) const { return operator+ ( Decimal(i) ); } \
Decimal operator - ( integralType i ) const { return operator- ( Decimal(i) ); } \
Decimal operator * ( integralType i ) const { return operator* ( Decimal(i) ); } \
Decimal operator / ( integralType i ) const { return operator/ ( Decimal(i) ); } \
/*Decimal operator % ( integralType i ) const { return operator% ( Decimal(i) ); }*/ \
\
Decimal& operator += ( integralType i ) { return operator+=( Decimal(i) ); } \
Decimal& operator -= ( integralType i ) { return operator-=( Decimal(i) ); } \
Decimal& operator *= ( integralType i ) { return operator*=( Decimal(i) ); } \
Decimal& operator /= ( integralType i ) { return operator/=( Decimal(i) ); } \
/*Decimal& operator %= ( integralType i ) { return operator%=( Decimal(i) ); }*/
MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE( int )
MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE( unsigned )
MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE( std::int64_t )
MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE( std::uint64_t )
MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE( float )
MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE( double )
//------------------------------
// операторы преобразования типа
explicit operator std::int64_t() const ; // { return toIntImpl<std:: int64_t>(); }
explicit operator std::uint64_t() const ; // { return toIntImpl<std::uint64_t>(); }
explicit operator int() const ; // { return (int) toIntImpl<std:: int64_t>(); }
explicit operator unsigned() const ; // { return (unsigned)toIntImpl<std::uint64_t>(); }
explicit operator float() const { return toFloat(); }
explicit operator double() const { return toDouble(); }
//----------------------------------------------------------------------------
Decimal& zerate() { m_sign = 0; return *this; } //!< Zerate-Mazerate ;) makes *this zero valued
Decimal& zeroise() { m_sign = 0; return *this; }
Decimal& negate() { m_sign *= -1; return *this; }
Decimal& invert() { return negate(); }
Decimal& reciprocate( int precision = MARTY_DECIMAL_DEFAULT_DIVISION_PRECISION ); //!< multiplicative inverse this: *this = 1/(*this) \returns *this
//------------------------------
int signum() const { return m_sign; }
int sign () const { return m_sign; }
int sgn () const { return m_sign; }
Decimal abs () const { Decimal res = *this; if (res.m_sign) res.m_sign = 1; return res; }
//Decimal mod ( Decimal d2 ) const;
Decimal neg () const { Decimal res = *this; res.negate(); return res; }
Decimal inv () const { return neg(); }
//------------------------------
Decimal zerated() const { return Decimal(0u); }
Decimal zeroised() const { return Decimal(0u); }
Decimal negated() const { return neg(); }
Decimal inverted() const { return inv(); }
//! return multiplicative inverse - 1/(*this)
Decimal reciprocated( int precision = MARTY_DECIMAL_DEFAULT_DIVISION_PRECISION ) const;
//------------------------------
bool zer () const { return m_sign==0; }
bool zero () const { return m_sign==0; }
bool isZero() const { return m_sign==0; }
bool zeq () const { return m_sign==0; } //!< zeq means "Zero EQual" - ==0. Also this is the russian joke - зек (ЗК) - means a prisoner
//------------------------------
int msp () const { return getMostSignificantDigitPower(); } //!< return Most Significant digit Power
//------------------------------
//------------------------------
//! Возвращает true, если обрезание/удлинение прошло предельно точно и ничего лишнего не было задето, и все жизненно важные органы остались на месте
bool precisionExpandTo( int p ); //!< Всегда возвращает true
bool precisionShrinkTo( int p ); //!< Возвращает true, если все обрезанные цифры были нулём
bool precisionFitTo( int p ); //!< Возвращает true, если все обрезанные цифры были нулём (если было обрезание), или true (если было расширение или ничего)
//------------------------------
//! Цифра чётна?
static
bool isDigitEven( int d );
//! Цифра нечётна?
static
bool isDigitOdd( int d );
//------------------------------
//Decimal& minimizePrecisionImpl();
int getLowestDecimalDigit() const;
//void replaceLowestDecimalDigit( unum_t d );
int getMostSignificantDigitPower() const;
//----------------------------------------------------------------------------
protected:
Decimal makeMinimalPrecisionImplHelper( int newNum ) const;
//----------------------------------------------------------------------------
public:
Decimal makeMinimalPrecisionOne() const;
Decimal makeMinimalPrecisionTwo() const;
Decimal makeMinimalPrecisionFive() const;
//------------------------------
//------------------------------
static int getOutputPrecision() { return m_outputPrecision; }
static void setOutputPrecision( int p ) { m_outputPrecision = p; }
static void setOutputPrecisionToStreamPrecision( ) { setOutputPrecision(0); }
static void setOutputPrecisionToAuto( ) { setOutputPrecision(-1); }
int precision() const { return m_precision; }
//------------------------------
static int getDivisionPrecision() { return m_divisionPrecision; }
static void setDivisionPrecision( int p = MARTY_DECIMAL_DEFAULT_DIVISION_PRECISION) { m_divisionPrecision = p; }
static unsigned getDivisionNumberOfSignificantDigits() { return m_divisionNumberOfSignificantDigits; }
static void setDivisionNumberOfSignificantDigits( unsigned n ) { m_divisionNumberOfSignificantDigits = n; }
//------------------------------
//! Return which part of d is *this (in percents/permilles) - d is 100%
Decimal getPercentOf ( const Decimal &d ) const;
Decimal getPermilleOf( const Decimal &d ) const;
Decimal getExPercentOf ( const Decimal &d, int precision, unsigned numSignificantDigits ) const;
Decimal getExPermilleOf( const Decimal &d, int precision, unsigned numSignificantDigits ) const;
//! Return which part of *this is d (in percents/permilles) - *this is 100%
Decimal getPercent ( const Decimal &d ) const { return d.getPercentOf (*this); }
Decimal getPermille ( const Decimal &d ) const { return d.getPermilleOf(*this); }
Decimal getExPercent ( const Decimal &d, int precision, unsigned numSignificantDigits ) const { return d.getExPercentOf (*this, precision, numSignificantDigits); }
Decimal getExPermille( const Decimal &d, int precision, unsigned numSignificantDigits ) const { return d.getExPermilleOf(*this, precision, numSignificantDigits); }
//------------------------------
Decimal& round ( int precision, RoundingMethod roundingMethod );
Decimal rounded( int precision, RoundingMethod roundingMethod ) const;
//------------------------------
static char setDefaultDecimalSeparator( char sep ) { char res = decimalSeparator; decimalSeparator = sep; return res; }
static char getDefaultDecimalSeparator( ) { return decimalSeparator; }
#if !defined(MARTY_DECIMAL_DISABLE_MODERN_CPP_SUPPORT)
std::size_t hash() const
{
std::size_t seed = bcd::raw_bcd_number_hasher(m_number);
seed = bcd::raw_bcd_hash_combine(seed, m_sign);
return bcd::raw_bcd_hash_combine(seed, m_precision);
}
#endif
//----------------------------------------------------------------------------
protected:
Decimal& roundingImpl2( int requestedPrecision, RoundingMethod roundingMethod );
Decimal& roundingImpl1( int requestedPrecision, RoundingMethod roundingMethod );
Decimal& roundingImpl ( int requestedPrecision, RoundingMethod roundingMethod );
Decimal implGetPercentOf ( const Decimal &scale, const Decimal &d ) const;
Decimal implGetExPercentOf ( const Decimal &scale, const Decimal &d, int precision, unsigned numSignificantDigits ) const;
Decimal( int sign, int precision, bcd::raw_bcd_number_t number )
: m_number(number), m_sign(sign), m_precision(precision)
{}
bcd::raw_bcd_number_t m_number;
int m_sign;
int m_precision;
//------------------------------
// Global affecting options
// if == -1 - Exact Decimal number precision will be used
// if == 0 - Output stream precision will be used
// if > 0 - Exact precision will be used
inline static int m_outputPrecision = -1;
inline static int m_divisionPrecision = MARTY_DECIMAL_DEFAULT_DIVISION_PRECISION;
inline static unsigned m_divisionNumberOfSignificantDigits = 3;
//------------------------------
inline static char decimalSeparator = '.';
}; // class Decimal
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
template<>
std::int64_t Decimal::toIntImpl<std::int64_t>() const
{
if (m_sign==0)
return 0;
else if (m_sign>0)
return (std::int64_t)getAsUint64();
else
return -(std::int64_t)getAsUint64();
}
//----------------------------------------------------------------------------
template<>
std::uint64_t Decimal::toIntImpl<std::uint64_t>() const
{
return (std::uint64_t)toIntImpl<std::int64_t>();
}
//----------------------------------------------------------------------------
inline /* explicit */ Decimal::operator std::int64_t() const { return toIntImpl<std:: int64_t>(); }
inline /* explicit */ Decimal::operator std::uint64_t() const { return toIntImpl<std::uint64_t>(); }
inline /* explicit */ Decimal::operator int() const { return (int) toIntImpl<std:: int64_t>(); }
inline /* explicit */ Decimal::operator unsigned() const { return (unsigned)toIntImpl<std::uint64_t>(); }
//----------------------------------------------------------------------------
#include "marty_bcd_decimal_impl.h"
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
inline
std::ostream& operator<<( std::ostream& os, const Decimal &v )
{
int precision = Decimal::getOutputPrecision();
if (precision==-1 || precision<0)
{
// Exact Decimal number precision will be used
precision = v.precision();
// if (precision > Decimal::maxPrecision())
// precision = Decimal::maxPrecision();
}
else if (precision>0)
{
// Exact taken precision will be used
}
else // precision==0
{
// Output stream precision will be used
precision = (int)os.width();
// if (precision > Decimal::maxPrecision())
// precision = Decimal::maxPrecision();
}
//auto minPrecision = (Decimal::precision_t)os.precision();
//if (minPrecision<1)
// minPrecision = 1;
os << v.toString(precision);
return os;
}
//----------------------------------------------------------------------------
#define MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE_FRIENDS( integralType ) \
\
inline Decimal operator + ( integralType i, const Decimal d ) { return Decimal(i) + d; } \
inline Decimal operator - ( integralType i, const Decimal d ) { return Decimal(i) - d; } \
inline Decimal operator * ( integralType i, const Decimal d ) { return Decimal(i) * d; } \
inline Decimal operator / ( integralType i, const Decimal d ) { return Decimal(i) / d; } \
/*Decimal operator % ( integralType i ) { return operator% ( Decimal(i) ); }*/
MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE_FRIENDS( int )
MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE_FRIENDS( unsigned )
MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE_FRIENDS( std::int64_t )
MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE_FRIENDS( std::uint64_t )
MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE_FRIENDS( float )
MARTY_DECIMAL_IMPLEMENT_ARIPHMETICT_OVERLOADS_FOR_INTEGRAL_TYPE_FRIENDS( double )
// Obsilette
//template<typename T>
//Decimal fromString( const T &t ) { return Decimal::fromString(t); }
} // namespace marty
// inject hash to std namespace
// https://en.cppreference.com/w/cpp/utility/hash
// https://stackoverflow.com/questions/17016175/c-unordered-map-using-a-custom-class-type-as-the-key
namespace std{
#if !defined(MARTY_DECIMAL_DISABLE_MODERN_CPP_SUPPORT)
template <>
struct hash<marty::Decimal>
{
typedef marty::Decimal argument_type;
typedef std::size_t result_type;
std::size_t operator()(const marty::Decimal& d) const
{
return d.hash();
}
};
inline
bool signbit(const marty::Decimal &d)
{
return d.signum()<0 ? true : false;
}
inline
std::string to_string(const marty::Decimal d)
{
return d.toString();
}
#endif
inline
marty::Decimal sqrt(const marty::Decimal &d)
{
return marty::Decimal( std::sqrt(double(d)) );
}
inline
marty::Decimal fabs(const marty::Decimal &d)
{
return d.abs();
}
inline
marty::Decimal abs(const marty::Decimal &d)
{
return d.abs();
}
} // namespace std
//----------------------------------------------------------------------------