332 - Rational Numbers from Repeating Fractions

All about problems in Volume 3. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

sakhassan
Experienced poster
Posts: 105
Joined: Sat Mar 11, 2006 9:42 am
Location: cse,DU

332 All the time WA

Post by sakhassan » Sun Jun 25, 2006 11:37 am

Why am I getting WA all the time ??? :( I didnt find any prob with my code...Heres it is
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>

int bad(char str[20],int n)
{
int len;

len = strlen(str);
//if(len)
return len-2-n;
//else
//return 1;
}

long int gcd(long int a,long int b)
{
if(b==0)
return a;
else return gcd(b,a%b);
}

int main()
{

int i;
char str[20];
int j,k;
double x,l;
long int a,b,c,d;
long int m;

//freopen("in1.txt","r",stdin);
//freopen("out.txt","w",stdout);


i=0;
while(1)
{

scanf("%d",&j);
//j = atoi(str);
if(j==-1)
break;
scanf("%s",str);

k = bad(str,j);
x = atof(str);
a = ceil( (pow(10,k+j)*x) );
b = floor(pow(10,k)*x);
c = (pow(10,k+j));
d = (pow(10,k));

if(j)
{
a = a - b;
b = c - d;

}
else if(str[0]!='0')
{
a = 0;
b = str[0]-'0';
}
else
{
a = a;
b = c;
}
m = gcd(a,b);

if(m)printf("Case %d: %ld/%ld\n",i+1,a/m,b/m);

i++;
}

return 0;
}

mohsincsedu
Learning poster
Posts: 63
Joined: Tue Sep 20, 2005 12:31 am
Location: Dhaka
Contact:

332 why WA???????

Post by mohsincsedu » Sun Aug 20, 2006 6:16 pm

What's the problem in my Code???
Precession Error????

Here is my Code:

Code: Select all

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

long gcd(long a,long b)
{
	if(a%b==0)
		return b;
	gcd(b,a%b);
}

int main()
{
	char in[20];
	int k,j,cas=1;
	long double a,r;
	long res1,res2,len;
	//freopen("332.in","r",stdin);
	while(scanf("%d",&j)==1)
	{
		if(j==-1)
			break;
		scanf("%s",in);
		k = strlen(in) - 2 - j;
		a = atof(in);
		if(j!=0)
		{
			
			res1 = ceil((pow(10,k+j)-pow(10,k))*a);
			
			res2 = pow(10,k+j) - pow(10,k);			
			len = gcd(res1,res2);			
			printf("Case %d: %ld/%ld\n",cas++,res1/len,res2/len);			
		}
		else
		{
			res1 = ceil(pow(10,k)*a);
			res2 = pow(10,k);
			len = gcd(res1,res2);
			printf("Case %d: %ld/%ld\n",cas++,res1/len,res2/len);
		}
		
			
	}
}




Thanks in Advance
Amra korbo joy akhdin............................

mohsincsedu
Learning poster
Posts: 63
Joined: Tue Sep 20, 2005 12:31 am
Location: Dhaka
Contact:

Why WA???

Post by mohsincsedu » Mon Aug 21, 2006 11:26 pm

My program gives same output that described above

But what 's problem???

Code: Select all

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

long gcd(long a,long b)
{
	if(a%b==0)
		return b;
	gcd(b,a%b);
}

int main()
{
	char in[20];
	int k,j,cas=1;
	long double a,r;
	long res1,res2,len;
//	freopen("332.in","r",stdin);
	while(scanf("%d",&j)==1)
	{
		if(j==-1)
			break;
		scanf("%s",in);
		k = strlen(in) - 2 - j;
		a = atof(in);
		if(j!=0)
		{
			if(k==0)
				res1 = pow(10,j)*a;
			else
				res1 = ceil((pow(10,k+j)-pow(10,k))*a);
			
			res2 = pow(10,k+j) - pow(10,k);			
			len = gcd(res1,res2);			
			printf("Case %d: %ld/%ld\n",cas++,res1/len,res2/len);			
		}
		else
		{
			res1 = pow(10,k)*a;
			res2 = pow(10,k);
			len = gcd(res1,res2);
			printf("Case %d: %ld/%ld\n",cas++,res1/len,res2/len);
		}
		
			
	}
}

All the time i got WA. Why??????????????


What's the tricky input???
Amra korbo joy akhdin............................

User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Post by sohel » Tue Aug 22, 2006 4:14 am

mohsincsedu wrote: My program gives same output that described above
And what do you mean by above?? There seems to be nothing up there!!

Which problem are you talking about?
From the code, it can be inferred that 332 is the problem number..
.. but you should mention the problem name and number in the title.

One other thing.. don't just post your code without any discussion..
.. and don't create a new thread if one already exists.

http://acm.uva.es/board/viewtopic.php?t ... 58465e9a89

you will find in/out here.

User avatar
cytmike
Learning poster
Posts: 95
Joined: Mon Apr 26, 2004 1:23 pm
Location: Hong Kong and United States
Contact:

Post by cytmike » Sat Nov 04, 2006 11:38 pm

little joey wrote:Hmm. Well my version of gcc compiles and runs atof() with doubles, but produces some weird results if I don't include <stdlib.h>. If I do, everything is ok. But maybe some C-guru (mf, krzysztof, misof) can explain that.
On my computer it works well without including cstdlib, but I'm getting WA on the judge... :evil: :evil:
After I strggule an hour, I include cstdlib and it's AC :evil: :evil: :evil: :evil: :evil:
Impossible is Nothing.

rhsumon
New poster
Posts: 48
Joined: Wed Aug 23, 2006 12:29 pm
Location: Dhaka

Post by rhsumon » Sat Sep 29, 2007 8:57 pm

I mean my code has no problem but there are some problem which i cannot see the test cases r working fine with my code,,,,
Plz chack someone my code is here:::::::::

Code: Select all

#include<stdio.h>
#include<string.h>
#include<math.h>

typedef long long iint;

int gcd(iint a,iint b)
{
	while (b > 0) {
		a = a % b;
		a ^= b;
		b ^= a;
		a ^= b;
	}
	return a;
}

int main()
{
	//freopen("332.out", "w", stdout);
	double X;
	iint nom,denom,i,sum,k,j,x1,x2,len,red,count,cas=1;
	char num[100];
	while(scanf("%lld",&j) == 1 && j != -1){
		scanf("%s",&num);
		len = strlen(num);
		count = 0;
		for(i=len-1; i>=0; i--){
			if(num[i] == '.')
				break;
			count++;
		}
		k = count - j;
		sum = 0;
		for(i=0; i<len; i++){
			if(num[i] != '.'){
				sum = sum*10 + num[i]-48;
			}
		}
		if(j != 0){
			X = sum/(pow(10,count));

			nom = (iint)(pow(10,k+j)*X) - (iint)(pow(10,k)*X);
			denom = (iint)pow(10,k+j) - (iint)pow(10,k);

			red = gcd(nom,denom);

			x1 = nom/red;
			x2 = denom/red;

			printf("Case %lld: %lld/%lld\n",cas++,x1,x2);
		}
		else {
			x1 = sum;
			x2 = (iint)pow(10,len-2);
			red = gcd(x1,x2);
			x1 /= red;
			x2 /= red;
			printf("Case %lld: %lld/%lld\n",cas++, x1, x2);
		}
	}
	return 0;
}


				
Plz chack someone.............plz

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Sun Sep 30, 2007 9:31 am

pow() returns double. And you are converting from double to long long. As I know this conversion is like

double -> int -> long long.

So, better to write a manual power function. Hope it helps.
Ami ekhono shopno dekhi...
HomePage

rhsumon
New poster
Posts: 48
Joined: Wed Aug 23, 2006 12:29 pm
Location: Dhaka

Post by rhsumon » Sun Sep 30, 2007 4:55 pm

i thought that and submitted that with my own pow() func but it still WA......

Would any one ans that what is the output of

Code: Select all

2 1.000000000 
is it 1/1 or 0/1 my code gives 1/1

plz describe any one about the ans plzzzzz........

sapnil
Experienced poster
Posts: 106
Joined: Thu Apr 26, 2007 2:40 pm
Location: CSE-SUST
Contact:

Post by sapnil » Mon Oct 08, 2007 12:56 pm

For input: 2 1.000000000
My acc code return: 0/1

Thanks
Keep posting
Sapnil

rajib_sust
New poster
Posts: 16
Joined: Sun Mar 02, 2008 10:34 am
Location: SUST , Sylhet, Bangladesh

help ::332 why WA please help me

Post by rajib_sust » Mon Aug 18, 2008 8:50 pm

Code: Select all

ACC
thanks for help
life is beautiful like coding

turcse143
Learning poster
Posts: 81
Joined: Wed May 09, 2007 9:59 pm
Location: (CSE,DU) Dhaka,Bangladesh

Re: 332 why RTE please help me

Post by turcse143 » Sun Sep 14, 2008 5:26 pm

Here is my code:
I got wA i don't know whats my problem. ples help me.

Code: Select all

#include<stdio.h>
#include<math.h>
#include<string.h>

int gcd(int a,int b)
{
	int c;
	if(b>a)
	{
		c=a;
		a=b;
		b=c;
	}
	if(b==0)
		return a;
	else
		return gcd(b,a%b);
}
int main()
{
	int Case,j,i,len,val,d,k,val2,a,b,c,r,m;
	double e,f,val1,p,q;
	char str[15];
	Case=1;
	freopen("332.in","rt",stdin);
	while(scanf("%d",&j)==1)
	{
		if(j==-1)
			break;
		scanf("%s",str);
		len=strlen(str);
		val=0;
		for(i=2;i<len;i++)
			val=val*10+(str[i]-'0');
		m=val;
		d=0;
		if(j!=0)
		{
			for(i=len-j;i<len;i++)
				d=d*10+(str[i]-'0');
		}
		p=(double)d;
		q=(double)val;
		e=p/pow(10,j);
		f=q+e;
		k=len-2-j;
		val1=q/pow(10,j);
		val=(int)(f-val1);
		r=len-2;
		val2=(int)pow(10,r)-pow(10,k);
		b=gcd(val,val2);
		if(b==0)
		{
			a=m;
			if(a==0)
				c=1;
			else
				c=pow(10,r);
		}
		else
		{
			a=val/b;
			c=val2/b;
		}
		printf("Case %d: %d/%d\n",Case,a,c);
		Case++;
	}
	return 0;
}
''I want to be most laziest person in the world''

fR0D
New poster
Posts: 29
Joined: Mon Feb 11, 2008 5:59 am
Contact:

Re: 332 why RTE please help me

Post by fR0D » Wed Dec 10, 2008 12:35 pm

My AC Code different output from the one above for input

Code: Select all

2 0.318     
1 0.3
2 0.09
6 0.714285
6 0.714285000
1 0.9999
9 0.123456789
8 0.987654321  
9 0.574454131
5 0.83777471
1 0.22222222
9 0.111111111
9 0.222222222
9 0.333333333
9 0.444444444   
9 0.555555555
9 0.666666666
9 0.777777777
9 0.888888888
9 0.999999999
9 0.000000000
1 0.200
8 0.200000000
8 0.020000000
0 0.3
0 0.5
0 0.55
1 0.55  
6 0.142857
0    0.9
1 0.9
6 0.076923
0 0.678453453
0 0.1
2 0.31818
9 0.345678993
2   0.25
1 0.3
0 0.3
0 0.0
5 0.99999
6 0.714285
0 0.5
0 0.35
1 0.111111111
  2   0.111111111
3 0.111111111
5 0.132456454
6   0.456467879
9 0.456467879
7 0.456467879
1 0.414141414
9 0.414141414
1 0.999999999
1 0.507462687
0 0.347826087
3 0.935483871
4 0.111111111
0 0.166666667
3 0.604166667
3 0.642857143
1 0.606060606
1 0.258064516
4 0.666666667
3 0.973684211
3 0.353658537
1 0.454545455
4 0.068965517
0 0.077922078
3 0.244186047
0 0.333333333
0 0.376623377
3 0.123711340
1 0.677777778
1 0.820895522
0 0.418918919
2 1.000000000
1 0.363636364
0 0.076923077
2 0.649122807
2 0.638554217
0 1.000000000
3 0.238636364
2 0.130434783
0 0.191176471
0 0.681318681
0 0.169491525
4 0.770833333
3 0.431578947
2 0.549450549
1 0.270270270
1 0.437500000
4 0.809523810
1 0.641509434
4 0.473684211
0 0.306818182
2 0.301075269
3 0.084337349
1 0.588235294
3 0.642857143
1 0.686274510
0 0.387755102
1 0.030612245
4 0.181818182
4 0.022471910
0 0.913978495
3 0.264367816
4 0.062500000
0 0.310344828
0 0.979591837
1 0.908163265
4 0.791666667
2 0.413043478
3 0.877777778
2 0.637362637
0 0.636363636
1 0.058823529
2 0.509090909
1 0.720930233
0 0.478260870
2 0.363636364
1 0.224489796
1 0.212121212
2 0.826086957
4 0.160000000
0 0.300000000
4 0.693181818
1 0.617977528
3 0.023529412
2 0.965909091
1 0.298245614
2 0.463768116
4 0.235955056
1 0.500000000
4 0.791666667
6 0.666666667
0 0.111111111
1 0.407407407
3 0.933333333
3 0.878048780
4 0.666666667
4 0.656250000
6 0.166666667
1 0.604166667
7 0.117647059
4 0.400000000
0 0.258064516
0 0.666666667
3 0.973684211
4 0.062500000
6 0.363636364
1 0.458333333
3 0.428571429
5 0.619047619
5 0.480000000
0 0.300000000
5 0.289473684
1 0.128205128
7 0.057142857
6 0.921052632
2 0.411764706
0 0.593750000
2 0.538461538
1 0.50000
0 0.79167
3 0.66667
4 0.11111
1 0.40741
1 0.93333
2 0.87805
4 0.66667
2 0.65625
1 0.40000
1 0.96552
1 0.87500
2 0.15789
0 0.33333
3 0.10870
0 0.68966
0 0.16667
3 0.60417
3 0.11765
1 0.40000
1 0.25806
0 0.3158
3 0.9444
2 0.2222
1 0.8667
3 0.2222
2 0.1000
0 0.4667
3 0.7857
0 1.0000
1 0.6667
2 0.6364
3 1.0000
3 0.2308
1 1.0000
2 0.1250
2 1.0000
2 0.6154
0 0.1818
3 0.5263
0 0.4706
3 0.0667
2 0.9091
0 0.0000
0 0.1250
3 0.5000
1 0.088
1 0.474
0 0.711
2 0.651
0 0.212
0 0.588
1 0.643
2 0.029
2 0.388
2 0.063
2 0.182
2 0.051
0 0.814
0 0.622
1 0.063
0 0.444
0 0.958
2 0.813
1 0.318
0 0.905
1 0.50
0 0.79
0 0.67
0 0.11
1 0.41
1 0.93
1 0.88
0 0.67
0 0.66
0 0.40
1 0.81
0 0.63
1 0.71
0 0.27
0 0.70
0 0.27
1 0.38
0 0.58
1 0.39
1 0.97
1 0.667
0 0.750
1 0.500
1 0.833
2 0.900
0 0.000
2 0.727
0 0.316
1 0.944
2 0.222
2 0.867
1 0.222
0 0.353
2 0.167
2 0.333
0 0.529
0 0.706
1 0.100
0 0.467
0 0.786
1 1.000
0 0.667
1 0.636
1 1.000
2 0.231
2 1.000
2 0.125
2 0.130435
4 0.722222
0 0.292683
5 0.900000
0 0.770833
1 0.642857
2 0.028571
2 0.387755
2 0.062500
-1
My output is :

Code: Select all

Case 1: 7/22
Case 2: 1/3
Case 3: 1/11
Case 4: 5/7
Case 5: 119047381/166666500
Case 6: 1/1
Case 7: 13717421/111111111
Case 8: 54869684/55555555
Case 9: 574454131/999999999
Case 10: 41888317/49999500
Case 11: 2/9
Case 12: 1/9
Case 13: 2/9
Case 14: 1/3
Case 15: 4/9
Case 16: 5/9
Case 17: 2/3
Case 18: 7/9
Case 19: 8/9
Case 20: 999999998/999999999
Case 21: 0/1
Case 22: 1/5
Case 23: 1/5
Case 24: 2000000/99999999
Case 25: 3/10
Case 26: 1/2
Case 27: 11/20
Case 28: 5/9
Case 29: 1/7
Case 30: 9/10
Case 31: 1/1
Case 32: 1/13
Case 33: 678453453/1000000000
Case 34: 1/10
Case 35: 7/22
Case 36: 38408777/111111111
Case 37: 25/99
Case 38: 1/3
Case 39: 3/10
Case 40: 0/1
Case 41: 1/1
Case 42: 5/7
Case 43: 1/2
Case 44: 7/20
Case 45: 1/9
Case 46: 1/9
Case 47: 1/9
Case 48: 4415171/33333000
Case 49: 456467423/999999000
Case 50: 456467879/999999999
Case 51: 228233917/499999950
Case 52: 372727273/900000000
Case 53: 138047138/333333333
Case 54: 1/1
Case 55: 456716419/900000000
Case 56: 347826087/1000000000
Case 57: 233637097/249750000
Case 58: 1/9
Case 59: 166666667/1000000000
Case 60: 603562501/999000000
Case 61: 321107143/499500000
Case 62: 90909091/150000000
Case 63: 15483871/60000000
Case 64: 666600001/999900000
Case 65: 972710527/999000000
Case 66: 117768293/333000000
Case 67: 40909091/90000000
Case 68: 7662069/111100000
Case 69: 38961039/500000000
Case 70: 243941861/999000000
Case 71: 333333333/1000000000
Case 72: 376623377/1000000000
Case 73: 123587629/999000000
Case 74: 610000001/900000000
Case 75: 73880597/90000000
Case 76: 418918919/1000000000
Case 77: 1/1
Case 78: 40909091/112500000
Case 79: 76923077/1000000000
Case 80: 642631579/990000000
Case 81: 25286747/39600000
Case 82: 1/1
Case 83: 7449929/31218750
Case 84: 32282609/247500000
Case 85: 191176471/1000000000
Case 86: 681318681/1000000000
Case 87: 6779661/40000000
Case 88: 37/48
Case 89: 431147369/999000000
Case 90: 135989011/247500000
Case 91: 27027027/100000000
Case 92: 7/16
Case 93: 134907143/166650000
Case 94: 577358491/900000000
Case 95: 473636843/999900000
Case 96: 153409091/500000000
Case 97: 99354839/330000000
Case 98: 21063253/249750000
Case 99: 105882353/180000000
Case 100: 321107143/499500000
Case 101: 68627451/100000000
Case 102: 193877551/500000000
Case 103: 27551021/900000000
Case 104: 181800001/999900000
Case 105: 22469663/999900000
Case 106: 182795699/200000000
Case 107: 88034483/333000000
Case 108: 1/16
Case 109: 77586207/250000000
Case 110: 979591837/1000000000
Case 111: 817346939/900000000
Case 112: 791587501/999900000
Case 113: 34076087/82500000
Case 114: 876900001/999000000
Case 115: 630989011/990000000
Case 116: 159090909/250000000
Case 117: 5882353/100000000
Case 118: 28/55
Case 119: 21627907/30000000
Case 120: 47826087/100000000
Case 121: 360000001/990000000
Case 122: 67346939/300000000
Case 123: 190909091/900000000
Case 124: 34076087/41250000
Case 125: 4/25
Case 126: 3/10
Case 127: 61/88
Case 128: 8690309/14062500
Case 129: 23505883/999000000
Case 130: 956250001/990000000
Case 131: 268421053/900000000
Case 132: 91826087/198000000
Case 133: 235931461/999900000
Case 134: 1/2
Case 135: 791587501/999900000
Case 136: 666666001/999999000
Case 137: 111111111/1000000000
Case 138: 366666667/900000000
Case 139: 14/15
Case 140: 73097561/83250000
Case 141: 666600001/999900000
Case 142: 21/32
Case 143: 166666501/999999000
Case 144: 543750001/900000000
Case 145: 29411762/249999975
Case 146: 2/5
Case 147: 64516129/250000000
Case 148: 666666667/1000000000
Case 149: 972710527/999000000
Case 150: 1/16
Case 151: 363636001/999999000
Case 152: 11/24
Case 153: 71357143/166500000
Case 154: 68782381/111110000
Case 155: 12/25
Case 156: 3/10
Case 157: 28947079/99999000
Case 158: 14423077/112500000
Case 159: 14285713/249999975
Case 160: 102339079/111111000
Case 161: 135882353/330000000
Case 162: 19/32
Case 163: 533076923/990000000
Case 164: 1/2
Case 165: 79167/100000
Case 166: 66601/99900
Case 167: 1/9
Case 168: 36667/90000
Case 169: 14/15
Case 170: 86927/99000
Case 171: 66661/99990
Case 172: 64969/99000
Case 173: 2/5
Case 174: 86897/90000
Case 175: 7/8
Case 176: 1954/12375
Case 177: 33333/100000
Case 178: 181/1665
Case 179: 34483/50000
Case 180: 16667/100000
Case 181: 20119/33300
Case 182: 653/5550
Case 183: 2/5
Case 184: 3871/15000
Case 185: 1579/5000
Case 186: 17/18
Case 187: 2/9
Case 188: 7801/9000
Case 189: 2/9
Case 190: 1/10
Case 191: 4667/10000
Case 192: 785/999
Case 193: 1/1
Case 194: 6001/9000
Case 195: 6301/9900
Case 196: 1/1
Case 197: 1153/4995
Case 198: 1/1
Case 199: 619/4950
Case 200: 1/1
Case 201: 677/1100
Case 202: 909/5000
Case 203: 2629/4995
Case 204: 2353/5000
Case 205: 667/9990
Case 206: 9001/9900
Case 207: 0/1
Case 208: 1/8
Case 209: 1/2
Case 210: 4/45
Case 211: 427/900
Case 212: 711/1000
Case 213: 43/66
Case 214: 53/250
Case 215: 147/250
Case 216: 193/300
Case 217: 29/990
Case 218: 7/18
Case 219: 7/110
Case 220: 181/990
Case 221: 17/330
Case 222: 407/500
Case 223: 311/500
Case 224: 19/300
Case 225: 111/250
Case 226: 479/500
Case 227: 161/198
Case 228: 287/900
Case 229: 181/200
Case 230: 1/2
Case 231: 79/100
Case 232: 67/100
Case 233: 11/100
Case 234: 37/90
Case 235: 14/15
Case 236: 8/9
Case 237: 67/100
Case 238: 33/50
Case 239: 2/5
Case 240: 73/90
Case 241: 63/100
Case 242: 32/45
Case 243: 27/100
Case 244: 7/10
Case 245: 27/100
Case 246: 7/18
Case 247: 29/50
Case 248: 2/5
Case 249: 44/45
Case 250: 601/900
Case 251: 3/4
Case 252: 1/2
Case 253: 5/6
Case 254: 9/10
Case 255: 0/1
Case 256: 8/11
Case 257: 79/250
Case 258: 17/18
Case 259: 2/9
Case 260: 859/990
Case 261: 2/9
Case 262: 353/1000
Case 263: 83/495
Case 264: 1/3
Case 265: 529/1000
Case 266: 353/500
Case 267: 1/10
Case 268: 467/1000
Case 269: 393/500
Case 270: 1/1
Case 271: 667/1000
Case 272: 191/300
Case 273: 1/1
Case 274: 229/990
Case 275: 1/1
Case 276: 62/495
Case 277: 129131/990000
Case 278: 13/18
Case 279: 292683/1000000
Case 280: 9/10
Case 281: 770833/1000000
Case 282: 144643/225000
Case 283: 14143/495000
Case 284: 17449/45000
Case 285: 1/16
Like for example,
for 9 0.999999999 my code gives 999999998/999999999
and for input 2 1.000000000 my code gives 1/1

amishera
New poster
Posts: 38
Joined: Sat Dec 27, 2008 10:42 pm

Re: 332 why RTE please help me

Post by amishera » Sun Jan 18, 2009 12:53 am

Hi,
I am getting wrong answer for this problem because of the conversion from double to long long is giving round off error. I was wondering why conversion of double to long gives round off error?

I figure I have to do the conversion any way because x is a double.

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Re: 332 why RTE please help me

Post by mf » Sun Jan 18, 2009 2:04 am

amishera wrote:I was wondering why conversion of double to long gives round off error?
Isn't there ever only one fundamental reason beyond that? - Some numbers, like 1/10, simply can't be represented by IEEE-754 floating point numbers, they are approximated. Accept it.
I figure I have to do the conversion any way because x is a double.
You can treat x as a string and convert it to an integer however you like - the problem uses only one decimal format, which is rather easy to parse.

Also, note that the following two test cases have very different answers, even though numerical value of x is the same.

Code: Select all

1 0.3
1 0.30
-1
The first one is 1/3, while the second one is 3/10.

amishera
New poster
Posts: 38
Joined: Sat Dec 27, 2008 10:42 pm

Re: 332 why RTE please help me

Post by amishera » Sun Jan 18, 2009 3:39 am

Okay thanks for your information.

I beg you pardon if my question is very stupid but I am not very knowledgeable in C/C++ and its floating point mess. My question is that isnt it inefficient to resort to string manipulation for converting a double number? Say I have got this double number after a series of arithmatic operations like matrix multiplications (unlike this case where we can begin with string in the first place) so do I have to convert it to string first and then convert it back to integer everytime?

I accept that the fraction part might get rounded off. But in my case the double value contains an integer representation:

Code: Select all

double x =  0.507462687;

double a = pow(10,k+j);
double b = pow(10,k);

double c = a*x; // c = 507462687
double d = (long long) b*x; //d = 50746268

double p = c -d; // p = .....419

long long num = (long long) (c-d); // num = ...418
That is what I could not get it : the last digit (unit position) becomes one less though there is no fraction involved.

So this thing is confusing me. Also I wonder how reliable the modf/ceil/floor methods are. If double values behave this way then why do we use that in the first place because they seem unreliable?

PS: I am just asking because I want to know the mystery. I hope you wont mind asking such stupid question.

Post Reply

Return to “Volume 3 (300-399)”