What is the C ++ function to increase the number?

How to raise the number to power?

2^1 2^2 2^3 

etc...

+101
c ++ math
May 10 '09 at 19:18
source share
16 answers

pow () in the cmath library. More info here . Remember to put #include<cmath> at the top of the file.

+122
May 10 '09 at 19:20
source share

std::pow in the <cmath> header has the following overloads:

 pow(float, float); pow(float, int); pow(double, double); // taken over from C pow(double, int); pow(long double, long double); pow(long double, int); 

Now you can’t just do

 pow(2, N) 

with N being int, because it does not know which of the float , double or long double versions it should accept, and you get an ambiguity error. All three will need to convert from int to floating point, and all three are equally expensive!

Therefore, be sure to type the first argument so that it matches perfectly with one of these three. I usually use double

 pow(2.0, N) 

Some lawyer shit from me again. I myself often fell into this trap, so I am going to warn you about this.

+88
May 10 '09 at 19:41
source share

In C ++, the "^" operator is a bitwise OR. It does not work to increase power. X <n is the left shift of the binary number, which coincides with multiplying x by 2 n times and can be used only when increasing 2 to power. The POW function is a mathematical function that will work as a whole.

+18
May 20 '15 at 18:33
source share

Use the pow (x, y) function: see here

Just turn on math.h and you are all set.

+14
May 10 '09 at 19:20
source share

You should be able to use ordinary C methods in math.

#include <cmath>

pow(2,3)

if you are using a unix-like system, man cmath

Is that what you are asking?

Sujal

+13
May 10 '09 at 19:24
source share

Although pow( base, exp ) is a great suggestion, keep in mind that it usually works with a floating point.

This may or may not be what you want: on some systems, a simple loop multiplied by a drive will be faster for integer types.

And for a square, you can just multiply the numbers together yourself, with a floating point or an integer; this does not really reduce readability (IMHO), and you avoid the overhead of a function call function.

+11
May 10 '09 at 19:25
source share

I don't have enough reputation for comment, but if you like working with QT, they have their own version.

  #include <QtCore/qmath.h> qPow(x, y); // returns x raised to the y power. 

Or, if you are not using QT, cmath has basically the same thing.

  #include <cmath> double x = 5, y = 7; //As an example, 5 ^ 7 = 78125 pow(x, y); //Should return this: 78125 
+9
Oct 08 '15 at 19:58
source share
 #include <iostream> #include <conio.h> using namespace std; double raiseToPow(double ,int) //raiseToPow variable of type double which takes arguments (double, int) void main() { double x; //initializing the variable x and i int i; cout<<"please enter the number"; cin>>x; cout<<"plese enter the integer power that you want this number raised to"; cin>>i; cout<<x<<"raise to power"<<i<<"is equal to"<<raiseToPow(x,i); } 

// define raiseToPower function

 double raiseToPow(double x, int power) { double result; int i; result =1.0; for (i=1, i<=power;i++) { result = result*x; } return(result); } 
+6
Dec 02 '14 at 15:47
source share

Is this the value of pow or powf in <math.h>

There is no special infix operator in Visual Basic or Python

+4
May 10, '09 at 19:21
source share
 pow(2.0,1.0) pow(2.0,2.0) pow(2.0,3.0) 

Your original question title is misleading. For a simple square, use 2*2 .

+4
May 10, '09 at 19:22
source share

Many answers suggest pow() or similar alternatives or their own implementations. However, given the examples ( 2^1 , 2^2 and 2^3 ) in your question, I would have guessed if you need to raise only 2 to an integer degree. If so, I would suggest you use 1 << n for 2^n .

+4
Jul 30 '18 at 7:03
source share

if you want to deal only with base_2, then I recommend using the left shift operator << instead of the math library .

sample code:

 int exp = 16; for(int base_2 = 1; base_2 < (1 << exp); (base_2 <<= 1)){ std::cout << base_2 << std::endl; } 

sample output:

 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 
+4
Aug 01 '18 at 11:22
source share

Note that using pow(x,y) less efficient than x*x*x y times, as shown here and the answer is given here https://stackoverflow.com/a/167449/

So if you want to increase efficiency, use x*x*x .

+3
Apr 09 '17 at 21:59 on
source share
 int power (int i, int ow) // works only for ow >= 1 { // but does not require <cmath> library!=) if (ow > 1) { i = i * power (i, ow - 1); } return i; } cout << power(6,7); //you can enter variables here 
+1
Feb 28 '17 at 20:43
source share

I use cmath or math.h library to use pow() library functions that take care of permissions

 #include<iostream> #include<cmath> int main() { double number,power, result; cout<<"\nEnter the number to raise to power: "; cin>>number; cout<<"\nEnter the power to raise to: "; cin>>power; result = pow(number,power); cout<<"\n"<< number <<"^"<< power<<" = "<< result; return 0; } 
+1
Oct 30 '17 at 14:37
source share

First add #include <cmath> then you can use the pow method in your code, for example:

 pow(3.5, 3); 

Which 3,5 is basic, and 3 is exp

0
May 7 '19 at 12:24
source share



All Articles