| | |
C++, issues with converting a decimal to fraction
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2007
Posts: 4
Reputation:
Solved Threads: 0
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:
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)
//example.cc #include <iostream> #include "reduce.h" #include "decimal-to-fraction.h" using namespace std; int main() { double deci; double n, d; while(1) { cout << "Enter a decimal to convert to a fraction ('0' to quit): "; cin >> deci; cin.ignore(INT_MAX, '\n'); if(!deci) exit(0); dec_to_frac(deci, n, d); if (n * d != n){ cout << '\n' << deci << " = " << n << '/' << d << "\n\n"; cout<<'\n' << d << " x " << n; n = d * n; cout<< " = " << n << "\n\n"; cout<<'\n' << n << '/' << d; reduce(n, d); cout << " = " << n << '/' << d << "\n\n"; cout<<'\n' << n << "\n\n"; } else cout<<'\n' << deci<< "\n\n"; } return 0; } #ifndef _REDUCE_H_ #error You must include "reduce.h" before this file #endif /* def _REDUCE_H_ */ #ifndef _DECIMAL_TO_FRACTION_H_ #define _DECIMAL_TO_FRACTION_H_ void dec_to_frac(double decimal, double &numerator, double &denominator) { //numerator = static_cast<int >(decimal); //numerator = (int )decimal; numerator = decimal; denominator = 1; while(numerator != (int)numerator) { numerator *= 10; denominator *= 10; } reduce(numerator, denominator); } #endif /* def _DECIMAL_TO_FRACTION_H_ */ #ifndef _REDUCE_H_ #define _REDUCE_H_ void reduce(double &numer, double &denom) { int i; for(i=2; i<=numer; ++i) { if( ((numer/i) == ((int )(numer/i))) && ((denom/i) == ((int)(denom/i))) ) { numer /= i; denom /= i; --i; } } } #endif /* def _REDUCE_H_ */
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.
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.
There are many ways to do this:
One is to use the fact that any fractional part can be represented by:-

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.
One is to use the fact that any fractional part can be represented by:-
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.
*Voted best profile in the world*
![]() |
Similar Threads
- Help With Decimal to Binary conversion (IT Professionals' Lounge)
- converting a float to a fraction. (C++)
- decimal & fraction (Java)
- calling a decimal function (C++)
- How do I Take in Hexidecimal input out put binary, decimal, and octal (Java)
Other Threads in the C++ Forum
- Previous Thread: VC++ Toolbox
- Next Thread: User define 2D array
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






