DGtal  0.9.3beta
arithmetic/fraction.cpp

Computes a fraction from its quotients.

See also
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;
}