HridoyHazard's blog

By HridoyHazard, history, 2 years ago, In English

i came to know this error recently while using pow function for a large data such as

pow(8376260,70)

it giving 0 as output which is wrong. i tried using long and unsigned but it giving the same output. is there any way to use large data in pow function??

  • Vote: I like it
  • -4
  • Vote: I do not like it

| Write comment?
»
2 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Yeah this function is kinda weird so it does problems. Create your own pow function so it doesn't cause problems. Also the numbers you are using are very large so it will overflow

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The function is fine, it even reports errors when it happens. You can check such errors with the errno macro.

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks. I never lnew that. But isn't implementing the function yourself better? Especially when you have to do it for large numbers where you have to use mod

      • »
        »
        »
        »
        2 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        yep, in this case that would probably be the only option.

        (unless you're using python, of course)

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

the pow function returns a "double" type.

»
2 years ago, # |
  Vote: I like it +1 Vote: I do not like it

You'd better use binary pow.

const long long MOD = 1e9 + 7;
long long binpow(long long n, long long p){
	long long res = 1;
	while(p) {
		if(p & 1) {
			res *= n;
			res %= MOD;
		}
		p >>= 1;
		n *= n;
		n %= MOD;
	}
	return res;
}