advise on how to properly use pow function

This is a discussion on advise on how to properly use pow function within the C Programming forums, part of the General Programming Boards category; I have a simple C code that uses 'pow' function pow(x,y) . declared integer variables total, x, y with given ...

  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 10:13 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    I suggest in your case to simply use doubles instead of ints.
    Last edited by King Mir; 08-30-2011 at 10: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,053
    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,308
    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,308
    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
    32,832
    > /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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 10: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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21