pranay2063's blog

By pranay2063, history, 9 years ago, In English

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?

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it +8 Vote: I do not like it

Read the documentation, it usually contains useful information.

pow operates with floating point numbers not integers. Double has only 52 significant bits, which is insufficient to precisely describe 335 ≈ 255.47