20 #include "DGtal/arithmetic/LighterSternBrocot.h"
21 #include "DGtal/base/StdRebinders.h"
26 using namespace DGtal;
30 void usage(
int,
char** argv )
32 std::cerr <<
"Usage: " << argv[ 0 ] <<
" <floating point number>" << std::endl;
33 std::cerr <<
"\t - computes the successive rational approximation of this number, up to a precision of 1e-14." << std::endl;
39 int main(
int argc,
char** argv )
51 typedef SB::Fraction Fraction;
52 typedef Fraction::ConstIterator ConstIterator;
53 typedef Fraction::Value Value;
54 typedef std::back_insert_iterator< Fraction > OutputIterator;
58 long double epsilon = 1e-14;
59 long double number0 = strtold( argv[ 1 ], 0 );
60 long double number = number0;
61 ASSERT( number >= 0.0 );
63 OutputIterator itback = std::back_inserter( f );
67 long double int_part = floorl( number );
69 *itback++ = std::make_pair( u, i++ );
73 std::cout <<
"z = " << f.p() <<
" / " << f.q()
74 <<
" =~ " << setprecision( 16 ) << approx << std::endl;
76 if ( ( (number0 - epsilon ) < approx )
77 && ( approx < (number0 + epsilon ) ) )
break;
78 number = 1.0 / number;
80 std::cout <<
"z = " << f.p() <<
" / " << f.q() << std::endl;