Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C Language Dictionary

  1. Home
  2. C Language Dictionary
  3. sqrt() / pow() / fabs() / ceil() / floor()

sqrt() / pow() / fabs() / ceil() / floor()

Since: C89(1989)

Common math functions for square roots, exponentiation, absolute values, ceiling, and floor. They are defined in <math.h>, and you may need to pass the -lm linker option at compile time.

Syntax

double sqrt(double x); // Returns the positive square root of x (x >= 0 required).
double pow(double x, double y); // Returns x raised to the power of y.
double fabs(double x); // Returns the absolute value of a double.
double ceil(double x); // Returns the smallest integer value >= x (ceiling).
double floor(double x); // Returns the largest integer value <= x (floor).
double round(double x); // Rounds to the nearest integer (C99 and later).
double fmax(double x, double y);// Returns the larger of x and y (C99 and later).
double fmin(double x, double y);// Returns the smaller of x and y (C99 and later).

Function List

FunctionDescriptionExample
sqrt()Square root.sqrt(9.0) → 3.0
pow()Exponentiation.pow(2.0, 10.0) → 1024.0
fabs()Absolute value.fabs(-3.5) → 3.5
ceil()Ceiling function (round up).ceil(1.2) → 2.0
floor()Floor function (round down).floor(1.9) → 1.0
round()Round to nearest integer.round(1.5) → 2.0
fmax()Returns the larger value.fmax(3.0, 5.0) → 5.0
fmin()Returns the smaller value.fmin(3.0, 5.0) → 3.0

Sample Code

sample_sqrt_pow_fabs_ceil_floor.c
#include <stdio.h>
#include <math.h>

int main(void) {
    // Compute a square root with sqrt.
    printf("sqrt(2.0) = %.6f\n", sqrt(2.0)); // Outputs "1.414214".

    // Compute an exponent with pow.
    printf("2^10 = %.0f\n", pow(2.0, 10.0)); // Outputs "1024".

    // Compute the absolute value of a floating-point number with fabs.
    printf("fabs(-3.7) = %.1f\n", fabs(-3.7)); // Outputs "3.7".

    // Round toward an integer with ceil and floor.
    double val = 2.3;
    printf("ceil(%.1f) = %.1f\n", val, ceil(val)); // Outputs "3.0".
    printf("floor(%.1f) = %.1f\n", val, floor(val)); // Outputs "2.0".

    double neg = -2.3;
    printf("ceil(%.1f) = %.1f\n", neg, ceil(neg)); // Outputs "-2.0".
    printf("floor(%.1f) = %.1f\n", neg, floor(neg)); // Outputs "-3.0".

    // Example: computing Euclidean distance.
    double dx = 3.0, dy = 4.0;
    printf("distance = %.1f\n", sqrt(dx*dx + dy*dy)); // Outputs "distance = 5.0".

    return 0;
}

Run the following command:

gcc sqrt_pow_fabs_ceil_floor.c -o sqrt_pow_fabs_ceil_floor -lm
./sqrt_pow_fabs_ceil_floor
sqrt(2.0) = 1.414214
2^10 = 1024
fabs(-3.7) = 3.7
ceil(2.3) = 3.0
floor(2.3) = 2.0
ceil(-2.3) = -2.0
floor(-2.3) = -3.0
distance = 5.0

Common Mistakes

Common Mistake: Passing a Negative Value to sqrt

sqrt() returns NaN (Not a Number) when passed a negative value. Always verify that the argument is 0 or greater before calling.

sqrt_negative_ng.c
#include <stdio.h>
#include <math.h>

int main(void) {
    /* NG: passing a negative value returns NaN */
    printf("NG: sqrt(-1.0) = %f\n", sqrt(-1.0)); /* nan */
    return 0;
}

Run the following command:

gcc sqrt_negative_ng.c -o sqrt_negative_ng -lm
./sqrt_negative_ng
NG: sqrt(-1.0) = -nan
sqrt_negative_ok.c
#include <stdio.h>
#include <math.h>

int main(void) {
    double values[] = {-1.0, 0.0, 2.0, 9.0};
    int n = (int)(sizeof(values) / sizeof(values[0]));

    for (int i = 0; i < n; i++) {
        if (values[i] < 0.0) {
            printf("sqrt(%.1f) = out of domain\n", values[i]);
        } else {
            printf("sqrt(%.1f) = %.6f\n", values[i], sqrt(values[i]));
        }
    }
    return 0;
}

Run the following command:

gcc sqrt_negative_ok.c -o sqrt_negative_ok -lm
./sqrt_negative_ok
sqrt(-1.0) = out of domain
sqrt(0.0) = 0.000000
sqrt(2.0) = 1.414214
sqrt(9.0) = 3.000000

Notes

Some environments (e.g., GCC on Linux) will produce a linker error if you omit the -lm flag from the compile command.

Passing a negative value to sqrt() returns NaN (Not a Number). Always verify that the argument is non-negative. Error conditions can be detected via errno or fpclassify().

For trigonometric functions, see sin() / cos() / tan(). For logarithms and exponentiation, see log() / exp().

If you find any errors or copyright issues, please .