#include <cstdlib>
#include <cmath>
#include <iostream>
#include <iomanip>
#include "DGtal/arithmetic/LighterSternBrocot.h"
#include "DGtal/base/StdRebinders.h"
void usage(
int,
char** argv )
{
std::cerr << "Usage: " << argv[ 0 ] << " <floating point number>" << std::endl;
std::cerr << "\t - computes the successive rational approximation of this number, up to a precision of 1e-14." << std::endl;
}
int main(
int argc,
char** argv )
{
if ( argc < 2 )
{
return 1;
}
typedef LighterSternBrocot<Integer, Quotient, StdMapRebinder> SB;
typedef SB::Fraction Fraction;
typedef std::back_insert_iterator< Fraction > OutputIterator;
long double epsilon = 1e-14;
long double number0 = strtold( argv[ 1 ], 0 );
long double number = number0;
ASSERT( number >= 0.0 );
Fraction f;
OutputIterator itback = std::back_inserter( f );
Quotient i = 0;
while ( true )
{
long double int_part = floorl( number );
*itback++ = std::make_pair( u, i++ );
long double approx =
std::cout << "z = " << f.p() << " / " << f.q()
<< " =~ " << std::setprecision( 16 ) << approx << std::endl;
number -= int_part;
if ( ( (number0 - epsilon ) < approx )
&& ( approx < (number0 + epsilon ) ) ) break;
number = 1.0 / number;
}
std::cout << "z = " << f.p() << " / " << f.q() << std::endl;
return 0;
}
void usage(int, char **argv)
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.
static DGtal::int64_t castToInt64_t(const std::decay< T >::type &aT)
Cast method to DGtal::int64_t (for I/O or board export uses only).
static double castToDouble(const std::decay< T >::type &aT)
Cast method to double (for I/O or board export uses only).
int main(int argc, char **argv)