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 -
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:
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 }
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:
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)