Thread: How to deal with repeating rational numbers

  1. #1
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56

    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.
    I am Error. When all else fails, use fire.

    My Current Screenshot

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  3. #3
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    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!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Program that prints numbers in columns
    By rayrayj52 in forum C++ Programming
    Replies: 12
    Last Post: 09-20-2004, 02:43 PM
  4. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM