Блог пользователя mamhh

Автор mamhh, 13 лет назад, По-английски
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 :) .
  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

13 лет назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится
Because this function returns double or float number. The result may be 99,999999... and when you output it as int, you get 99. I think this way :)
13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
Try to make it like this: " pow( 10 , (int)str.size()-1 ) "
13 лет назад, # |
  Проголосовать: нравится -20 Проголосовать: не нравится
Use,
#include <math> instead of #include<math.h>
13 лет назад, # |
  Проголосовать: нравится -11 Проголосовать: не нравится
just do not use the built in pow() . Make your own function for  pow().

13 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится
This code is not compliled: pow( 10 , str.size()-1 ).
This code pow( 10.0 , 2) returns expected 100 as output.
13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
You should write pow(a,b) where a and b are double (not int)!
So, your correct code must be:
pow ( 10.0, 1.0*( ((int) s.size()) -1 );
or
pow( (double) 10, 1.0*( ((int) s.size()) -1 ).

13 лет назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится
pow is a floating-point function: double pow(double x, double y).  Like most of the other standard floating-point functions, the result of pow is only guaranteed to be approximately correct.  pow(10.0, 2.0) can be exactly 100.0, slightly greater, or slightly less.  If pow(10.0, 2.0) is slightly less than 100.0, casting it to the type int converts it to 99.

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.
13 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится
For int,long int data type you can use this simple function:

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.
  • 13 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится
    In addition, in <math> there is two functions:
         double pow (      double base,         int exponent );
    long double pow ( long double base, int exponent );


    They use fast power counting, just as you said.
»
10 лет назад, # |
  Проголосовать: нравится -11 Проголосовать: не нравится

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

»
9 лет назад, # |
  Проголосовать: нравится -8 Проголосовать: не нравится

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

»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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!

»
5 лет назад, # |
Rev. 2   Проголосовать: нравится +11 Проголосовать: не нравится

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

#include "bits/stdc++.h"
#include <ext/numeric>
using namespace std;
using namespace __gnu_cxx;
int main()
{
   cout<<power(2,3);
}
»
5 лет назад, # |
Rev. 5   Проголосовать: нравится +10 Проголосовать: не нравится

In C++, you can write a user-defined function long long pow10(int n) to compute and return the nth power of 10 for some non-negative integer 0 ≤ n ≤ 18 using the string-to-long-long conversion function string::stoll()as follows.

inline long long pow10(int n)    
{
    return stoll("1"+string(n,'0'));
}

string(n,'0') constructs an n-character string filled with decimal zeros. When concatenated properly with "1", the sought string representation of 10n is generated.

You may use the C++ preprocessor to write the return expression as a #define macro as follows.

#define pow10(n) stoll("1"+string(n,'0'))
»
5 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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

»
4 года назад, # |
  Проголосовать: нравится -17 Проголосовать: не нравится

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.

»
4 года назад, # |
Rev. 2   Проголосовать: нравится -8 Проголосовать: не нравится

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....