PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Rational.h
Go to the documentation of this file.
1
10
11#ifndef PBAT_MATH_RATIONAL_H
12#define PBAT_MATH_RATIONAL_H
13
14#include "PhysicsBasedAnimationToolkitExport.h"
15
16#include <cstdint>
17#include <pbat/Aliases.h>
18#include <tuple>
19
20namespace pbat {
21namespace math {
22
28struct PBAT_API Rational
29{
34 Rational();
42 template <std::integral Integer>
43 Rational(Integer _a, Integer _b)
44 : a(static_cast<std::int64_t>(_a)), b(static_cast<std::int64_t>(_b))
45 {
46 }
47
54 template <std::integral Integer>
55 Rational(Integer value) : a(static_cast<std::int64_t>(value)), b(1)
56 {
57 }
58
63 Rational operator+(Rational const& other) const;
69 Rational operator-(Rational const& other) const;
74 Rational operator-() const;
80 Rational operator*(Rational const& other) const;
86 Rational operator/(Rational const& other) const;
92 bool operator==(Rational const& other) const;
98 bool operator<(Rational const& other) const;
105 bool Rebase(std::int64_t denominator);
111 explicit operator double() const;
117 explicit operator float() const;
121 void simplify();
122
123 std::int64_t a;
124 std::int64_t b;
125};
126
135template <std::integral Integer>
136inline Rational operator-(Integer a, Rational const& b)
137{
138 return (-b) + a;
139}
140
149template <std::integral Integer>
150inline Rational operator+(Integer a, Rational const& b)
151{
152 return b + a;
153}
154
163template <std::integral Integer>
164inline Rational operator*(Integer a, Rational const& b)
165{
166 return b * a;
167}
168
177template <std::integral Integer>
178inline Rational operator/(Integer a, Rational const& b)
179{
180 return Rational{a} / b;
181}
182
183} // namespace math
184} // namespace pbat
185
186#endif // PBAT_MATH_RATIONAL_H
Math related functionality.
Definition Concepts.h:19
Rational operator-(Integer a, Rational const &b)
Subtraction operation between Rational and integral type.
Definition Rational.h:136
Rational operator*(Integer a, Rational const &b)
Multiplication operation between Rational and integral type.
Definition Rational.h:164
Rational operator/(Integer a, Rational const &b)
Division operation between Rational and integral type.
Definition Rational.h:178
Rational operator+(Integer a, Rational const &b)
Addition operation between Rational and integral type.
Definition Rational.h:150
The main namespace of the library.
Definition Aliases.h:15
Fixed size rational number using std::int64_t for numerator and denominator.
Definition Rational.h:29
Rational(Integer _a, Integer _b)
Construct a new Rational object.
Definition Rational.h:43
Rational()
Construct a new Rational object.
Definition Rational.cpp:11
std::int64_t a
Numerator.
Definition Rational.h:123
Rational(Integer value)
Construct a new Rational object.
Definition Rational.h:55
std::int64_t b
Denominator.
Definition Rational.h:124