Converting decimal to fraction, infinite loop.

I have written this program a million times in TI-BASIC. It takes a decimal number and converts it to a simplified fraction. I've been trying to rewrite it in C++ but the function always gets stuck in an infinite loop.

header.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>
#include<unistd.h>
#include<termios.h>
#include<string>
#include<sstream>

//System common functions
std::string dec2frac(long double dec){
unsigned long long int a = 1;
unsigned long long int b = 1;
std::stringstream streamfrac;
std::string frac;
while((a/b) != dec)
	{std::cout<<a<<","<<b<<"\n";
	if((a/b) < dec)(a = a +1);
	if((a/b) > dec)(b = b +1);
	if(b <= 0){std::cout<< "Program has been caught in an infinite loop. This is probably caused by the machine running out of memory. The task of converting a decimal to a fraction has been left incomplete, in an attempt to recover from the breakage.";return "0/1";}}
streamfrac<< a <<"/" << b;
return streamfrac.str();
}


//processors
class PrincessMonsterTruck {
private:
std::string addition(std::string one, std::string two){

}
std::string multiplication(){}
};


a.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include"header.h"
#include<unistd.h>
#include<termios.h>
#include<cstdio>

using namespace std;

char getch(){
    /*#include <unistd.h>   //_getch*/
    /*#include <termios.h>  //_getch*/
    char buf=0;
    struct termios old={0};
    fflush(stdout);
    if(tcgetattr(0, &old)<0)
        perror("tcsetattr()");
    old.c_lflag&=~ICANON;
    old.c_lflag&=~ECHO;
    old.c_cc[VMIN]=1;
    old.c_cc[VTIME]=0;
    if(tcsetattr(0, TCSANOW, &old)<0)
        perror("tcsetattr ICANON");
    if(read(0,&buf,1)<0)
        perror("read()");
    old.c_lflag|=ICANON;
    old.c_lflag|=ECHO;
    if(tcsetattr(0, TCSADRAIN, &old)<0)
        perror ("tcsetattr ~ICANON");
    return buf;
 }

int main(){
cout<< dec2frac(0.5);
getch();
}
Last edited on
Typical output:

15896,15897
15897,15898
15898,15899
15899,15900
15900,15901
15901,15902
15902,15903
15903,15904
15904,15905
15905,15906
15906,15907
15907,15908
15908,15909
15909,15910
15910,15911
15911,15912
15912,15913
15913,15914
15914,15915
15915,15916
15916,15917
15917,15918
15918,15919
15919,15920
15920,15921
15921,15922
15922,15923
15923,15924
15924,15925
15925,15926
15926,15927
15927,15928
15928,15929
15929,15930
15930,15931
15931,15932
15932,15933
15933,15934
15934,15935
15935,15936
15936,15937
15937,15938
15938,15939
15939,15940
15940,15941
15941,15942
15942,15943
15943,15944
15944,15945
15945,15946
15946,15947
15947,15948
15948,15949
15949,15950
15950,15951
15951,15952
15952,15953
15953,15954
15954,15955
15955,15956
15956,15957
15957,15958
15958,15959
15959,15960
15960,15961
15961,15962
15962,15963
15963,15964
15964,15965
Last edited on
Note that when you divide two integers the result will be an integer (the fractional part is lost).

Calculation with floating point number are not always exact. Not all values can be stored exactly as floating point number and there can be rounding errors. This means you should normally not rely on == and != when comparing floating point values.
Fixed it, thanks.
Topic archived. No new replies allowed.