oILAB
Loading...
Searching...
No Matches
RationalApproximations.h
Go to the documentation of this file.
1//
2// Created by Nikhil Chandra Admal on 4/20/23.
3//
4
5#ifndef OILAB_RATIONALAPPROXIMATIONS_H
6#define OILAB_RATIONALAPPROXIMATIONS_H
7
8#include "IntegerMath.h"
9#include "Rational.h"
10#include "Farey.h"
11
12namespace oILAB {
13template <typename IntScalarType> class RationalApproximations {
14public:
15 double number;
16 double tolerance;
17 IntScalarType max_denominator;
18 std::vector<Rational<IntScalarType>> approximations;
19
21 : /* init */ number(number),
23 /* init */ tolerance(tolerance) {
24 double n = floor(number);
25 auto seq = farey(max_denominator, false);
26 for (auto p : seq) {
27 double approx =
28 static_cast<double>(p.first) / static_cast<double>(p.second);
29 if (std::abs(number - n - approx) <= tolerance) {
30 p.first = p.first + n * p.second;
31 approximations.push_back(Rational<IntScalarType>(p.first, p.second));
32 }
33 }
34 }
35
36
37
38
39 };
40
41 } // namespace oILAB
42#endif //OILAB_RATIONALAPPROXIMATIONS_H
std::vector< Rational< IntScalarType > > approximations
RationalApproximations(double number, int max_denominator, double tolerance)
std::vector< std::pair< int, int > > farey(int limit, const bool firstQuadrant)
Definition Farey.cpp:5