Thread: Repeating Decimals

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    7

    Repeating Decimals

    how to display repeating decimals like this?

    5 / 7 = 0.714285...
    the repeating cycle is 714285
    and the cycle length is 6...

    how to display the repeating decimal and the cycle length? and assume that the input may vary...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. Write a long division algorithm.
    2. Keep hold of the digits -- better yet, keep hold of the remainder parts of the long division (you know, what you get after you subtract)
    3. Once a remainder part repeats, there you go.

    I'm assuming you know how to read in the fraction as a fraction.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    ok... but how can i divide repeating decimals that accurate? what variable do i need to declare?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What have you got so far? We do not write code for you, but we will help you get your code right...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    i think the program is running smoothly except for the fact that it divides inaccurately... the repeating decimal is true until the 6th decimal digit (when i use float) and 15th (when i use double) and afterward, it was all false numbers...

    i tried to convert the decimals to whole numbers so that i can used modulo to them... and to distinguish what are the repeating numbers...

    i've converted these decimals into whole numbers, the float can make it right to int but when i use double, the result is an endless loop (even i used long)...
    Last edited by jaypeebee18; 08-02-2008 at 11:17 PM.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jaypeebee18 View Post
    but how can i divide repeating decimals that accurate? what variable do i need to declare?
    I assume you mean you have a floating point value, and you want to print it out as a recurring fraction. You can't do that. Floating point variables have finite precision, and therefore do not cycle forever. So there is no "variable to declare". You simply can't do this if all you have is a floating point value, because a floating point value is usually only an approximation.

    The approach suggested by tabstop will work, assuming you start with the integers you are dividing. There is a basic mathematical fact that, if a and b are positive integers, then a/b will have a repeating cycle that is less than or equal to b in length.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    thanks for the fact...and im getting some accuracy because of that fact but still, i cant divide 11 / 13... i always get a garbage value (-214748...)

    5 / 7 is accurate though...

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you have a bug when numbers of more than one digit are involved. You might want to post the smallest and simplest compilable program that demonstrates the bug.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    The bug is in the approach, which seems to be simply to do the calculation, then sprintf the result into a buffer and look for the pattern.

    Sure, this will work up to a point (the easy cases), but it's broken-as-designed for anything larger.

    Grumpy and tabstop are on the right track though. The OP needs to read up on some number theory to determine the correct approach for any given pair of numbers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    when i tried to divide something by 11 or more it results to -214748...
    even 11 / 11 results to that...
    Last edited by jaypeebee18; 08-03-2008 at 12:31 AM.

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    and also, can i do this w/o using arrays?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jaypeebee18 View Post
    and also, can i do this w/o using arrays?
    Sort of -- I would think of using linked lists, but that's even worse from your point of view, probably.

    But you're going to need to store the digits as you divide them, somewhere; you're going to need to store the remainders (since the repeating cycle is based on the remainders more than the digits), somewhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help to show decimals
    By engstudent363 in forum C Programming
    Replies: 4
    Last Post: 02-19-2008, 04:13 PM
  2. allow decimals
    By dankassdann in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2006, 06:41 PM
  3. How to deal with repeating rational numbers
    By carrja99 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 11:33 AM
  4. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 12:58 PM
  5. Help me for repeating decimal numbers plz
    By Supra in forum C Programming
    Replies: 3
    Last Post: 09-27-2001, 03:18 PM