pow function

M

Magix

#include <math.h>
double pow( double base, double exp );


will pow (2, -30) works fine ? (exp is negative value). If not, what are the
workaround for exp to be negative?
 
J

Jack Klein

#include <math.h>
double pow( double base, double exp );


will pow (2, -30) works fine ? (exp is negative value). If not, what are the
workaround for exp to be negative?

Do you really think posting to usenet is faster and/or better than
looking at your compiler's library documentation, man pages, or online
help? Really?

Well, if you insist, here is a part of the C standard's definition of
the pow() function:

"2 The pow functions compute x raised to the power y. A domain error
occurs if x is finite and negative and y is finite and not an integer
value. A domain error may occur if x is zero and y is less than or
equal to zero. A range error may occur."
 
S

Scott J. McCaughrin

: #include <math.h>
: double pow( double base, double exp );


: will pow (2, -30) works fine ? (exp is negative value). If not, what are the
: workaround for exp to be negative?

2^(-30) = 1.0/(2^30) = (1/2)^30, so try pow(0.5, 30.0).
 
R

Richard Bos

Scott J. McCaughrin said:
: #include <math.h>
: double pow( double base, double exp );

: will pow (2, -30) works fine ? (exp is negative value). If not, what are the
: workaround for exp to be negative?

2^(-30) = 1.0/(2^30) = (1/2)^30, so try pow(0.5, 30.0).

Ah, but now you need to work out 1/2 by hand. This is feasible for
constants, but what if base is a variable? Luckily,

base ** (-exp) == base ** (-1*exp) == (base ** -1) ** exp

so you could do pow(pow(2, -1), 30)...

Richard
 
M

Martin Ambuhl

Richard said:
Ah, but now you need to work out 1/2 by hand. This is feasible for
constants, but what if base is a variable? Luckily,

base ** (-exp) == base ** (-1*exp) == (base ** -1) ** exp

so you could do pow(pow(2, -1), 30)...


What the hell are you guys smoking?
#include <math.h>
#include <stdio.h>
#include <float.h>

int main(void)
{
double x1, x2;
x1 = pow(2, -30);
x2 = pow(0.5, 30);
printf("x1 = pow(2,-30) = %.*g\n"
"x2 = pow(0.5,30) = %.*g\n"
"x1-x2 = %.*g\n",
DBL_DIG, x1, DBL_DIG, x2, DBL_DIG, x1 - x2);
return 0;
}

[output]
x1 = pow(2,-30) = 9.31322574615479e-10
x2 = pow(0.5,30) = 9.31322574615479e-10
x1-x2 = 0
 
R

Richard Bos

Martin Ambuhl said:
What the hell are you guys smoking?

I can't speak for Mr. McCaughrin, but I was smoking some of those man
pages Jack Klein mentioned...

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top