Is there a square(x) function in C?

Solved
geekat Posted messages 223 Registration date   Status Membre Last intervention   -  
jisisv Posted messages 3678 Status Modérateur -
Hello,
There is one for the square root, which is sqrt, but I haven't found one for the square.
I would like to write this function:
My code:
#include <stdlib.h> #include <stdio.h> #include <math.h> int main(int argc, char *argv[]) { for (i=0; i<=n; i++) int x = sqrt(x); // I don't know how to continue }

3 réponses

dubcek Posted messages 18814 Registration date   Status Contributeur Last intervention   5 655
 
hello
there is
double pow(double x, double y)
to raise x to the power of y
6
geekat Posted messages 223 Registration date   Status Membre Last intervention  
 
Thank you, I didn't know her!
0
jisisv Posted messages 3678 Status Modérateur 936
 
There is NO interest in using pow and the like for a square.
It is significantly more efficient to use the product of the entity (preferably a simple variable) by itself. In general, all integer power elevations are resolved in a simple loop and squares, thanks to Horner.
Using pow in this context is inefficient.
At most, one can define a macro for the square:
#define square(a) (a)*(a)

or for the cube if necessary..

Let's leave pow([lf] their usage: raising an entity to a non-integer power and different from 1/2 (in which case sqrt will be more efficient)
2