Hi all, the function "pow( x , y )" should raise x to the y-th power , it works fine on my PC, but it gives me wrong answer on codeforces .

the problem is : I have a string , say "123", when I make call the function like this " pow( 10 , str.size()-1 ) " it returns "99"

why not returning "100" ???

my code: http://pastebin.com/B1LXwys6

the problem : http://www.codeforces.com/problemset/problem/61/C

thnx in advance :) .

the problem is : I have a string , say "123", when I make call the function like this " pow( 10 , str.size()-1 ) " it returns "99"

why not returning "100" ???

my code: http://pastebin.com/B1LXwys6

the problem : http://www.codeforces.com/problemset/problem/61/C

thnx in advance :) .

(int)str.size()-1 ) "mamhh,If you use floating-point operations, handle numerical errors appropriately; it is painful but necessary.

My recommendation is to avoid unnecessary uses of floating-point operations. If both the input and the output are integers, you can avoid floating-point operations without much trouble in most cases.

template<class T> T power(T N,T P){ return (P==0)? 1: N*power(N,P-1); }

Or make something like this for your own and add it in your template. You can also raise power lot faster using repeated squaring.

They use fast power counting, just as you said.

I too hate this kind of precision errors in C++.

I had same problem in past contest.

use this pow("m" is mod in the code,it is good for calculating

(x^y)%m,remove it if you don't use it):link

i think because the pow function must work with doubles it is O(N) instead of O(log(N))(if isn't tell me).

because O(log(N)) pow is very useful try to use your pow with O(log(N))!

thanx!

Here my code with builtin fast_power which work in O(log(N))

In C++, you can write a user-defined function

`long long pow10(int n)`

to compute and return thenth power of 10 for some non-negative integer 0 ≤n≤ 18 using the string-to-long-long conversion function`string::stoll()`

as follows.`string(n,'0')`

constructs ann-character string filled with decimal zeros. When concatenated properly with "1", the sought string representation of 10^{n}is generated.You may use the C++ preprocessor to write the return expression as a

`#define`

macro as follows.Using ceil( pow(base,exponent) ) worked for me.

pow(x, y) is most likely implemented as exp(y * log(x)) which can go off for surprisingly small integral arguments. This manifests itself in a particularly pathological way if the result is truncated to an integer.

The moral of the story is to avoid the pow function when working in integer arithmetic.

Imagine reviving a 10 year old blog just to say that...

In case of resulting an int value try using binary exponentiation instead of pow() function.... It is more reliable than pow() function..

you can see the here...

Hope that this will help you to not getting this type of error again....

I also faced this type of problem. but when I need perfectly 1000, 9000 these types of numbers I create a function that will generate a

stringas"1000"or"9000"and then convert it intointusingstoi()function and return it.