Like Tree3Likes
  • 1 Post By grumpy
  • 2 Post By tabstop

pow () function with variables

This is a discussion on pow () function with variables within the C Programming forums, part of the General Programming Boards category; I used pow() function with a variable and I found some error Code: for (i=0;i<3;i++) pow(5,i); Well, this kind of ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    pow () function with variables

    I used pow() function with a variable and I found some error

    Code:
    for (i=0;i<3;i++)
      pow(5,i);
    Well, this kind of things are not working with pow() functions.. Is there any way to solve this issue.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    Post your error messages.

    Also, where do you assign the result of calling pow() ?
    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.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    What does "some error" mean? We can't "solve this issue" if you do not explain what it is.

    The code you posted will produce 1.0, 5.0, and 25.0, which is correct.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    What is the value of i when that loop starts

    What is 5 ^ 0?

    Also what variable types does pow expect? And what are you handing it?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    Apart from the above, there needs to be an #include <math.h> in the source file before using pow().

    Some implementations (compiler, linker, IDE, etc) do not link in floating point support (sometimes described as math support) by default - so will trigger error messages when linking, even if the source file is correct. If you have one of those you will need to read the documentation for your implementation to find out how to link in math support.
    TheBigH likes this.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    @OP:64 posts on this board and you still post this garbage?
    Quote Originally Posted by suryak View Post
    I used pow() function with a variable and I found some error

    Code:
    for (i=0;i<3;i++)
      pow(5,i);
    Well, this kind of things are not working with pow() functions.. Is there any way to solve this issue.
    My advice, turn off your computer and throw it out the window. You will never accomplish anything if you cannot at a minimum figure out How to ask a question.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    I compiled the following code..

    # include <stdio.h>
    # include <math.h>

    Code:
    int main(void)
    {
       int n=5;
      
       printf("%f",pow(3+sqrt(5),n));
    }
    got this error.

    Code:
    /tmp/ccgmJtjc.o: In function `main':
    test.c:(.text+0x23): undefined reference to `pow'
    collect2: ld returned 1 exit status
    when I place an number instead of a variable in 2nd argument of pow() I'm not getting error.

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    You need to add -lm to the compilation command.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    Well you need to compile with

    gcc prog.c -lm

    to link with the math library.

    The reason it works with a constant is because the compiler can see what you're doing, and optimise out an actual call to pow()
    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.

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    @grumpy :
    if I compile according to
    its showing


    test.c: In function ‘main’:
    test.c:8: warning: incompatible implicit declaration of built-in function ‘pow’
    test.c:8: warning: incompatible implicit declaration of built-in function ‘sqrt’
    /tmp/ccpaH4qk.o: In function `main':
    test.c.text+0x23): undefined reference to `pow'
    collect2: ld returned 1 exit status

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    2,804
    What header files are you including? You need at least stdio.h and math.h.

    Jim

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Actually.. grumpy was saying not to mention math.h .. so I was telling her the output what I got without math.h..

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by suryak View Post
    Actually.. grumpy was saying not to mention math.h .. so I was telling her the output what I got without math.h..
    Read grumpy's post again. And maybe another time for good measure.
    AndrewHunter and TheBigH like this.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    Quote Originally Posted by suryak View Post
    Actually.. grumpy was saying not to mention math.h .. so I was telling her the output what I got without math.h..
    I actually said the opposite. You also got my gender wrong.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function variables
    By acerweather in forum C Programming
    Replies: 3
    Last Post: 02-16-2010, 01:16 AM
  2. function variables
    By lilhawk2892 in forum C++ Programming
    Replies: 5
    Last Post: 07-05-2006, 10:38 AM
  3. Global variables sent to a function
    By ExtremelyStupid in forum C Programming
    Replies: 5
    Last Post: 08-02-2004, 05:57 AM
  4. do variables die in function
    By Chobo_C++ in forum C++ Programming
    Replies: 7
    Last Post: 04-16-2004, 04:59 PM
  5. pointers as function variables
    By PHP in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2003, 05:45 AM

Tags for this Thread


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