1. advise on how to properly use pow function

I have a simple C code that uses 'pow' function pow(x,y) . declared integer variables total, x, y with given values for x and y. I should be expecting a value of 8. See error details below upon executing this.

Code:
```#include <stdio.h>
#include <math.h>
int main(void)

{

int total, x, y;

x = 2;
y = 3;
pow (x, y);  // expecting the value of x raise to y is "8"

printf("The total value of x and y is%d:\n", total);
return 0;
}

/tmp/ccMV9JVl.o(.text+0x2c): In function `main':
: undefined reference to `pow'
collect2: ld returned 1 exit status```

2. pow() is for floating point values... Look up the definition in your C Library documentation.

for integer values...
Code:
```int int_pow(int x, int y)
{ int z = x;
while (--y)
z *= x;
return z; }```

3. Originally Posted by libchk
declared integer variables total, x, y with given values for x and y. I should be expecting a value of 8.
Even if you did not encounter the problem that you did, your expectation would not have been met because you failed to assign to total.

Originally Posted by CommonTater
pow() is for floating point values.
As such, you could cast the arguments to double and return value to int, though be careful of floating point inaccuracy.

Originally Posted by CommonTater
for integer values...
Note that the time complexity of this algorithm is proportional to the value of the exponent, so be wary of using it (especially repeatedly) for large powers. Luckily, it is not difficult to implement a version that has time complexity proportional to the logarithm of the value of the exponent.

4. I suggest in your case to simply use doubles instead of ints.

5. Originally Posted by laserlight
Note that the time complexity of this algorithm is proportional to the value of the exponent, so be wary of using it (especially repeatedly) for large powers. Luckily, it is not difficult to implement a version that has time complexity proportional to the logarithm of the value of the exponent.
Hi Laze ... just trying to keep it at a level a beginner is going to understand.

I'd like to see your algorythm, though... I'm always game to learn something new/better/faster/easier/etc.

6. Originally Posted by King Mir
I suggest in your case to simply use doubles instead of ints.
As long as the last bit ambiguity in floating point isn't an issue... sure.

7. Originally Posted by CommonTater
As long as the last bit ambiguity in floating point isn't an issue... sure.
Actually double has 53 effective bits of precision, which is more than enough to cover the entire 32 bit range of int. So no int will ever lose precision for being converted to double, nor will any integer stored in a double suffer rounding error when converted to int, provided it is withing the range an int can hold.

8. Originally Posted by King Mir
Actually double has 53 effective bits of precision, which is more than enough to cover the entire 32 bit range of int. So no int will ever lose precision for being converted to double, nor will any integer stored in a double suffer rounding error when converted to int, provided it is withing the range an int can hold.
Well... except for that last bit ambiguity that can (for example) turn 8 into 7.99999999999 which ends up being 7 when you convert it to an integer.

What Every Computer Scientist Should Know About Floating-Point Arithmetic

9. Originally Posted by King Mir
Actually double has 53 effective bits of precision

"Actually double has 53 ineffective bits of precision..."

Quzah.

10. Originally Posted by CommonTater
I'd like to see your algorythm, though... I'm always game to learn something new/better/faster/easier/etc.
Most C++ compilers are accompanied by an implementation of the binary pow method for integer powers, which you'll find in their source code.

11. Originally Posted by iMalc
Most C++ compilers are accompanied by an implementation of the binary pow method for integer powers, which you'll find in their source code.
But we're not talking about C++ are we?

12. Originally Posted by CommonTater
But we're not talking about C++ are we?
I didn't mean to imply you were. I was fully aware of which forum I was posting in and I could have said "Pascal" for all it matters. The point being, I gave you one way of finding an implementation of it.

But really, what self-respecting programmer doesn't have a C++ compiler installed.

13. > /tmp/ccMV9JVl.o(.text+0x2c): In function `main':
> : undefined reference to `pow'
> collect2: ld returned 1 exit status
Meaning you forgot to link with the math library.

gcc -Wall prog.c -lm

That's minus-L-M (all in lower case)