DGtal
2.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
convergents-biginteger.cpp
Go to the documentation of this file.
1
39
42
#include <iostream>
43
#include "DGtal/arithmetic/LighterSternBrocot.h"
45
47
48
using namespace
DGtal
;
49
51
52
void
usage
(
int
,
char
** argv )
53
{
54
std::cerr <<
"Usage: "
<< argv[ 0 ] <<
" <p> <q>"
<< std::endl;
55
std::cerr <<
"\t - computes the successive convergent of the fraction p / q."
<< std::endl;
56
}
57
61
int
main
(
int
argc,
char
** argv )
62
{
63
if
( argc < 3 )
64
{
65
usage
( argc, argv );
66
return
1;
67
}
68
std::string inputP = argv[ 1 ];
69
std::string inputQ = argv[ 2 ];
70
72
typedef
BigInteger
Integer
;
73
typedef
DGtal::int64_t
Quotient;
74
typedef
LighterSternBrocot<Integer, Quotient, StdMapRebinder>
SB;
// the type of the Stern-Brocot tree
75
typedef
SB::Fraction Fraction;
// the type for fractions
76
typedef
Fraction::ConstIterator
ConstIterator
;
// the iterator type for visiting quotients
77
typedef
Fraction::Value
Value
;
// the value of the iterator, a pair (quotient,depth).
79
81
Integer
p( inputP );
82
Integer
q( inputQ );
83
Fraction f( p, q );
// fraction p/q
85
87
// Visit quotients u_k as pair (u_k,k)
88
std::cout <<
"z = "
;
89
ConstIterator
itbegin = f.begin(), itend = f.end();
90
for
(
ConstIterator
it = itbegin; it != itend; ++it )
91
{
92
Value
u = *it;
93
std::cout << ( ( it == itbegin ) ?
"["
:
","
)
94
<< u.first;
95
}
96
std::cout <<
"]"
<< std::endl;
98
100
Fraction
g
;
// fraction null, 0/0, invalid
101
for
(
ConstIterator
it = itbegin; it != itend; ++it )
102
{
103
Value
u = *it;
104
std::cout <<
"z_"
<< u.second <<
" = "
;
105
g
.push_back( u );
// add (u_i,i) to existing fractions
106
std::cout <<
g
.p() <<
" / "
<<
g
.q() << std::endl;
107
}
109
return
0;
110
}
DGtal::ConstIterator
DGtal::LighterSternBrocot
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
Definition
LighterSternBrocot.h:109
Integer
Point::Coordinate Integer
Definition
examplePlaneProbingParallelepipedEstimator.cpp:44
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition
ClosedIntegerHalfPlane.h:49
DGtal::int64_t
std::int64_t int64_t
signed 94-bit integer.
Definition
BasicTypes.h:73
DGtal::BigInteger
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>, boost::multiprecision::et_off > BigInteger
Definition
BasicTypes.h:75
DGtal::Value
g
std::mt19937 g(rd())
main
int main(int, char **)
Definition
testIntegerComputer.cpp:331
usage
void usage(int, char **argv)
Definition
testPolynomial.cpp:58
examples
arithmetic
convergents-biginteger.cpp
Generated on
for DGtal by
1.18.0