Unexpected behaviour of pow()

Revision en1, by pranay2063, 2015-07-23 23:06:13

Hi Codeforces,

For a particular problem , i used first the predefined pow() function and then my own version of pow() function , named power().

Code :

#include<bits/stdc++.h>

using namespace std;

#define ull unsigned long long

ull power(ull a,ull n)
{

    ull res=1;

    while(n>=1)
    {

        if(n&1)  res*=a;

        n>>=1;

        a*=a;

    }

    return res;

}

int main()
{

    ull a=3,b=35;

    cout<<(ull)pow(a,b)-1<<" ";
    cout<<power(a,b)-1<<endl;

    return 0;

}

But , two of them give different outputs for same input.

The output on codeblocks is :

50031545098999705 50031545098999706

What can be reason for the same?

Tags pow(), c++

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English pranay2063 2015-07-23 23:06:13 756 Initial revision (published)