home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-08 | 1.4 KB | 75 lines | [TEXT/KAHL] |
- #include <math.h>
-
- #include <iostream.h>
- #include <iomanip.h>
-
- #include "continued_fraction.h"
-
- continued_fraction::continued_fraction( long double number) : x( number)
- {
- restart();
- }
-
- void continued_fraction::restart()
- {
- rest = x;
-
- p_2 = 0;
- p_1 = 1;
- q_2 = 1;
- q_1 = 0;
- }
-
- int continued_fraction::step( int numsteps)
- {
- int num_steps_taken = 0;
-
- while( (num_steps_taken < numsteps) && (step_once() == true))
- {
- num_steps_taken += 1;
- }
- compute_deriveds();
- return num_steps_taken;
- }
-
- Boolean continued_fraction::step_once()
- {
- if( rest != 0.0)
- {
- const unsigned long a_k = (const unsigned long)floor( rest);
- const unsigned long p_0 = a_k * p_1 + p_2;
- const unsigned long q_0 = a_k * q_1 + q_2;
-
- rest = 1.0 / (rest - (long double)a_k);
- p_2 = p_1;
- q_2 = q_1;
- p_1 = p_0;
- q_1 = q_0;
- }
- return (rest != 0.0);
- }
-
- long double continued_fraction::step_till_relerror( long double max_relerror)
- {
- while( (relerror > max_relerror) && step()){}
- return relerror;
- }
-
- long double continued_fraction::step_till_error( long double max_error)
- {
- while( (error > max_error) && step()){}
- return error;
- }
-
- ostream &operator<<( ostream &os, const continued_fraction &it)
- {
- static const int longWidth = 9;
- static const int doubleWidth = 10;
- os
- << setw( longWidth) << it.p_1 << " / " << setw( longWidth) << it.q_1
- << " = " << setw( doubleWidth) << it.fract
- << " (error " << it.error << "; " << it.relerror << " relative)";
-
- return os;
- }
-