OnixS BME SENAF Handler C++ library 2.3.0
API documentation
Loading...
Searching...
No Matches
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
29namespace OnixS { namespace Senaf { namespace MarketData {
30
32struct ONIXS_BME_SENAF_EXPORT Rational
33{
35 static const long long NO_VALUE;
36
38 long long numerator;
39
41 unsigned int denominator;
42
44 Rational();
45
47 Rational(long long numerator, unsigned int denominator);
48
50 Rational(const Rational& other, unsigned int newDenominator);
51
53 bool noValue() const;
54
64 template <typename T>
65 T convertTo() const;
66
68 operator float() const;
69
71 operator double() const;
72
74 std::string toString() const;
75
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
86ONIXS_BME_SENAF_EXPORT std::ostream& operator<<(std::ostream& stream, const Rational& value);
87
90 , denominator(1)
91{
92}
93
94inline Rational::Rational(long long inNumerator, unsigned int inDenominator)
95 : numerator(inNumerator)
96 , denominator(inDenominator)
97{
98}
99
100inline 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
106inline bool Rational::noValue() const
107{
108 return numerator == NO_VALUE;
109}
110
111template <typename T>
112inline T Rational::convertTo() const
113{
114 return T(numerator) / T(denominator);
115}
116
117inline Rational::operator float() const
118{
119 return convertTo<float>();
120}
121
122inline Rational::operator double() const
123{
124 return convertTo<double>();
125}
126
127inline 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
137inline bool Rational::operator!=(const Rational& right) const
138{
139 return !(*this == right);
140}
141
142inline 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
152inline bool Rational::operator>(const Rational& right) const
153{
154 return right < *this;
155}
156
157inline bool Rational::operator<=(const Rational& right) const
158{
159 return !(right < *this);
160}
161
162inline bool Rational::operator>=(const Rational& right) const
163{
164 return !(*this < right);
165}
166
167}}} // namespace OnixS::Senaf::MarketData
std::ostream & operator<<(std::ostream &, const Error &)
Make it printable to formatted C++ I/O streams.
Rational number representation.
Definition Rational.h:33
bool operator>(const Rational &right) const
Definition Rational.h:152
static const long long NO_VALUE
This value indicates that value is in not set.
Definition Rational.h:35
bool noValue() const
Indicates if Rational has no value.
Definition Rational.h:106
bool operator<=(const Rational &right) const
Definition Rational.h:157
bool operator<(const Rational &right) const
Definition Rational.h:142
std::string toString() const
string representation of Rational
long long numerator
Numerator.
Definition Rational.h:38
bool operator>=(const Rational &right) const
Definition Rational.h:162
bool operator!=(const Rational &right) const
Definition Rational.h:137
bool operator==(const Rational &right) const
Rational comparison.
Definition Rational.h:127
unsigned int denominator
Denominator.
Definition Rational.h:41
Rational()
Constructs default instance.
Definition Rational.h:88