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 gbLAB {
13 template<typename IntScalarType>
15 {
16 public:
17 double number;
18 double tolerance;
19 IntScalarType max_denominator;
20 std::vector<Rational<IntScalarType>> approximations;
21
23 /* init */ number(number),
25 /* init */ tolerance(tolerance)
26 {
27 double n=floor(number);
28 auto seq = farey(max_denominator,false);
29 for (auto p : seq) {
30 double approx = static_cast<double>(p.first) / static_cast<double>(p.second);
31 if (std::abs(number - n - approx) <= tolerance) {
32 p.first= p.first + n*p.second;
33 approximations.push_back(Rational<IntScalarType>(p.first,p.second));
34 }
35 }
36 }
37
38
39
40
41 };
42
43}
44#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:6