# How to deal with repeating rational numbers

• 10-21-2002
carrja99
How to deal with repeating rational numbers
Okay, I feel like this is a pretty STUPID question, but the way to do it has just not came to me at all. I wrote a program that converts any number to a selected base, including rational numbers and negatives (unbiased of course). The dilemma I am facing, however, is when I have repeating numbers. for example, when I convert a number to binary and get something like 101.1100110011001100110011001100. I want to fina a conventional way to trim the output so that it only prints unique digits after the decimal point. DO NOT givd me C++ code, but some form of psuedo code or perhaps just a suggestion to point me in the right direction. My first idea was put the numbers in an array, then search the array and compare values until it finds a repeating pattern. This is however a little consuming, and causes alot of bloat in my source. Any sugestions would be apperciated... even if it is a simple predefined function included in C++ that does htis for me.
• 10-21-2002
Here's an off the cuff approach. might work, might not.

determine how far out you want to look for a repeat first. If it is indefinite, you'll need a super computer, not a desk top.

convert number into string.

compare string[0] to each subsequent char in the string.

if string[0] repeats in the string at all note the position of the repeat by storing it in variable called r.

now compare each char beyond string[0] and string[r] using a loop. If each char between 0 and r doesn't repeat after r then it is not a repeating pattern.

advance to string[1] and repeat process.

continue repeating until every char evaluated up to the limit you set.
• 10-21-2002
correlcj
Quote:

trim the output so that it only prints unique digits after the decimal point
I believe setprecision() is what you want. an example would be
101.0000111111 and you want it 101.00, use setprecision(2).
You want 101.00001, then use setprecision(5). got it!
Hope this helps!
;)