DGtal  1.1.0
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;
}
DGtal::Z2i::Integer
DGtal::int32_t Integer
Definition: StdDefs.h:74
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition: ClosedIntegerHalfPlane.h:49
main
int main(int argc, char **argv)
Definition: testArithmeticDSS-benchmark.cpp:147
usage
void usage(int, char **argv)
Definition: approximation.cpp:64
DGtal::LighterSternBrocot
Aim: The Stern-Brocot tree is the tree of irreducible fractions. This class allows to construct it pr...
Definition: LighterSternBrocot.h:109
DGtal::int64_t
boost::int64_t int64_t
signed 94-bit integer.
Definition: BasicTypes.h:74