OnixS BME SENAF Handler C++ library  2.3.0
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(newDenominator == other.denominator ? other.numerator : int(other.convertTo<double>() * newDenominator))
102  , denominator(newDenominator)
103 {
104 }
105 
106 inline bool Rational::noValue() const
107 {
108  return numerator == NO_VALUE;
109 }
110 
111 template <typename T>
112 inline T Rational::convertTo() const
113 {
114  return T(numerator) / T(denominator);
115 }
116 
117 inline Rational::operator float() const
118 {
119  return convertTo<float>();
120 }
121 
122 inline Rational::operator double() const
123 {
124  return convertTo<double>();
125 }
126 
127 inline bool Rational::operator==(const Rational& right) const
128 {
129  if (denominator == right.denominator)
130  {
131  return numerator == right.numerator;
132  }
133 
134  return numerator * right.denominator == right.numerator * denominator;
135 }
136 
137 inline bool Rational::operator!=(const Rational& right) const
138 {
139  return !(*this == right);
140 }
141 
142 inline bool Rational::operator<(const Rational& right) const
143 {
144  if (denominator == right.denominator)
145  {
146  return numerator < right.numerator;
147  }
148 
149  return numerator * right.denominator < right.numerator * denominator;
150 }
151 
152 inline bool Rational::operator>(const Rational& right) const
153 {
154  return right < *this;
155 }
156 
157 inline bool Rational::operator<=(const Rational& right) const
158 {
159  return !(right < *this);
160 }
161 
162 inline bool Rational::operator>=(const Rational& right) const
163 {
164  return !(*this < right);
165 }
166 
167 }}} // namespace OnixS::Senaf::MarketData
bool operator<=(const Rational &right) const
Definition: Rational.h:157
bool operator>(const Rational &right) const
Definition: Rational.h:152
bool operator>=(const Rational &right) const
Definition: Rational.h:162
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:137
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:142
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:127
bool noValue() const
Indicates if Rational has no value.
Definition: Rational.h:106