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