[gcj] Re: Error in my C++ code

2010-05-15 Thread tonka
why the heck will you write such a clumsy and lengthy code??? take a look at my code: #include #include #include #include #include #include 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)

[gcj] Re: Error in my C++ code

2010-05-16 Thread Aamir Khan
yeah i got my mistake thanks for that.. but i don't understand how 10^8 can be saved in int..as int have range from -32767 to +32768 and one more thing to ask is that "int PD=1< wrote: > I found one mistake : If b < PD, you are returning 0. Why is that? > Lets say you have 4 switches, PD = 16.

[gcj] Re: Error in my C++ code

2010-05-16 Thread Aamir Khan
"n" is int in this casehow can pow(2,n) can workit requires both the arguments to be double??? On May 15, 11:15 pm, tonka wrote: > why the heck will you write such a clumsy and lengthy code??? take a > look at my code: > #include > #include > #include > #include > #include > #include > >

[gcj] Re: Error in my C++ code

2010-05-16 Thread Aamir Khan
the information given by you is good..but in other situations the text written by you is not so much clearcould you please clearly post it.what is "to use logn multiplications" Thanks for help On May 15, 11:50 pm, Davi Costa wrote: > You should take care about (long int) cast, be

[gcj] Re: Error in my C++ code

2010-05-17 Thread tonka
i wouldn't suggest using fmod() because it is going to use the value returned by pow() which is double in this case and my intuition says that it is bound to take longer than simply taking the mod (%) of 2 integer numbers as it involves significant conversions. moreover in this particular problem

[gcj] Re: Error in my C++ code

2010-05-17 Thread tonka
n is going to be converted to double by the function pow() and then it is used. the result is again casted back to long int. On May 16, 1:39 am, Aamir Khan wrote: > "n" is int in this casehow can pow(2,n) can workit requires > both the arguments to be double??? > > On May 15, 11:15 pm, to

[gcj] Re: Error in my C++ code

2010-05-17 Thread TripleM
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

[gcj] Re: Error in my C++ code

2010-05-20 Thread Aamir Khan
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 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 w

Re: [gcj] Re: Error in my C++ code

2010-05-15 Thread Davi Costa
You should take care about (long int) cast, because in c++ most compilers uses the int precision the same as long int precision (32 bits), if you need more you should use (long long int). Also using the function pow to exponentiate integers is a very bad ideia. It first change it to double, use so

Re: [gcj] Re: Error in my C++ code

2010-05-16 Thread Carlos Guia
Unless you are using a very old compiler/computer, int is a 32 bit integer, the range is [-2147483648, 2147483647]. And even if they were 16 bit, the range would be [-32768, 32767]. About the "logn multiplication" I think he meant O(log n) exponentiation. Type "fast exponentiation" in Google, and

Re: [gcj] Re: Error in my C++ code

2010-05-16 Thread Bharath Raghavendran
I believe in your compiler, int will be 2 bytes and long int will be 4 bytes. Check that once. If so, you can use long int instead of int everywhere. That way you can store an integer in the order of 10^8. On 16 May 2010 02:07, Aamir Khan wrote: > yeah i got my mistake thanks for that.. > but i

Re: [gcj] Re: Error in my C++ code

2010-05-17 Thread Manish kumar Singh
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 wrote: > why the heck will you write such a clumsy and lengthy code??? take a > look at my code: > #include > #include > #include > #include > #include > #in

Re: [gcj] Re: Error in my C++ code

2010-05-17 Thread Leopoldo Taravilse
This way is easier: if(++k%(1

Re: [gcj] Re: Error in my C++ code

2010-05-17 Thread Amtep
On Mon, May 17, 2010 at 04:49:41PM +0530, Manish kumar Singh wrote: > is using this statement a good idea? > if (fmod(k + 1, pow(2, n)) == 0)... > It worked. All of the values involved are exactly representable as 'double', so it should work as long as fmod and pow are accurate to the limits

Re: [gcj] Re: Error in my C++ code

2010-05-18 Thread Manish kumar Singh
very good point TripleM.. :) On Tue, May 18, 2010 at 7:24 AM, TripleM 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(

Re: [gcj] Re: Error in my C++ code

2010-05-21 Thread Carlos Guia
Check 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 an