Page 6 of 6

Re: 332 why RTE please help me

Posted: Sun Jan 18, 2009 4:45 am
by mf
double c = a*x; // c = 507462687
Here's the first error. x is not equal to the mathematical constant 0.507462687, because double can't represent it.
x represents the value 0.507462687 +/- eps, where eps is some value of the order of about 1e-15 (double's precision). And as a result of that, when you multiply x by 1e10, you don't get the exact integer 507462687. Instead you get c = 507462687 +/- eps2, where eps2 is now about 0.00001.

Same thing for variable d - it's the integer you want it to be, plus or minus an epsilon of about 0.00001.

Next, the following statement:
long long num = (long long) (c-d);
tells the computer to find the difference between c and d, round it down to the lower integer, and store it in variable num. But because c and d are not integers, c-d most likely also will not be an integer. It could end up being something like 418.999999, which after cast to long long will become 418, and not 419 as you might have hoped.

I hope this sheds some light on this mystery with rounding. Here are a couple of pages for further reading, if you're interested - [1], [2].

Re: 332 - Rational Numbers from Repeating Fractions

Posted: Wed Mar 18, 2009 6:32 am
by toru
Hi !!, Can anyone plzzzz show me how to get 11/20 for the input " 0 0.55 ". as here i m getting 0 for both the upper value and lower value...... then how can i proceed??????? I m not getting it..................Thanks in advance.


Re: 332 - Rational Numbers from Repeating Fractions

Posted: Sat May 23, 2009 1:51 am
by Jehad Uddin
hello everyone i m getting frustrated with this prob,can any one tell me whats the wrong with this code

Code: Select all

long long int gcd(long long int a,long long int b)
	if(a<b) swap(a,b);
	return ((b==0)?a:gcd(b,a%b));

long long int power(long long int a,long long int b)
 long long int i=1,ans=1;
 return  ans;

long double count(char *str)
 long long int i=0,l;
 long double a=0;
 return a;
int main()
 char str[15];
 long double a,p,q,r;
 long long int j,k,x,y,z,m,n=0;
  if(j==-1) break;
  z=(long long int)p-(long long int)r;
  m=(long long int)(q);
 cout<<"Case "<<n<<": "<<z<<"/"<<m<<endl;
 return 0;

Re: 332 why RTE please help me

Posted: Sun Feb 21, 2010 4:08 pm
by meghla
after 9 RTE i,ve got accetped;
i think ,when the input is 3 0.0000 then calculating GCD code terminates;
so it causes RTE..................................

Re: 332 - Rational Numbers from Repeating Fractions

Posted: Wed Mar 30, 2011 6:01 pm
by Bella Swan
View solution here which does not contain any floating point process ... ating.html

332 getting wrong answer Help please.

Posted: Fri Jul 08, 2011 10:24 pm
by Yousuf

Re: 332 - Rational Numbers from Repeating Fractions

Posted: Sun Aug 28, 2011 8:44 pm
by plamplam
I really like this problem(May be because I got AC in first try :P). You can solve this very simply. I'll explain with an example.
Consider the input 2 0.318.

Here, j = 2, and k = 1. You can approximate X to be 0.318 and use it in the formula but there is a better way. Take the double number as string(You can always convert it to double from string easily with atof). As j = 2, so the digits 18 will be repeated after 0.318. Now use strcat to add the repeating digits(here 18) to the original string 5-6 times and there you go. An approximation for x :wink:. Now use atof to convert the string to a double value and just apply the formula. Just be careful about cases where j can be 0. And also try to make your own power function as advised by Jan. To convert from double to int just use the round function in cpp to avoid precision error. (e.g 5 calculated as 4.999999987 and a statement like int = double value may result in a precision error. Instead use int value = round(double value)).

Re: 332 - Rational Numbers from Repeating Fractions

Posted: Fri Jan 10, 2014 12:51 pm
by Mukit Chowdhury
No need of double/floating type ... :)

Re: 332 - Rational Numbers from Repeating Fractions

Posted: Sun Mar 22, 2015 4:48 pm
by coder.tanvir
i think working with double for this problem is so risky.