Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C (Inverse Powers)

Status
Not open for further replies.

TankCDR

Programmer
Sep 22, 2001
1
0
0
US
I am a beginner in the C programming language. I was working on a problem and was trying to determine how to print a table of inverse powers. The output would be similar to what is below:

Number 1/Square 1/Cube 1/Quatric

1 1.0 1.0 1.0
2 0.25 0.125 0.0625
3 0.11 0.012 0.0041
etc...

Originally I thought to divide the number by it's cube to get the inverse square, the number by the quatric to get the inverse cube, etc...
This seems to work for the number 2, but seems to fall apart when I use the same logic with the number 3.

Any suggestions?

Thanks,
Alan
 
For bigger numbers you may get an over or underflow so you have to use some bignum library. Hope it will help.

#include <stdio.h>
#include <math.h>

int main (void)
{
int i;
double InvSquare,InvCube,InvQuatric;
for (i=1; i<100; i++)
{
InvSquare = 1./(i*i);
InvCube = 1./(i*i*i);
InvQuatric = 1./(i*i*i*i); printf(&quot;%d%12.10lf%12.10lf%12.10lf\n&quot;,i,InvSquare,InvCube,InvQuatric);
}
return 0;
}
 
you may also try the pow function defined in <math.h> which takes a double and raises to another double...
Therefore, you might say something like:
Code:
double inversePower;
int i;
for (i=0;i<whatever;i++)
   inversePower = pow(1,-i);
hth. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top