Thread: advise on how to properly use pow function

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    5

    advise on how to properly use pow function

    I have a simple C code that uses 'pow' function pow(x,y) . declared integer variables total, x, y with given values for x and y. I should be expecting a value of 8. See error details below upon executing this.

    Code:
    #include <stdio.h>
    #include <math.h>
    int main(void)
    
    {
    
    int total, x, y;    
    	
            x = 2;
    	y = 3;
    	pow (x, y);  // expecting the value of x raise to y is "8"
    	
    	printf("The total value of x and y is%d:\n", total);
    	return 0;
    }
    
    
    /tmp/ccMV9JVl.o(.text+0x2c): In function `main':
    : undefined reference to `pow'
    collect2: ld returned 1 exit status

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    pow() is for floating point values... Look up the definition in your C Library documentation.

    for integer values...
    Code:
    int int_pow(int x, int y)
      { int z = x;
        while (--y)
           z *= x;
        return z; }
    Last edited by CommonTater; 08-30-2011 at 09:13 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by libchk
    declared integer variables total, x, y with given values for x and y. I should be expecting a value of 8.
    Even if you did not encounter the problem that you did, your expectation would not have been met because you failed to assign to total.

    Quote Originally Posted by CommonTater
    pow() is for floating point values.
    As such, you could cast the arguments to double and return value to int, though be careful of floating point inaccuracy.

    Quote Originally Posted by CommonTater
    for integer values...
    Note that the time complexity of this algorithm is proportional to the value of the exponent, so be wary of using it (especially repeatedly) for large powers. Luckily, it is not difficult to implement a version that has time complexity proportional to the logarithm of the value of the exponent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I suggest in your case to simply use doubles instead of ints.
    Last edited by King Mir; 08-30-2011 at 09:59 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    Note that the time complexity of this algorithm is proportional to the value of the exponent, so be wary of using it (especially repeatedly) for large powers. Luckily, it is not difficult to implement a version that has time complexity proportional to the logarithm of the value of the exponent.
    Hi Laze ... just trying to keep it at a level a beginner is going to understand.

    I'd like to see your algorythm, though... I'm always game to learn something new/better/faster/easier/etc.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by King Mir View Post
    I suggest in your case to simply use doubles instead of ints.
    As long as the last bit ambiguity in floating point isn't an issue... sure.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by CommonTater View Post
    As long as the last bit ambiguity in floating point isn't an issue... sure.
    Actually double has 53 effective bits of precision, which is more than enough to cover the entire 32 bit range of int. So no int will ever lose precision for being converted to double, nor will any integer stored in a double suffer rounding error when converted to int, provided it is withing the range an int can hold.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by King Mir View Post
    Actually double has 53 effective bits of precision, which is more than enough to cover the entire 32 bit range of int. So no int will ever lose precision for being converted to double, nor will any integer stored in a double suffer rounding error when converted to int, provided it is withing the range an int can hold.
    Well... except for that last bit ambiguity that can (for example) turn 8 into 7.99999999999 which ends up being 7 when you convert it to an integer.

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by King Mir View Post
    Actually double has 53 effective bits of precision
    That should read:

    "Actually double has 53 ineffective bits of precision..."


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by CommonTater View Post
    I'd like to see your algorythm, though... I'm always game to learn something new/better/faster/easier/etc.
    Most C++ compilers are accompanied by an implementation of the binary pow method for integer powers, which you'll find in their source code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iMalc View Post
    Most C++ compilers are accompanied by an implementation of the binary pow method for integer powers, which you'll find in their source code.
    But we're not talking about C++ are we?

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by CommonTater View Post
    But we're not talking about C++ are we?
    I didn't mean to imply you were. I was fully aware of which forum I was posting in and I could have said "Pascal" for all it matters. The point being, I gave you one way of finding an implementation of it.

    But really, what self-respecting programmer doesn't have a C++ compiler installed.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > /tmp/ccMV9JVl.o(.text+0x2c): In function `main':
    > : undefined reference to `pow'
    > collect2: ld returned 1 exit status
    Meaning you forgot to link with the math library.

    gcc -Wall prog.c -lm

    That's minus-L-M (all in lower case)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. modf function doesn't work properly
    By paranoidgnu in forum C Programming
    Replies: 1
    Last Post: 06-29-2011, 09:59 AM
  2. Replies: 2
    Last Post: 03-08-2011, 02:16 AM
  3. Replies: 2
    Last Post: 12-09-2009, 12:04 PM
  4. How to properly use a C function?
    By John_L in forum C Programming
    Replies: 4
    Last Post: 05-30-2008, 02:01 AM
  5. Loop doesn't seem to function properly
    By TeQno in forum C++ Programming
    Replies: 1
    Last Post: 01-31-2005, 05:25 PM