Thread: Power

  1. #1
    Registered User Bitojis's Avatar
    Join Date
    Jun 2005
    Posts
    20

    Power

    Hi, how can I write a power? For example, I want to do 2^8, or variable "b"^8.

    Thanks,

  2. #2
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Code:
    double b=8;
    cout<<pow((double)2, 8)<<" and "<<pow(b, 8)<<endl;
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    In the <math.h>

    use it like: pow(2.4 , 3); // power of 3

    I found this info on google (www.tutorials4u.com/c/t99.htm), just going to assume its correct (you could create your own too, but it'd probably have less flexibility unless you put some time into it).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Registered User Bitojis's Avatar
    Join Date
    Jun 2005
    Posts
    20
    Thank you, is very useful web!!

  5. #5
    Learn from the llama Marlon's Avatar
    Join Date
    Jun 2005
    Location
    South africa
    Posts
    25
    I normally use a loop for this kind of thing eg temp=temp*2 and run it 3 times to get 2^3.Probally not the best solution but it works
    `Who are YOU?' said the Caterpillar.
    This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.

  6. #6
    Registered User Bitojis's Avatar
    Join Date
    Jun 2005
    Posts
    20
    Yes, that is a good idea when you can´t use <math.h>.

  7. #7
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    If you are coding in C++, you should include <cmath.h>, I think. <math.h> is for C, while <cmath.h> is a C++ implementation of the same functions.

    The difference is in that while C++ supports function overloading, C doesn't. That's why in math.h you will find functions like:

    double pow (double base, double exp);
    float powf (float base, float exp);
    long double powl (long double base, long double exp);
    ...

    While cmath.h has:

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


    So, when using math.h, and wanting to do something like 2.15f ^ 2.3f, you must do it with powf() (or perform a cast from float to double and use pow()), while with <cmath.h>, you can still use the overloaded pow() for nearly anything (long double, float, double,etc.)
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's <cmath>, not <cmath.h>. And yes, that's preferable.

    Also, in the first answer, there's absolutely no need to cast the arguments. In fact it's a bad idea, as many compilers use integer powers instead of the more complicated and slower floating point power algorithm.

    Marlon, I do hope you use full *= temp inside the loop. But it's still unnecessary. For short powers, write it out (temp^2 -> temp*temp, temp^3 -> temp*temp*temp), for larger ones use the cmath function.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    It's <cmath>, not <cmath.h>
    Yeah, sorry, you are right. The problem was that I looked it up in "Programmer's Reference" by Herbert Schildt, where he said it must be <cmath.h>. Which I thought was strange, 'cause most of the C++ headers do not have the .h, like <iostream>, <vector> etc. But now I remember that I've heard somewhere that Schildt was a bad author or something?

    Also, in the first answer, there's absolutely no need to cast the arguments. In fact it's a bad idea, as many compilers use integer powers instead of the more complicated and slower floating point power algorithm.
    There is no need to cast the second argument, no. But what about the first one? If I just type pow(2,8); I get following errors:

    Code:
    main.cpp:12: error: call of overloaded `pow(int, int)' is ambiguous
    /usr/include/bits/mathcalls.h:154: error: candidates are: double pow(double,
       double)
    /usr/include/c++/3.3.4/cmath:512: error:				 long double
       std::pow(long double, int)
    /usr/include/c++/3.3.4/cmath:508: error:				 float std::pow(float,
       int)
    /usr/include/c++/3.3.4/cmath:504: error:				 double
       std::pow(double, int)
    /usr/include/c++/3.3.4/cmath:495: error:				 long double
       std::pow(long double, long double)
    /usr/include/c++/3.3.4/cmath:486: error:				 float std::pow(float,
       float)
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Hmm ... then apparently the cast is indeed necessary in order to disambiguate. (Annoying.)

    But a better idea here is to use constant suffixes:
    pow(8.0, 3) // calls pow(double, int)
    pow(8.0f, 3) // calls pow(float, int)
    pow(8.0l, 3) // if compiles, calls pow(long double, int)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by MathFan
    Yeah, sorry, you are right. The problem was that I looked it up in "Programmer's Reference" by Herbert Schildt, where he said it must be <cmath.h>. Which I thought was strange, 'cause most of the C++ headers do not have the .h, like <iostream>, <vector> etc. But now I remember that I've heard somewhere that Schildt was a bad author or something?
    Its because they changed the standard (ANSI or something) after that reference was made. It used to require .h but some odd 4 years ago they changed it so it no longer required .h and its now depreciated. (in gcc 4.0+ and .NET 2003+ or something).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Quote Originally Posted by CornedBee
    But a better idea here is to use constant suffixes:
    Mhm, will keep that in mind.

    Quote Originally Posted by Dae
    Its because they changed the standard (ANSI or something) after that reference was made. It used to require .h but some odd 4 years ago they changed it so it no longer required .h and its now depreciated. (in gcc 4.0+ and .NET 2003+ or something).
    Oh, didn't know that. Thanx...
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    What Dae just said is wrong. There never was a cmath.h header in any standard. From the very beginning, the C++ standard contained math.h and cmath.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Hmmm... my edition of the book was printed in 2000, and as far as I know, there were no changes in the standard since then. But then why on earth does Schildt write (even several times!) that I should include <cmath.h> in his book??
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Because while Schildt is indeed a good author, in that he writes interesting texts, he very frequently gets his facts wrong. Here's what a page about the C and C++ standards has to say about his book "The Annotated ANSI C Standard":
    Herbert Schildt is not held in wide regard by knowledgeable C and C++programmers, and this is the only book with his name on it most of them would ever recommend. The format of the book places the text of the standard on the left hand pages side-by-side with Schildt's annotations on the right hand side.

    While many of Schildt's comments are incorrect and flatly contradict the actual text of the standard on the facing page, the standard itself is complete except for one missing page.
    In fact, the reason this book is recommended is simply that it's the cheapest way of acquiring a copy of the standard.
    Some more:
    http://www.lysator.liu.se/c/schildt.html
    http://herd.plethora.net/~seebs/c/c_tcr.html
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  2. No Power, but power being supplied.
    By jrahhali in forum Tech Board
    Replies: 6
    Last Post: 08-11-2005, 02:50 AM
  3. The destructive power of a nuclear bomb
    By InvariantLoop in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 03-24-2005, 02:46 AM
  4. Power supplies and demanding processors
    By Liger86 in forum Tech Board
    Replies: 12
    Last Post: 03-17-2005, 11:56 AM