Check http://en.wikipedia.org/wiki/Double_precision_floating-point_format
<http://en.wikipedia.org/wiki/Double_precision_floating-point_format> pow will use double and not long long, so it can't accurately store 2^60. Stay away from floating point types when possible, if the input are integers and the results are integers there is usually a way to do all calculations with integers. If they are integers but they don't fit in 64 bits, you are probably better of using some sort of arbitrary precision integer library, with floating point values you can be almost sure there will be rounding errors. Carlos Guía On Fri, May 21, 2010 at 2:56 AM, Aamir Khan <[email protected]> wrote: > why there is discrepance in the result??? of 1LL<<60 and pow(2,60)?? > > Can you explain it in brief?? > > On May 18, 6:54 am, TripleM <[email protected]> wrote: > > While using fmod(k+1,pow(2,n)) will work in this case, it is most > > definitely not a good idea. Suppose the bounds were a bit bigger so > > that 64 bit integers were required, rather than just 32-bit ones. > > > > Then pow(2,60) is equal to 1152921504606847000, while 1LL<<60 is > > 1152921504606846976. > > > > You should get in the habit of never using floating point arithmetic > > when only integers are involved. > > > > On May 17, 11:19 pm, Manish kumar Singh <[email protected]> wrote: > > > > > > > > > tonka, > > > > > is using this statement a good idea? > > > if (fmod(k + 1, pow(2, n)) == 0)... > > > It worked. > > > > > On Sat, May 15, 2010 at 11:45 PM, tonka <[email protected]> > wrote: > > > > why the heck will you write such a clumsy and lengthy code??? take a > > > > look at my code: > > > > #include<iostream.h> > > > > #include<fstream.h> > > > > #include<conio.h> > > > > #include<math.h> > > > > #include<stdio.h> > > > > #include<string.h> > > > > > > void main() > > > > { > > > > int t,flag=0,n; > > > > long int k; > > > > ifstream fi("A-large.in",ios::binary|ios::in); > > > > ofstream fo("outputlar.out",ios::out); > > > > fi>>t; > > > > for(int t_c=0;t_c<t;t_c++) > > > > { > > > > fi>>n>>k; > > > > if(((k+1)%(long int)pow(2,n))==0) > > > > fo<<"Case #"<<(t_c+1)<<": "<<"ON"<<endl; > > > > else > > > > fo<<"Case #"<<(t_c+1)<<": "<<"OFF"<<endl; > > > > } > > > > fi.close(); > > > > fo.close(); > > > > getch(); > > > > } > > > > > > This works for the large input also. > > > > > > On May 14, 7:49 pm, Bharath Raghavendran <[email protected]> > wrote: > > > > > I found one mistake : If b < PD, you are returning 0. Why is that? > > > > > Lets say you have 4 switches, PD = 16. If b = 15, the answer should > be > > > > ON. > > > > > > > Other than that, there are many ways you can improve your code. > > > > > 1. > > > > > dont use double. It will remove accuracy in many places. 10^8 will > come > > > > > within 4 bytes (usually int suffices this. else use long int). > > > > > > > 2. > > > > > To calculate 2^n, you can use the left shift operator. "int PD = > 1<<n" > > > > will > > > > > put 2^n into PD. > > > > > > > 3. > > > > > To check if b gives reminder PD -1, you don't need to run a loop. > You can > > > > do > > > > > "if (b%PD == PD-1)" > > > > > > > On 13 May 2010 10:47, Aamir Khan <[email protected]> wrote: > > > > > > > > #include <cmath> > > > > > > #include <cstdio> > > > > > > #include <iostream> > > > > > > using namespace std; > > > > > > > > #define _CRT_SECURE_NO_WARNINGS > > > > > > #define For(i,a,b) for (long i(a),_b(b); i <= _b; ++i) > > > > > > > > char buf[1024*1024]; > > > > > > > > bool isEven(long a,long b) > > > > > > { bool result=0; > > > > > > if(b%2==0) > > > > > > result=0; > > > > > > else > > > > > > { > > > > > > double ad=0.00; > > > > > > for(int j=0;j<=30;j++) > > > > > > { > > > > > > if(int(ad)==a) > > > > > > { break;} > > > > > > ad=ad+1.00; > > > > > > } > > > > > > long PD=long(pow(2.00,ad)); > > > > > > if(b<PD) > > > > > > result=0; > > > > > > > > else > > > > > > { > > > > > > for(long i=PD;i<=100000000;i+=PD) > > > > > > { > > > > > > if(b==i-1) > > > > > > { > > > > > > result=1; > > > > > > break; > > > > > > } > > > > > > } > > > > > > } > > > > > > } > > > > > > return result; > > > > > > } > > > > > > > > int main() { > > > > > > freopen("A-large.in", "rt", stdin); > > > > > > freopen("output_Snapper_Chain.txt", "wt", stdout); > > > > > > > > gets(buf); > > > > > > long size=atoi(buf); > > > > > > > > For(test, 1, size) > > > > > > { > > > > > > // cout<<"No. of loops"<<test; > > > > > > long N; > > > > > > long K; > > > > > > > > scanf("%d%d", &N, &K); > > > > > > // cout<<"N"<<N<<"K"<<K; > > > > > > > > bool res=isEven(N,K); > > > > > > if(res) > > > > > > printf("Case #%d: ON\n", test); > > > > > > else > > > > > > printf("Case #%d: OFF\n", test); > > > > > > } > > > > > > exit(0); > > > > > > } > > > > > > > --------------------------------------------------------------------------- > > > > ------------------------------------------------ > > > > > > SNAPPER CHAIN: > > > > > > Can anybody tell me the errors in the code.....i have done this > > > > > > problem using the simple concept which i know....it is not giving > > > > > > right output.... > > > > > > > > Brief explanation of code: > > > > > > i have first converted the long taken from file into the double > using > > > > > > for loop..then using pow function i have found 2^N and then found > > > > > > whether k==2^N-1; > > > > > > > > _____ > > > > > > Aamir > > > > > > > > -- > > > > > > You received this message because you are subscribed to the > Google > > > > Groups > > > > > > "google-codejam" group. > > > > > > To post to this group, send email to > [email protected]. > > > > > > To unsubscribe from this group, send email to > > > > > > [email protected]<google-code%[email protected]> > <google-code%[email protected]<google-code%[email protected]> > > > > > > <google-code%2bunsubscr...@googlegr oups.com> > > > > > > . > > > > > > For more options, visit this group at > > > > > >http://groups.google.com/group/google-code?hl=en. > > > > > > > -- > > > > > You received this message because you are subscribed to the Google > Groups > > > > "google-codejam" group. > > > > > To post to this group, send email to [email protected]. > > > > > To unsubscribe from this group, send email to > > > > [email protected]<google-code%[email protected]> > <google-code%[email protected]<google-code%[email protected]> > > > > > > . > > > > > For more options, visit this group athttp:// > > > > groups.google.com/group/google-code?hl=en. > > > > > > -- > > > > You received this message because you are subscribed to the Google > Groups > > > > "google-codejam" group. > > > > To post to this group, send email to [email protected]. > > > > To unsubscribe from this group, send email to > > > > [email protected]<google-code%[email protected]> > <google-code%[email protected]<google-code%[email protected]> > > > > > > . > > > > For more options, visit this group at > > > >http://groups.google.com/group/google-code?hl=en. > > > > > -- > > > You received this message because you are subscribed to the Google > Groups "google-codejam" group. > > > To post to this group, send email to [email protected]. > > > To unsubscribe from this group, send email to > [email protected]<google-code%[email protected]> > . > > > For more options, visit this group athttp:// > groups.google.com/group/google-code?hl=en. > > > > -- > > You received this message because you are subscribed to the Google Groups > "google-codejam" group. > > To post to this group, send email to [email protected]. > > To unsubscribe from this group, send email to > [email protected]<google-code%[email protected]> > . > > For more options, visit this group athttp:// > groups.google.com/group/google-code?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "google-codejam" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]<google-code%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/google-code?hl=en. > > -- You received this message because you are subscribed to the Google Groups "google-codejam" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-code?hl=en.
