OnixS C++ CME MDP Conflated UDP Handler 1.1.2
API documentation
Loading...
Searching...
No Matches
Decimal.h
Go to the documentation of this file.
1// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
2//
3// This software owned by Onix Solutions Limited [OnixS] and is
4// protected by copyright law and international copyright treaties.
5//
6// Access to and use of the software is governed by the terms of the applicable
7// OnixS Software Services Agreement (the Agreement) and Customer end user license
8// agreements granting a non-assignable, non-transferable and non-exclusive license
9// to use the software for it's own data processing purposes under the terms defined
10// in the Agreement.
11//
12// Except as otherwise granted within the terms of the Agreement, copying or
13// reproduction of any part of this source code or associated reference material
14// to any other location for further reproduction or redistribution, and any
15// amendments to this copyright notice, are expressly prohibited.
16//
17// Any reproduction or redistribution for sale or hiring of the Software not in
18// accordance with the terms of the Agreement is a violation of copyright law.
19//
20
21#pragma once
22
23#include <string>
24
26
28
36template
37<
38 class MantissaType,
39 class ExponentType
40>
42{
43 // Only mantissa is stored.
44 MantissaType mantissa_;
45
46public:
48 typedef MantissaType Mantissa;
49
51 typedef ExponentType Exponent;
52
54 enum
55 {
62 Size = sizeof(Mantissa)
63 };
64
67 : mantissa_()
68 {
69 }
70
72 explicit
75 : mantissa_(mantissa)
76 {
77 }
78
81 {
82 return mantissa_;
83 }
84
87 {
88 return Exponent();
89 }
90};
91
93template
94<
95 class Mantissa,
96 class Exponent
97>
98bool
99operator ==(
100 const
102 <Mantissa, Exponent>& left,
103 const
105 <Mantissa, Exponent>& right)
106{
107 return
108 (
109 left.mantissa() ==
110 right.mantissa()
111 );
112}
113
115template
116<
117 class Mantissa,
118 class Exponent
119>
120bool
121operator !=(
122 const
124 <Mantissa, Exponent>& left,
125 const
127 <Mantissa, Exponent>& right)
128{
129 return
130 (
131 left.mantissa() !=
132 right.mantissa()
133 );
134}
135
137template
138<
139 class Mantissa,
140 class Exponent
141>
142bool
143operator <(
144 const
146 <Mantissa, Exponent>& left,
147 const
149 <Mantissa, Exponent>& right)
150{
151 return
152 (
153 left.mantissa() <
154 right.mantissa()
155 );
156}
157
159template
160<
161 class Mantissa,
162 class Exponent
163>
164bool
165operator >(
166 const
168 <Mantissa, Exponent>& left,
169 const
171 <Mantissa, Exponent>& right)
172{
173 return
174 (
175 left.mantissa() >
176 right.mantissa()
177 );
178}
179
181template
182<
183 class Mantissa,
184 class Exponent
185>
186bool
187operator <=(
188 const
190 <Mantissa, Exponent>& left,
191 const
193 <Mantissa, Exponent>& right)
194{
195 return
196 (
197 left.mantissa() <=
198 right.mantissa()
199 );
200}
201
203template
204<
205 class Mantissa,
206 class Exponent
207>
208bool
209operator >=(
210 const
212 <Mantissa, Exponent>& left,
213 const
215 <Mantissa, Exponent>& right)
216{
217 return
218 (
219 left.mantissa() >=
220 right.mantissa()
221 );
222}
223
225typedef Int64 DecimalMantissa;
226
229
232{
233 DecimalMantissa mantissa_;
234 DecimalExponent exponent_;
235
236public:
238 typedef
241
243 typedef
246
249 : mantissa_(0)
250 , exponent_(0)
251 {
252 }
253
258 : mantissa_(mantissa)
259 , exponent_(exponent)
260 {
261 }
262
265 const Decimal& other)
266 : mantissa_(other.mantissa_)
267 , exponent_(other.exponent_)
268 {
269 }
270
273 template
274 <
275 class AMantissa,
276 class AExponent
277 >
279 const
281 <AMantissa, AExponent>& other)
282 : mantissa_(
283 other.mantissa())
284 , exponent_(
285 other.exponent())
286 {
287 }
288
291 {
292 return mantissa_;
293 }
294
297 {
298 return exponent_;
299 }
300
302 Decimal&
303 operator =(
304 const Decimal& other)
305 {
306 mantissa_ = other.mantissa_;
307 exponent_ = other.exponent_;
308
309 return *this;
310 }
311
314 template
315 <
316 class AMantissa,
317 class AExponent
318 >
319 Decimal&
320 operator =(
321 const
323 <AMantissa, AExponent>& other)
324 {
325 mantissa_ = other.mantissa();
326 exponent_ = other.exponent();
327
328 return *this;
329 }
330};
331
333ONIXS_CONFLATEDUDP_EXPORTED
334bool
336 const Decimal& left,
337 const Decimal& right);
338
340inline
341bool
342operator ==(
343 const Decimal& left,
344 const Decimal& right)
345{
346 return
347 (
348 (
349 left.exponent() ==
350 right.exponent()
351 )
352 ? (
353 left.mantissa() ==
354 right.mantissa()
355 )
357 (
358 left,
359 right
360 )
361 );
362}
363
365inline
366bool
367operator !=(
368 const Decimal& left,
369 const Decimal& right)
370{
371 return
372 (
373 (
374 left.exponent() ==
375 right.exponent()
376 )
377 ? (
378 left.mantissa() !=
379 right.mantissa()
380 )
381 : !decimalEqual
382 (
383 left,
384 right
385 )
386 );
387}
388
390template
391<
392 class Mantissa,
393 class Exponent
394>
395inline
396bool
397operator ==(
398 const Decimal& left,
399 const
401 <Mantissa, Exponent>& right)
402{
403 return (left == Decimal(right));
404}
405
407template
408<
409 class Mantissa,
410 class Exponent
411>
412inline
413bool
414operator ==(
415 const
417 <Mantissa, Exponent>& left,
418 const Decimal& right)
419{
420 return (Decimal(left) == right);
421}
422
424template
425<
426 class Mantissa,
427 class Exponent
428>
429inline
430bool
431operator !=(
432 const Decimal& left,
433 const
435 <Mantissa, Exponent>& right)
436{
437 return (left != Decimal(right));
438}
439
441template
442<
443 class Mantissa,
444 class Exponent
445>
446inline
447bool
448operator !=(
449 const
451 <Mantissa, Exponent>& left,
452 const Decimal& right)
453{
454 return (Decimal(left) != right);
455}
456
458ONIXS_CONFLATEDUDP_EXPORTED
459bool
461 const Decimal& left,
462 const Decimal& right);
463
465inline
466bool
467operator <(
468 const Decimal& left,
469 const Decimal& right)
470{
471 return
472 (
473 (
474 left.exponent() ==
475 right.exponent()
476 )
477 ? (
478 left.mantissa() <
479 right.mantissa()
480 )
482 (
483 left,
484 right
485 )
486 );
487}
488
490inline
491bool
492operator >(
493 const Decimal& left,
494 const Decimal& right)
495{
496 return (right < left);
497}
498
500inline
501bool
502operator <=(
503 const Decimal& left,
504 const Decimal& right)
505{
506 return !(right < left);
507}
508
510inline
511bool
512operator >=(
513 const Decimal& left,
514 const Decimal& right)
515{
516 return !(right > left);
517}
518
520template
521<
522 class Mantissa,
523 class Exponent
524>
525inline
526bool
527operator <(
528 const Decimal& left,
529 const
531 <Mantissa, Exponent>& right)
532{
533 return (left < Decimal(right));
534}
535
537template
538<
539 class Mantissa,
540 class Exponent
541>
542inline
543bool
544operator <(
545 const
547 <Mantissa, Exponent>& left,
548 const Decimal& right)
549{
550 return (Decimal(left) < right);
551}
552
554template
555<
556 class Mantissa,
557 class Exponent
558>
559inline
560bool
561operator >(
562 const Decimal& left,
563 const
565 <Mantissa, Exponent>& right)
566{
567 return (left > Decimal(right));
568}
569
571template
572<
573 class Mantissa,
574 class Exponent
575>
576inline
577bool
578operator >(
579 const
581 <Mantissa, Exponent>& left,
582 const Decimal& right)
583{
584 return (Decimal(left) > right);
585}
586
589ONIXS_CONFLATEDUDP_EXPORTED
590bool
592 Decimal&,
593 const Char*,
594 size_t);
595
598ONIXS_CONFLATEDUDP_EXPORTED
599void
601 std::string&,
602 Int64,
603 Int32);
604
606inline
607void
609 std::string& str,
610 const Decimal& number)
611{
613 (
614 str,
615 number.mantissa(),
616 number.exponent()
617 );
618}
619
621inline
622std::string
624 const Decimal& number)
625{
626 std::string str;
627
628 toStr(str, number);
629
630 return str;
631}
632
634template
635<
636 class Mantissa,
637 class Exponent
638>
639inline
640void
642 std::string& str,
643 const
645 <Mantissa, Exponent>& number)
646{
647 toStr(
648 str,
649 Decimal(number));
650}
651
653template
654<
655 class Mantissa,
656 class Exponent
657>
658inline
659std::string
661 const
663 <
664 Mantissa,
665 Exponent
666 >& number)
667{
668 std::string str;
669
670 toStr(str, number);
671
672 return str;
673}
674
#define ONIXS_CONFLATEDUDP_LTWT_CLASS
Definition Bootstrap.h:95
#define ONIXS_CONFLATEDUDP_NAMESPACE_END
Definition Bootstrap.h:157
#define ONIXS_CONFLATEDUDP_NAMESPACE_BEGIN
Definition Bootstrap.h:153
A real number with floating exponent.
Definition Decimal.h:232
DecimalExponent Exponent
Aliases exponent component type.
Definition Decimal.h:245
Mantissa mantissa() const
Returns mantissa of given decimal.
Definition Decimal.h:290
Decimal(Mantissa mantissa, Exponent exponent)
Explicitly initializes instance from its mantissa value.
Definition Decimal.h:255
Decimal(const FixedPointDecimal< AMantissa, AExponent > &other)
Definition Decimal.h:278
Decimal(const Decimal &other)
Initializes instance as copy of the other one.
Definition Decimal.h:264
Decimal()
Default (zero) initialization.
Definition Decimal.h:248
DecimalMantissa Mantissa
Aliases mantissa component type.
Definition Decimal.h:240
Exponent exponent() const
Returns exponent of given decimal.
Definition Decimal.h:296
FixedPointDecimal(Mantissa mantissa)
Explicitly initializes instance from its mantissa value.
Definition Decimal.h:73
FixedPointDecimal()
Default (zero) initialization.
Definition Decimal.h:66
Exponent exponent() const
Returns exponent of given decimal.
Definition Decimal.h:86
Int32 DecimalExponent
Aliases exponent component type for the decimal type.
Definition Decimal.h:228
Int64 DecimalMantissa
Aliases mantissa component type for the decimal type.
Definition Decimal.h:225
ONIXS_CONFLATEDUDP_EXPORTED bool decimalLess(const Decimal &left, const Decimal &right)
Compares two decimals.
ONIXS_CONFLATEDUDP_EXPORTED bool fromStr(Decimal &, const Char *, size_t)
ONIXS_CONFLATEDUDP_EXPORTED bool decimalEqual(const Decimal &left, const Decimal &right)
Checks two decimals for equality.
ONIXS_CONFLATEDUDP_EXPORTED void toStr(std::string &, BookState::Enum)
Serializes book state value into a string.
Int32 Int32
int32.
Definition Fields.h:69
ONIXS_CONFLATEDUDP_EXPORTED void decimalToStr(std::string &, Int64, Int32)
char Char
Character type alias.
Definition String.h:36