Question..

This is a discussion on Question.. within the C++ Programming forums, part of the General Programming Boards category; How do I use the pow command? I need to basicly do 10^x I dont know how to do it ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    Unhappy Question..

    How do I use the pow command?

    I need to basicly do 10^x

    I dont know how to do it with the pow command if any one could help me I would be thankful

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    <batman>
    pow!!!!
    zap!!!!
    </batman>

    > How do I use the pow command?
    Read the manual page, follow the examples?
    n = pow(x, y);
    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
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >How do I use the pow command?
    Code:
    double answer = pow(10., x)

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Code:
    double pow( double base, double exp );
    As such,

    Code:
    double result;
    double exp = 3; // for instance
    
    result = pow(10.0, exp);
    Do NOT use integers with this function. The standard states that implementations are free to overload floating-point functions to their float, double and long double types. So, its possible that your implementation has pow defined as:

    double pow( float base, float exp );
    double pow( double base, double exp );
    double pow( long double base, long double exp );


    If you use an integer as an argument to one or both parameters, the compiler will complain your attempt is ambiguous. Even if it does not (because some compilers do not overload or provide preprocessor policies), your code may fail to compile on other implementations.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Do NOT use integers with this function.
    Doesn't C++ have a:
    Code:
    double pow( double base, int exp );
    I have no idea how to check this. Maybe the C++ standard has a list?

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    If you need to do ints, just write it all out:
    Code:
    x = x*x*x*x*x*x*x*x*x*x      //x^10
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Quote Originally Posted by manutd
    If you need to do ints, just write it all out:
    Code:
    x = x*x*x*x*x*x*x*x*x*x      //x^10
    You are joking right?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Yes.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I checked the library for Dev-C++ and it does have a:
    Code:
    double pow( double base, int exp );
    I dunno if it's standard though.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,659
    It's not. I don't understand why you can't just use a cast to double for intermediate use of pow() though.

  11. #11
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    As citizen said. It's not a big deal to use a cast in this situation. Cast to whatever floating-point type is more appropriate, adorn the literal or define floating-point variables, do what you must. But avoid using integers even if your compiler happily says everything is alright
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >It's not.
    Do you have a copy of the C++ standard to verify that? Because I'm fairly certain it's in there.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >But avoid using integers even if your compiler happily says everything is alright
    I see no reason to avoid using int for the exponent. Unless you're compiling C code.

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,659
    > Do you have a copy of the C++ standard to verify that?
    I can find a reference.
    http://www.dinkumware.com/manuals/?m...=math.html#pow
    Barring you don't trust that, various implementations will let you know what is available by extention if you look it up in the help files.

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Well... the standard does in fact state the following signatures for pow (26.5.6):

    float pow(float, int)
    float pow(float, float)
    double pow(double, int)
    double pow(double, double)
    long double pow(long double, int)
    long double pow(long double, long double)

    So yes, an int exponent will not generate ambiguity.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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