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...
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...
- Write a long division algorithm.
- 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)
- Once a remainder part repeats, there you go.
I'm assuming you know how to read in the fraction as a fraction.
ok... but how can i divide repeating decimals that accurate? what variable do i need to declare?
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.
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.
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.
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...
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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.
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.