aGrUM  0.14.2
rational.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN and Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
27 #ifndef GUM_RATIONAL_H
28 #define GUM_RATIONAL_H
29 
30 #include <iomanip>
31 #include <iostream>
32 #include <sstream>
33 #include <stdlib.h>
34 #include <vector>
35 #include <agrum/core/math/math.h>
36 
37 // 64 bits for windows (long is 32 bits)
38 #ifdef _MSC_VER
39 typedef __int64 int64_t;
40 typedef unsigned __int64 uint64_t;
41 #else
42 # include <stdint.h>
43 #endif
44 
45 namespace gum {
46 
56  template < typename GUM_SCALAR >
57  class Rational {
58  public:
59  // ========================================================================
61  // ========================================================================
63 
81  static void farey(int64_t& numerator,
82  int64_t& denominator,
83  const GUM_SCALAR& number,
84  const int64_t& den_max = 1000000L,
85  const GUM_SCALAR& zero = 1e-6);
86 
108  static void continuedFracFirst(int64_t& numerator,
109  int64_t& denominator,
110  const GUM_SCALAR& number,
111  const double& zero = 1e-6);
112 
129  static void continuedFracBest(int64_t& numerator,
130  int64_t& denominator,
131  const GUM_SCALAR& number,
132  const int64_t& den_max = 1000000);
133 
135  };
136 
137 } // namespace gum
138 
139 
140 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
141 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
142 extern template class gum::Rational< double >;
143 # endif
144 #endif
145 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
146 # ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
147 extern template class gum::Rational< long double >;
148 # endif
149 #endif
150 
151 
152 // Always include template implementation in header file
154 
155 #endif // GUM_RATIONAL_H
Useful macros for maths.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
static void continuedFracBest(int64_t &numerator, int64_t &denominator, const GUM_SCALAR &number, const int64_t &den_max=1000000)
Find the best rational approximation.
Definition: rational_tpl.h:190
Class template used to approximate decimal numbers by rationals.
Definition: rational.h:57
static void farey(int64_t &numerator, int64_t &denominator, const GUM_SCALAR &number, const int64_t &den_max=1000000L, const GUM_SCALAR &zero=1e-6)
Find the rational close enough to a given ( decimal ) number in [-1,1] and whose denominator is not h...
Definition: rational_tpl.h:34
static void continuedFracFirst(int64_t &numerator, int64_t &denominator, const GUM_SCALAR &number, const double &zero=1e-6)
Find the first best rational approximation.
Definition: rational_tpl.h:93
Class template used to approximate decimal numbers by rationals.