OnixS C++ CME MDP Conflated TCP Handler 1.3.6
API Documentation
Loading...
Searching...
No Matches
Decimal.Operations.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
24
27
28#include <limits>
29
31
36void throwCannotQuantizeOrConvert();
37
40//
41
45{
46 return false;
47}
48
53bool
55 const Decimal& operand,
56 Int32 exponent,
57 Decimal& quantized);
58
67Decimal quantize(const Decimal& operand, Int32 exponent);
68
74template <class MantissaType>
76MantissaType quantizedMantissa(const Decimal& operand, Int32 exponent)
77{
78 const Decimal::Mantissa res =
80 operand, exponent)
81 .mantissa();
82
83 if(static_cast<MantissaType>(
84 (std::numeric_limits<MantissaType>::max)()) >= res)
85 {
86 return static_cast<MantissaType>(res);
87 }
88
89 throwCannotQuantizeOrConvert();
90}
91
92template
93<
94 class MantissaType,
95 class ExponentType
96>
99 const Decimal& number)
100{
101 res =
103 quantizedMantissa<MantissaType>(number, ExponentType()));
104}
105
106template
107 <
108 class MantissaType,
109 class ExponentType
110 >
113 const Decimal& number)
114{
115 if(static_cast<MantissaType>(
116 (std::numeric_limits<MantissaType>::max)()) < number.mantissa())
117 {
118 throwCannotQuantizeOrConvert();
119 }
120
121 res =
123 static_cast<MantissaType>(number.mantissa()),
124 number.exponent());
125}
126
132template
133<
134 class DecimalT
135>
138typename EnableIf<details::IsDecimal<DecimalT>::value, DecimalT>::type
139 convert(const Decimal& number)
140{
141 DecimalT res;
142 convert(res, number);
143 return res;
144}
145
149bool
150operator ==(
151 const Decimal& left,
152 const Decimal& right) ONIXS_CONFLATEDTCP_NOTHROW;
153
157bool
158operator <(
159 const Decimal& left,
160 const Decimal& right);
161
165bool
166operator <=(
167 const Decimal& left,
168 const Decimal& right);
169
171inline
173bool
174operator >(
175 const Decimal& left,
176 const Decimal& right)
177{
178 return (right < left);
179}
180
182inline
184bool
185operator >=(
186 const Decimal& left,
187 const Decimal& right)
188{
189 return (right <= left);
190}
191
194void
195decimalToStr(
196 std::string&,
197 Int64,
198 Int32);
199
202size_t
203toStr(
204 const Decimal&,
205 Char* buf,
206 size_t size);
207
211bool
213 Decimal&,
214 const Char*,
216
217
220inline
221bool
223 Decimal& value,
224 const std::string& str) ONIXS_CONFLATEDTCP_NOTHROW
225{
226 return
227 fromStr(
228 value,
229 str.c_str(),
230 str.size());
231}
232
234inline
235void
236toStr(
237 std::string& str,
238 const Decimal& number)
239{
240 decimalToStr
241 (
242 str,
243 number.mantissa(),
244 number.exponent()
245 );
246}
247
249template
250<
251 class Mantissa,
252 class Exponent
253>
254inline
255void
257 std::string& str,
258 const
260 <Mantissa, Exponent>& number)
261{
262 if(isNull(number))
263 {
264 str += "[]";
265 return;
266 }
267
268 toStr(str, Decimal(number));
269}
270
272template
273<
274 class Mantissa,
275 class Exponent
276>
277inline
278std::ostream&
280 std::ostream& stream,
281 const
283 <Mantissa, Exponent>& value)
284{
285 std::string str;
286
287 toStr(str, value);
288
289 return stream << str;
290}
291
293template
294<
295 class Mantissa,
296 class Exponent
297>
298inline
299void
301 std::string& str,
302 const
304 <Mantissa, Exponent>& number)
305{
306 if(isNull(Decimal(number)))
307 {
308 str += "[]";
309 return;
310 }
311
312 toStr(str, Decimal(number));
313}
314
316template
317<
318 class Mantissa,
319 class Exponent
320>
323{
324 std::string str;
325
326 toStr(str, number);
327
328 return str;
329}
330
332template
333<
334 class Mantissa,
335 class Exponent
336>
337inline
339std::string
341 const
343 <
344 Mantissa,
345 Exponent
346 >& number)
347{
348 std::string str;
349
350 toStr(str, number);
351
352 return str;
353}
354
356inline
357std::ostream& operator << (std::ostream& stream, const Decimal& value)
358{
359 std::string str;
360
361 toStr(str, value);
362
363 return stream << str;
364}
365
367template
368 <
369 class Mantissa,
370 class Exponent
371 >
372inline
373std::ostream&
375 std::ostream& stream,
376 const
378 <Mantissa, Exponent>& value)
379{
380 std::string str;
381
382 toStr(str, value);
383
384 return stream << str;
385}
386
388template
389<
390 class Decimal1,
391 class Decimal2
392>
393void checkAgsValid(
394 const Decimal1& arg1, const Decimal2& arg2)
395{
396 if(isNull(arg1) || isNull(arg2))
397 throw std::invalid_argument("Provided argument is Null.");
398}
399
401template
402<
403 class Mantissa,
404 class Exponent
405>
408bool
409operator ==(
410 const
412 <Mantissa, Exponent>& left,
413 const
415 <Mantissa, Exponent>& right)
416{
417 if(isNull(left) && isNull(right))
418 return true;
419
420 return left.mantissa() == right.mantissa();
421}
422
424template
425<
426 class Mantissa,
427 class Exponent
428>
431bool
432operator !=(
433 const
435 <Mantissa, Exponent>& left,
436 const
438 <Mantissa, Exponent>& right)
439{
440 return !(left == right);
441}
442
444template
445<
446 class Mantissa,
447 class Exponent
448>
451bool
452operator <(
453 const
455 <Mantissa, Exponent>& left,
456 const
458 <Mantissa, Exponent>& right)
459{
460 checkAgsValid(left, right);
461
462 return left.mantissa() < right.mantissa();
463}
464
466template
467<
468 class Mantissa,
469 class Exponent
470>
473bool
474operator >(
475 const
477 <Mantissa, Exponent>& left,
478 const
480 <Mantissa, Exponent>& right)
481{
482 checkAgsValid(left, right);
483
484 return left.mantissa() > right.mantissa();
485}
486
488template
489<
490 class Mantissa,
491 class Exponent
492>
495bool
496operator <=(
497 const
499 <Mantissa, Exponent>& left,
500 const
502 <Mantissa, Exponent>& right)
503{
504 checkAgsValid(left, right);
505
506 return left.mantissa() <= right.mantissa();
507}
508
510template
511<
512 class Mantissa,
513 class Exponent
514>
517bool
518operator >=(
519 const
521 <Mantissa, Exponent>& left,
522 const
524 <Mantissa, Exponent>& right)
525{
526 checkAgsValid(left, right);
527
528 return left.mantissa() >= right.mantissa();
529}
530
532template
533<
534 class Decimal1,
535 class Decimal2
536>
539typename EnableIf<details::AreBothDecimals<Decimal1, Decimal2>::value, bool>::type
540operator==(
541 const Decimal1& left, const Decimal2& right)
542{
543 bool isNullLeft = isNull(left);
544 bool isNullRight = isNull(right);
545
546 if (isNullLeft && isNullRight)
547 return true;
548
549 return Decimal(left) == Decimal(right);
550}
551
553template
554<
555 class Decimal1,
556 class Decimal2
557>
560typename EnableIf<details::AreBothDecimals<Decimal1, Decimal2>::value, bool>::type
562 const Decimal1& left, const Decimal2& right)
563{
564 return !(left == right);
565}
566
568template
569<
570 class Decimal1,
571 class Decimal2
572>
575typename EnableIf<details::AreBothDecimals<Decimal1, Decimal2>::value, bool>::type
576operator>(
577 const Decimal1& left, const Decimal2& right)
578{
579 checkAgsValid(left, right);
580
581 return Decimal(left) > Decimal(right);
582}
583
585template
586<
587 class Decimal1,
588 class Decimal2
589>
592typename EnableIf<details::AreBothDecimals<Decimal1, Decimal2>::value, bool>::type
593operator>=(
594 const Decimal1& left, const Decimal2& right)
595{
596 checkAgsValid(left, right);
597
598 return Decimal(left) >= Decimal(right);
599}
600
602template
603<
604 class Decimal1,
605 class Decimal2
606>
609typename EnableIf<details::AreBothDecimals<Decimal1, Decimal2>::value, bool>::type
610operator<(
611 const Decimal1& left, const Decimal2& right)
612{
613 checkAgsValid(left, right);
614
615 return Decimal(left) < Decimal(right);
616}
617
619template
620<
621 class Decimal1,
622 class Decimal2
623>
626typename EnableIf<details::AreBothDecimals<Decimal1, Decimal2>::value, bool>::type
627operator<=(
628 const Decimal1& left, const Decimal2& right)
629{
630 checkAgsValid(left, right);
631
632 return Decimal(left) <= Decimal(right);
633}
634
#define ONIXS_CONFLATEDTCP_MESSAGING_MDP_NAMESPACE_BEGIN
Definition ABI.h:147
#define ONIXS_CONFLATEDTCP_MESSAGING_MDP_NAMESPACE_END
Definition ABI.h:151
#define ONIXS_CONFLATEDTCP_PURE
Definition Compiler.h:189
#define ONIXS_CONFLATEDTCP_NORETURN
Definition Compiler.h:184
#define ONIXS_CONFLATEDTCP_CONSTEXPR
Definition Compiler.h:179
#define ONIXS_CONFLATEDTCP_EXPORTED
Definition Compiler.h:175
#define ONIXS_CONFLATEDTCP_NOTHROW
Definition Compiler.h:176
#define ONIXS_CONFLATEDTCP_NODISCARD
Definition Compiler.h:185
#define ONIXS_CONFLATEDTCP_COLDPATH
Definition Compiler.h:188
A real number with a floating exponent.
Definition Decimal.h:39
std::ostream & operator<<(std::ostream &stream, const FloatingPointDecimal< Mantissa, Exponent > &value)
Serializes into a stream.
bool isNull(const PRICE9 &value) noexcept
Definition Composites.h:767
FloatingPointDecimal< Int64, Int32 > Decimal
Universal decimal type.
bool operator!=(const FixedPointDecimal< Mantissa, Exponent > &left, const FixedPointDecimal< Mantissa, Exponent > &right)
Compares two fixed-point decimals.
MantissaType quantizedMantissa(const Decimal &operand, Int32 exponent)
Quantize so its exponent is the same as that of provided value.
std::string toStr(const FixedPointDecimal< Mantissa, Exponent > &)
Serializes a fixed-point decimal into a string.
bool quantize(const Decimal &operand, Int32 exponent, Decimal &quantized)
Quantize so its exponent is the same as that of provided value.
void convert(FixedPointDecimal< MantissaType, ExponentType > &res, const Decimal &number)
bool fromStr(Decimal &, const Char *, size_t) noexcept
Deserializes a decimal number from the given text presentation.
char Char
Character type alias.
Definition String.h:30
void toStr(std::string &str, const Negotiate200 &obj)
Serializes into a string.