OnixS ICE iMpact Multicast Price Feed Handler C++ library 8.18.0
API documentation
Loading...
Searching...
No Matches
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
27namespace OnixS { namespace ICE { namespace iMpact { namespace MarketData {
28
30struct ONIXS_ICEMDH_EXPORT Rational
31{
33 static const long long NO_VALUE;
34
36 long long numerator;
37
39 unsigned int denominator;
40
42 Rational();
43
45 Rational(long long numerator, unsigned int denominator);
46
48 Rational(const Rational& other, unsigned int newDenominator);
49
51 bool noValue() const;
52
62 template <typename T>
63 T convertTo() const;
64
66 operator float() const;
67
69 operator double() const;
70
72 std::string toString() const;
73
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
84ONIXS_ICEMDH_EXPORT std::ostream& operator<<(std::ostream& stream, const Rational& value);
85
88 , denominator(1)
89{
90}
91
92inline Rational::Rational(long long inNumerator, unsigned int inDenominator)
93 : numerator(inNumerator)
94 , denominator(inDenominator)
95{
96}
97
98inline 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
104inline bool Rational::noValue() const
105{
106 return numerator == NO_VALUE;
107}
108
109template <typename T>
110inline T Rational::convertTo() const
111{
112 return T(numerator) / T(denominator);
113}
114
115inline Rational::operator float() const
116{
117 return convertTo<float>();
118}
119
120inline Rational::operator double() const
121{
122 return convertTo<double>();
123}
124
125inline bool Rational::operator==(const Rational& right) const
126{
127 if (denominator == right.denominator)
128 {
129 return numerator == right.numerator;
130 }
131
132 return numerator * right.denominator == right.numerator * denominator;
133}
134
135inline bool Rational::operator!=(const Rational& right) const
136{
137 return !(*this == right);
138}
139
140inline bool Rational::operator<(const Rational& right) const
141{
142 if (denominator == right.denominator)
143 {
144 return numerator < right.numerator;
145 }
146
147 return numerator * right.denominator < right.numerator * denominator;
148}
149
150inline bool Rational::operator>(const Rational& right) const
151{
152 return right < *this;
153}
154
155inline bool Rational::operator<=(const Rational& right) const
156{
157 return !(right < *this);
158}
159
160inline bool Rational::operator>=(const Rational& right) const
161{
162 return !(*this < right);
163}
164
165}}}} // namespace OnixS::ICE::iMpact::MarketData
std::ostream & operator<<(std::ostream &, const Error &)
Make it printable to formatted C++ I/O streams.
Rational number representation.
Definition Rational.h:31
bool operator>(const Rational &right) const
Definition Rational.h:150
static const long long NO_VALUE
This value indicates that value is in not set.
Definition Rational.h:33
bool noValue() const
Indicates if Rational has no value.
Definition Rational.h:104
bool operator<=(const Rational &right) const
Definition Rational.h:155
bool operator<(const Rational &right) const
Definition Rational.h:140
std::string toString() const
string representation of Rational
bool operator>=(const Rational &right) const
Definition Rational.h:160
bool operator!=(const Rational &right) const
Definition Rational.h:135
bool operator==(const Rational &right) const
Rational comparison.
Definition Rational.h:125
unsigned int denominator
Denominator.
Definition Rational.h:39
Rational()
Constructs default instance.
Definition Rational.h:86