Is there a square(x) function in C?

Solved
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

  • Votes
  • Date
  • Descending date
  1. Contributor
    hello
    there is
    double pow(double x, double y)
    to raise x to the power of y
    1. Thank you, I didn't know her!
    2. Moderator
      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)