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 -
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 answers

  1. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 659
     
    hello
    there is
    double pow(double x, double y)
    to raise x to the power of y
    6
    1. geekat Posted messages 223 Registration date   Status Member Last intervention  
       
      Thank you, I didn't know her!
      0
    2. jisisv Posted messages 3678 Status Moderator 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