Is there a square(x) function in C?
Solved
geekat
Posted messages
223
Registration date
Status
Member
Last intervention
-
jisisv Posted messages 3678 Status Moderator -
jisisv Posted messages 3678 Status Moderator -
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:
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 answers
-
hello
there isdouble pow(double x, double y)
to raise x to the power of y-
-
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)
-