DGtal 1.3.0
Searching...
No Matches
arithmetic/fraction.cpp

Computes a fraction from its quotients.

Fractions are back insertable
# More precise approximation of pi
\$ ./examples/arithmetic/fraction 3 7 15 1 292 1 1 1 2 1 3 1 14
z = 80143857 / 25510582

#include <iostream>
#include "DGtal/arithmetic/LighterSternBrocot.h"
using namespace DGtal;
void usage( int, char** argv )
{
std::cerr << "Usage: " << argv[ 0 ] << " <u_0> <u_1> ... <u_k>" << std::endl;
std::cerr << "\t - computes the fraction [u_0; u_1, ..., u_k] from its partial quotients." << std::endl;
}
int main( int argc, char** argv )
{
if ( argc < 3 )
{
usage( argc, argv );
return 1;
}
typedef DGtal::int64_t Quotient;
typedef LighterSternBrocot<Integer, Quotient, StdMapRebinder> SB; // the type of the Stern-Brocot tree
typedef SB::Fraction Fraction; // the type for fractions
typedef std::back_insert_iterator< Fraction > OutputIterator;
Fraction f;
OutputIterator itback = std::back_inserter( f );
for ( Quotient i = 1; i < argc; ++i)
{
Quotient u = atoll( argv[ i ] );
*itback++ = std::make_pair( u, i-1 );
}
std::cout << "z = " << f.p() << " / " << f.q() << std::endl;
return 0;
}
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
Point::Coordinate Integer
DGtal is the top-level namespace which contains all DGtal functions and types.
boost::int64_t int64_t
signed 94-bit integer.
Definition: BasicTypes.h:74
int main()
Definition: testBits.cpp:56