Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to raise a double value by power of 12?

I have a double which is:

double mydouble = 10;

and I want 10^12, so 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10 * 10. I tried

double newDouble = pow(10, 12);

and it returns me in NSLog: pow=-1.991886

makes not much sense... I think pow isn't my friend right?

like image 871
Another Registered User Avatar asked Nov 29 '09 17:11

Another Registered User


People also ask

How do you raise something to a power in C++?

pow() is function to get the power of a number, but we have to use #include<math. h> in c/c++ to use that pow() function. then two numbers are passed. Example – pow(4 , 2); Then we will get the result as 4^2, which is 16.

How do you do exponents in C?

The C language lacks an exponentiation operator, which would raise a base value to a certain power. For example: 2^8 , which in some programming languages is an expression to raise 2 to the 8th power. Instead, C uses the pow() function.

What is the correct prototype of power function?

C pow() Prototype To find the power of int or a float variable, you can explicitly convert the type to double using cast operator. int base = 3; int power = 5; pow(double(base), double(power));


2 Answers

try pow(10.0, 12.0). Better yet, #include math.h.

To clarify: If you don't include math.h, the compiler assumes that pow() returns an integer. Including math.h brings in a prototype like

double pow(double, double);

So the compiler can understand how to treat the arguments and the return value.

like image 174
Richard Pennington Avatar answered Oct 26 '22 06:10

Richard Pennington


I couldn't even get the program to compile without:

#include <math.h>

When using math functions like this you should ALWAYS include math.h and make sure you are calling the right pow function. Who knows what the other pow function might be ... it could stand for "power wheels" haha

like image 30
Brian T Hannan Avatar answered Oct 26 '22 05:10

Brian T Hannan