944,088 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 7544
  • C++ RSS
Jun 2nd, 2007
0

C++, issues with converting a decimal to fraction

Expand Post »
Hi all,

I am working on a C++ program that converts a decimal to a fraction, finds the lowest common multiple, multiplies the numerator by the denominator, then reduces the fraction again so the fraction will be 'numerator/1'.

I am using this code in a larger program to change a floating point slope value to a whole value for slope-intercept equations and standard form equations.

The program will run but I tested almost 500 numbers and 20 numbers made the program crash (it stalled and did not finish the equation, I had to close the terminal and open it up again... )

These 20 numbers I had problems with:

2.36, 7.36, 1.11, 1.001, 1.66, 1.88, 2.22, 2.13, 3.24, 3,26, 3.32, 3.43, 3.49, 3.51, 3.57, 3.68, 3.74, 3.76, 3.82

(I am sure there are more...)

I have tried c-style casting and dynamic and static casting to try to resolve this issue. Can someone help me out and point out why these certain numbers make the program crash and offer a possible sloution?

I tried debugging this program on gdb and keep on getting this frustrating message..

(gdb) break 16
No line 16 in file "init.c".
(gdb)


Here is the program's code:

C++ Syntax (Toggle Plain Text)
  1.  
  2. //example.cc
  3. #include <iostream>
  4. #include "reduce.h"
  5. #include "decimal-to-fraction.h"
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. double deci;
  11. double n, d;
  12.  
  13. while(1) {
  14. cout << "Enter a decimal to convert to a fraction ('0' to quit): ";
  15. cin >> deci;
  16. cin.ignore(INT_MAX, '\n');
  17. if(!deci) exit(0);
  18.  
  19. dec_to_frac(deci, n, d);
  20.  
  21. if (n * d != n){
  22. cout << '\n' << deci << " = " << n << '/' << d << "\n\n";
  23. cout<<'\n' << d << " x " << n;
  24. n = d * n;
  25. cout<< " = " << n << "\n\n";
  26. cout<<'\n' << n << '/' << d;
  27. reduce(n, d);
  28.  
  29. cout << " = " << n << '/' << d << "\n\n";
  30.  
  31. cout<<'\n' << n << "\n\n";
  32. }
  33.  
  34. else
  35. cout<<'\n' << deci<< "\n\n";
  36.  
  37. }
  38.  
  39. return 0;
  40. }
  41.  
  42. #ifndef _REDUCE_H_
  43.  
  44. #error You must include "reduce.h" before this file
  45.  
  46. #endif /* def _REDUCE_H_ */
  47.  
  48.  
  49.  
  50. #ifndef _DECIMAL_TO_FRACTION_H_
  51.  
  52. #define _DECIMAL_TO_FRACTION_H_
  53.  
  54.  
  55.  
  56. void dec_to_frac(double decimal, double &numerator, double &denominator)
  57.  
  58. {
  59.  
  60. //numerator = static_cast<int >(decimal);
  61. //numerator = (int )decimal;
  62. numerator = decimal;
  63.  
  64. denominator = 1;
  65.  
  66.  
  67.  
  68. while(numerator != (int)numerator) {
  69.  
  70. numerator *= 10;
  71.  
  72. denominator *= 10;
  73.  
  74. }
  75.  
  76. reduce(numerator, denominator);
  77.  
  78. }
  79.  
  80.  
  81.  
  82. #endif /* def _DECIMAL_TO_FRACTION_H_ */
  83.  
  84. #ifndef _REDUCE_H_
  85.  
  86. #define _REDUCE_H_
  87.  
  88.  
  89.  
  90. void reduce(double &numer, double &denom)
  91.  
  92. {
  93.  
  94. int i;
  95.  
  96.  
  97.  
  98. for(i=2; i<=numer; ++i) {
  99.  
  100.  
  101. if( ((numer/i) == ((int )(numer/i))) && ((denom/i) == ((int)(denom/i))) ) {
  102.  
  103. numer /= i;
  104.  
  105. denom /= i;
  106.  
  107. --i;
  108.  
  109. }
  110.  
  111. }
  112.  
  113. }
  114.  
  115.  
  116.  
  117. #endif /* def _REDUCE_H_ */
Attached Files
File Type: zip reduce.zip (9.6 KB, 68 views)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
alpha_gamma is offline Offline
4 posts
since Jun 2007
Jun 2nd, 2007
1

Re: C++, issues with converting a decimal to fraction

You should post ALL your custom includes as another "code" entry in text, just like the main program.
Nobody likes to open attachments...

Did you try removing the breakpoint in question?
sometimes it's just as easy or easier to insert a "test cout" that reports the state of variables in a non-cooperative program.
JRM
Reputation Points: 130
Solved Threads: 75
Practically a Master Poster
JRM is offline Offline
618 posts
since Oct 2006
Jun 2nd, 2007
0

Re: C++, issues with converting a decimal to fraction

There are many ways to do this:

One is to use the fact that any fractional part can be represented by:-

x = a_0 + \frac{1}{a_1 + \frac{1}{a_2 + \frac{1}{a_3 + \frac{1}{\ddots\,}}}}

where a0 is some integer and all the other numbers an are positive integers. (continued fractions)

This elegant idea is described quite nicely here

The other way is to look at it is as follows. Count the number of digits after the decimal place. 0.345 In this case it is three digits after the decimal place. Let the number of zeros after the decimal place be the number you divide by with a one in front of it i.e = 1000 (three zeros) so you can apply Euclids GCD algo on 345/1000 .

1.93 is two (100). So GCD(93/100) etc.

0.04 is two. So GCD(4/100) etc.
Last edited by iamthwee; Jun 2nd, 2007 at 5:07 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Visual C++ 2005 Express Edition
Next Thread in C++ Forum Timeline: User define 2D array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC