power minus

Feb 8, 2011 at 4:36pm
I wanna write code to show the value of 2 power -5,

if I do like this, it gives correct answer.

if(b<0)
{
for(int i=1;i<=-b;i++)
{
result *=1.0/a;
}
}

////////////////////////////////////////////////////////////////////////
But, if I do like this, it can't give correct answer, why?
I think both are same.

if(b<0)
{
for(int i=-b;i<=1;i++)
{
result *=1.0/a;
}
}
///////////////////////////////////////////////////////////
Feb 8, 2011 at 4:57pm
If b = 3, for example, the first for() loop runs 3 times: i = 1, i = 2, i = 3.
the second for() loop runs 5 times: i = -3, i = -2, i = -1, i = 0, i = 1.
Feb 9, 2011 at 3:38am
I see
thanks :)
Topic archived. No new replies allowed.