OnixS ICE iMpact Multicast Price Feed Handler C++ library  8.11.0
API documentation
Rational.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Onix Solutions Limited. All rights reserved.
3  *
4  * This software owned by Onix Solutions Limited and is protected by copyright law
5  * and international copyright treaties.
6  *
7  * Access to and use of the software is governed by the terms of the applicable ONIXS Software
8  * Services Agreement (the Agreement) and Customer end user license agreements granting
9  * a non-assignable, non-transferable and non-exclusive license to use the software
10  * for it's own data processing purposes under the terms defined in the Agreement.
11  *
12  * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13  * of this source code or associated reference material to any other location for further reproduction
14  * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15  *
16  * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17  * the terms of the Agreement is a violation of copyright law.
18  */
19 
20 #pragma once
21 
22 #include "Export.h"
23 
24 #include <iosfwd>
25 #include <string>
26 
27 namespace OnixS { namespace ICE { namespace iMpact { namespace MarketData {
28 
29 /// Rational number representation.
30 struct ONIXS_ICEMDH_EXPORT Rational
31 {
32  /// This value indicates that value is in not set.
33  static const long long NO_VALUE;
34 
35  /// Numerator
36  long long numerator;
37 
38  /// Denominator.
39  unsigned int denominator;
40 
41  /// Constructs default instance.
42  Rational();
43 
44  /// Create initialized instance.
45  Rational(long long numerator, unsigned int denominator);
46 
47  /// Converts some Rational value to another denominator.
48  Rational(const Rational& other, unsigned int newDenominator);
49 
50  /// Indicates if Rational has no value.
51  bool noValue() const;
52 
53  /// Converts Rational representation to needed number type.
54  ///
55  /// Target number type should have accessible constructor from \c int type,
56  /// and should have visible \c operator/.
57  ///
58  /// \code
59  /// Decimal d = rational.convertTo<Decimal>()
60  /// \endcode
61  ///
62  template <typename T>
63  T convertTo() const;
64 
65  /// Implicit conversion to float number type
66  operator float() const;
67 
68  /// Implicit conversion to double number type
69  operator double() const;
70 
71  /// string representation of Rational
72  std::string toString () const;
73 
74  /// Rational comparison
75  bool operator==(const Rational& right) const;
76  bool operator!=(const Rational& right) const;
77  bool operator<(const Rational& right) const;
78  bool operator>(const Rational& right) const;
79  bool operator<=(const Rational& right) const;
80  bool operator>=(const Rational& right) const;
81 };
82 
83 /// Make it printable to formatted C++ I/O streams.
84 ONIXS_ICEMDH_EXPORT std::ostream& operator<<(std::ostream& stream, const Rational& value);
85 
87  : numerator(NO_VALUE)
88  , denominator(1)
89 {
90 }
91 
92 inline Rational::Rational(long long inNumerator, unsigned int inDenominator)
93  : numerator(inNumerator)
94  , denominator(inDenominator)
95 {
96 }
97 
98 inline Rational::Rational(const Rational& other, unsigned int newDenominator)
99  : numerator(newDenominator == other.denominator ? other.numerator : int (other.convertTo<double>() * newDenominator))
100  , denominator(newDenominator)
101 {
102 }
103 
104 inline bool Rational::noValue() const
105 {
106  return numerator == NO_VALUE;
107 }
108 
109 template <typename T>
110 inline T Rational::convertTo() const
111 {
112  return T(numerator) / T(denominator);
113 }
114 
115 inline Rational::operator float() const
116 {
117  return convertTo<float>();
118 }
119 
120 inline Rational::operator double() const
121 {
122  return convertTo<double>();
123 }
124 
125 inline bool Rational::operator==(const Rational& right) const
126 {
127  if (denominator == right.denominator)
128  return numerator == right.numerator;
129 
130  return numerator * right.denominator == right.numerator * denominator;
131 }
132 
133 inline bool Rational::operator!=(const Rational& right) const
134 {
135  return !(*this == right);
136 }
137 
138 inline bool Rational::operator<(const Rational& right) const
139 {
140  if (denominator == right.denominator)
141  return numerator < right.numerator;
142 
143  return numerator * right.denominator < right.numerator * denominator;
144 }
145 
146 inline bool Rational::operator>(const Rational& right) const
147 {
148  return right < *this;
149 }
150 
151 inline bool Rational::operator<=(const Rational& right) const
152 {
153  return !(right < *this);
154 }
155 
156 inline bool Rational::operator>=(const Rational& right) const
157 {
158  return !(*this < right);
159 }
160 
161 }}}} // namespace MarketData, iMpact, ICE, OnixS
bool noValue() const
Indicates if Rational has no value.
Definition: Rational.h:104
bool operator<=(const Rational &right) const
Definition: Rational.h:151
static const long long NO_VALUE
This value indicates that value is in not set.
Definition: Rational.h:33
std::ostream & operator<<(std::ostream &, const Error &)
Make it printable to formatted C++ I/O streams.
bool operator>(const Rational &right) const
Definition: Rational.h:146
Rational()
Constructs default instance.
Definition: Rational.h:86
Rational number representation.
Definition: Rational.h:30
bool operator>=(const Rational &right) const
Definition: Rational.h:156
unsigned int denominator
Denominator.
Definition: Rational.h:39
bool operator==(const Rational &right) const
Rational comparison.
Definition: Rational.h:125
bool operator!=(const Rational &right) const
Definition: Rational.h:133
bool operator<(const Rational &right) const
Definition: Rational.h:138