OnixS C++ ICE Binary Order Entry Handler 1.0.0
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
23#include <OnixS/ICE/BOE/ABI.h>
24
27
29
30
32
35//
36
37
38constexpr
39inline bool isNull(const Decimal&) noexcept
40{
41 return false;
42}
43
48bool
50 const Decimal& operand,
51 Int32 exponent,
52 Decimal& quantized);
53
62Decimal quantize(const Decimal& operand, Int32 exponent);
63
69template <class MantissaType>
71MantissaType quantizedMantissa(const Decimal& operand, Int32 exponent)
72{
73 const Decimal::Mantissa res =
75 operand, exponent)
76 .mantissa();
77
78 if(static_cast<MantissaType>(
79 (std::numeric_limits<MantissaType>::max)()) >= res)
80 {
81 return static_cast<MantissaType>(res);
82 }
83
85}
86
87template
88<
89 class MantissaType,
90 class ExponentType
91>
100
101template
102 <
103 class MantissaType,
104 class ExponentType
105 >
108 const Decimal& number)
109{
110 if(static_cast<MantissaType>(
111 (std::numeric_limits<MantissaType>::max)()) < number.mantissa())
112 {
114 }
115
116 res =
118 static_cast<MantissaType>(number.mantissa()),
119 number.exponent());
120}
121
127template
128<
129 class DecimalT
130>
133typename std::enable_if<details::IsDecimal<DecimalT>::value, DecimalT>::type
134 convert(const Decimal& number)
135{
136 DecimalT res;
137 convert(res, number);
138 return res;
139}
140
141
145 const Decimal& left, const Decimal& right) noexcept;
146
147
148inline bool operator ==(const Decimal& left, const Decimal& right) noexcept
149{
150 return ((left.exponent() == right.exponent()) ? (left.mantissa() == right.mantissa()) : decimalEqual(left, right));
151}
152
153
154inline bool operator!=(
155 const Decimal& left, const Decimal& right)
156{
157 return ((left.exponent() == right.exponent()) ? (left.mantissa() != right.mantissa()) : !decimalEqual(left, right));
158}
159
160
164 const Decimal& left, const Decimal& right) noexcept;
165
166
167inline bool operator <(
168 const Decimal& left, const Decimal& right) noexcept
169{
170 return ((left.exponent() == right.exponent()) ? (left.mantissa() < right.mantissa()) : decimalLess(left, right));
171}
172
173
174inline bool operator<=(
175 const Decimal& left, const Decimal& right)
176{
177 return !(right < left);
178}
179
180
181inline
183bool
184operator >(
185 const Decimal& left,
186 const Decimal& right)
187{
188 return (right < left);
189}
190
191
192inline
194bool
195operator >=(
196 const Decimal& left,
197 const Decimal& right)
198{
199 return (right <= left);
200}
201
203void
205 std::string&,
206 Int64,
207 Int32);
208
210size_t
212 const Decimal&,
213 Char* buf,
214 size_t size);
215
219bool
221 Decimal&,
222 const Char*,
223 size_t) noexcept;
224
225
228inline
229bool
231 Decimal& value,
232 const std::string& str) noexcept
233{
234 return
235 fromStr(
236 value,
237 str.c_str(),
238 str.size());
239}
240
241inline
242void
244 std::string& str,
245 const Decimal& number)
246{
248 (
249 str,
250 number.mantissa(),
251 number.exponent()
252 );
253}
254
256template
257<
258 class Mantissa,
259 class Exponent
260>
261inline
262void
264 std::string& str,
265 const
267 <Mantissa, Exponent>& number)
268{
269 if(isNull(number))
270 {
271 str += "[]";
272 return;
273 }
274
275 toStr(str, Decimal(number));
276}
277
279template
280<
281 class Mantissa,
282 class Exponent
283>
284inline
285std::ostream&
287 std::ostream& stream,
288 const
290 <Mantissa, Exponent>& value)
291{
292 std::string str;
293
294 toStr(str, value);
295
296 return stream << str;
297}
298
300template
301<
302 class Mantissa,
303 class Exponent
304>
305inline
306void
308 std::string& str,
309 const
311 <Mantissa, Exponent>& number)
312{
313 if(isNull(number))
314 {
315 str += "[]";
316 return;
317 }
318
319 toStr(str, Decimal(number));
320}
321
323template
324<
325 class Mantissa,
326 class Exponent
327>
330{
331 std::string str;
332
333 toStr(str, number);
334
335 return str;
336}
337
339template
340<
341 class Mantissa,
342 class Exponent
343>
344inline
346std::string
348 const
350 <
351 Mantissa,
352 Exponent
353 >& number)
354{
355 std::string str;
356
357 toStr(str, number);
358
359 return str;
360}
361
363inline
364std::ostream& operator << (std::ostream& stream, const Decimal& value)
365{
366 std::string str;
367
368 toStr(str, value);
369
370 return stream << str;
371}
372
374template
375 <
376 class Mantissa,
377 class Exponent
378 >
379inline
380std::ostream&
382 std::ostream& stream,
383 const
385 <Mantissa, Exponent>& value)
386{
387 std::string str;
388
389 toStr(str, value);
390
391 return stream << str;
392}
393
395template
396<
397 class Decimal1,
398 class Decimal2
399>
400void checkAgsValid(
401 const Decimal1& arg1, const Decimal2& arg2)
402{
403 if(isNull(arg1) || isNull(arg2))
404 throw std::invalid_argument("Provided argument is Null.");
405}
406
408template
409<
410 class Mantissa,
411 class Exponent
412>
415bool
416operator ==(
417 const
419 <Mantissa, Exponent>& left,
420 const
422 <Mantissa, Exponent>& right)
423{
424 if(isNull(left) && isNull(right))
425 return true;
426
427 return left.mantissa() == right.mantissa();
428}
429
431template
432<
433 class Mantissa,
434 class Exponent
435>
438bool
439operator !=(
440 const
442 <Mantissa, Exponent>& left,
443 const
445 <Mantissa, Exponent>& right)
446{
447 return !(left == right);
448}
449
451template
452<
453 class Mantissa,
454 class Exponent
455>
458bool
459operator <(
460 const
462 <Mantissa, Exponent>& left,
463 const
465 <Mantissa, Exponent>& right)
466{
467 checkAgsValid(left, right);
468
469 return left.mantissa() < right.mantissa();
470}
471
473template
474<
475 class Mantissa,
476 class Exponent
477>
480bool
481operator >(
482 const
484 <Mantissa, Exponent>& left,
485 const
487 <Mantissa, Exponent>& right)
488{
489 checkAgsValid(left, right);
490
491 return left.mantissa() > right.mantissa();
492}
493
495template
496<
497 class Mantissa,
498 class Exponent
499>
502bool
503operator <=(
504 const
506 <Mantissa, Exponent>& left,
507 const
509 <Mantissa, Exponent>& right)
510{
511 checkAgsValid(left, right);
512
513 return left.mantissa() <= right.mantissa();
514}
515
517template
518<
519 class Mantissa,
520 class Exponent
521>
524bool
525operator >=(
526 const
528 <Mantissa, Exponent>& left,
529 const
531 <Mantissa, Exponent>& right)
532{
533 checkAgsValid(left, right);
534
535 return left.mantissa() >= right.mantissa();
536}
537
539template
540<
541 class Decimal1,
542 class Decimal2
543>
546typename std::enable_if<details::AreBothDecimals<Decimal1, Decimal2>::value, bool>::type
548 const Decimal1& left, const Decimal2& right)
549{
550 if(isNull(left) && isNull(right))
551 return true;
552
553 return Decimal(left) == Decimal(right);
554}
555
557template
558<
559 class Decimal1,
560 class Decimal2
561>
564typename std::enable_if<details::AreBothDecimals<Decimal1, Decimal2>::value, bool>::type
566 const Decimal1& left, const Decimal2& right)
567{
568 return !(left == right);
569}
570
572template
573<
574 class Decimal1,
575 class Decimal2
576>
579typename std::enable_if<details::AreBothDecimals<Decimal1, Decimal2>::value, bool>::type
581 const Decimal1& left, const Decimal2& right)
582{
583 checkAgsValid(left, right);
584
585 return Decimal(left) > Decimal(right);
586}
587
589template
590<
591 class Decimal1,
592 class Decimal2
593>
596typename std::enable_if<details::AreBothDecimals<Decimal1, Decimal2>::value, bool>::type
598 const Decimal1& left, const Decimal2& right)
599{
600 checkAgsValid(left, right);
601
602 return Decimal(left) >= Decimal(right);
603}
604
606template
607<
608 class Decimal1,
609 class Decimal2
610>
613typename std::enable_if<details::AreBothDecimals<Decimal1, Decimal2>::value, bool>::type
615 const Decimal1& left, const Decimal2& right)
616{
617 checkAgsValid(left, right);
618
619 return Decimal(left) < Decimal(right);
620}
621
623template
624<
625 class Decimal1,
626 class Decimal2
627>
630typename std::enable_if<details::AreBothDecimals<Decimal1, Decimal2>::value, bool>::type
632 const Decimal1& left, const Decimal2& right)
633{
634 checkAgsValid(left, right);
635
636 return Decimal(left) <= Decimal(right);
637}
638
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE_BEGIN
Definition ABI.h:102
#define ONIXS_ICEBOE_MESSAGING_NAMESPACE_END
Definition ABI.h:106
#define ONIXS_ICEBOE_EXPORTED
Definition Compiler.h:153
#define ONIXS_ICEBOE_PURE
Definition Compiler.h:157
#define ONIXS_ICEBOE_NODISCARD
Definition Compiler.h:154
A real number with a floating exponent.
Definition Decimal.h:39
bool operator>=(const Decimal &left, const Decimal &right)
std::ostream & operator<<(std::ostream &stream, const FloatingPointDecimal< Mantissa, Exponent > &value)
Serializes into a stream.
FloatingPointDecimal< Int64, Int32 > Decimal
Universal decimal type.
MantissaType quantizedMantissa(const Decimal &operand, Int32 exponent)
Quantize so its exponent is the same as that of provided value.
constexpr std::enable_if<!details::HasMemberTraits< Value >::value, size_t >::type size() noexcept
Definition Memory.h:303
bool decimalLess(const Decimal &left, const Decimal &right) noexcept
bool operator>(const Decimal &left, const Decimal &right)
bool operator!=(const Decimal &left, const Decimal &right)
bool decimalEqual(const Decimal &left, const Decimal &right) noexcept
char Char
Character type alias.
Definition String.h:31
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 decimalToStr(std::string &, Int64, Int32)
bool operator<=(const Decimal &left, const Decimal &right)
bool operator<(const Decimal &left, const Decimal &right) noexcept
bool fromStr(Decimal &, const Char *, size_t) noexcept
Deserializes a decimal number from the given text presentation.
bool operator==(const Decimal &left, const Decimal &right) noexcept
constexpr bool isNull(const Decimal &) noexcept
ONIXS_ICEBOE_FORCEINLINE auto convert(typename ArgType< Callable >::type value) noexcept(noexcept(Callable::Nothrow)) -> decltype(std::declval< Callable >()(std::declval< typename ArgType< Callable >::type >()))
Definition Composites.h:35