Skip to Content
Course content

107: Power and Square Root Functions

Click on the "Edit" button in the top corner of the screen to edit your slide content.

In most modern languages, you're used to having a handy operator for exponents—like ** in Python. C isn't that generous. If you want to raise a number to a power or find a square root, you have to step outside the core language and lean on the standard math library.

Mapping out a distance calculator

To see these functions in action, I want to build a simple utility that calculates the distance between two points on a 2D plane. If you remember your geometry, the distance formula is just the Pythagorean theorem in disguise: distance = sqrt((x2-x1)^2 + (y2-y1)^2). This is a perfect scenario because we need both a power function for the squaring and a square root function for the final result.

First, I'll include <math.h>. Without this, the compiler has no idea what pow() or sqrt() are. I'm using double for all my coordinates because math functions in C almost exclusively work with floating-point numbers. Using integers here would lead to a lot of annoying casting.

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

int main() {
    double x1 = 2.0, y1 = 3.0;
    double x2 = 5.0, y2 = 7.0;

    double dx = x2 - x1;
    double dy = y2 - y1;

    // We need to square the differences
    double distance_sq = pow(dx, 2) + pow(dy, 2);
    double distance = sqrt(distance_sq);

    printf("The distance is: %f\n", distance);
    return 0;
}

The "Undefined Reference" headache

Here is where I usually trip up the first few times I do this. I hit compile with gcc main.c -o dist and... boom. A wall of errors. Specifically: undefined reference to 'pow' and undefined reference to 'sqrt'.

It's frustrating because the code is syntactically perfect. The problem is that <math.h> is just a header file—it tells the compiler what the functions look like, but it doesn't actually contain the code for the functions themselves. That code lives in a separate library file. On Linux and macOS, you have to explicitly tell the linker to link the math library using the -lm flag.

I have to change my compilation command to: gcc main.c -o dist -lm. Now it works. Just remember: if you see "undefined reference" to a math function, stop staring at your code and check your linker flags.

Refining the math for performance

Now, I'll let you in on a little industry secret. While pow(dx, 2) is readable, it's actually quite slow. The pow() function is designed to handle anything—fractional exponents, negative bases, the works. It's a heavy piece of machinery.

When you're just squaring a number, it's much more efficient to just multiply the number by itself. I'm going to rewrite the logic to be more "C-like"—fast and direct.

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

int main() {
    double x1 = 2.0, y1 = 3.0;
    double x2 = 5.0, y2 = 7.0;

    double dx = x2 - x1;
    double dy = y2 - y1;

    // Much faster than calling pow() twice
    double distance_sq = (dx * dx) + (dy * dy); 
    double distance = sqrt(distance_sq);

    printf("The distance is: %.2f\n", distance);
    return 0;
}

I also added %.2f to the printf. It cleans up the output so we aren't staring at six decimal places we don't actually need.




📋 Practical Task

Build a Hypotenuse Finder for Right Triangles

Write a program that asks the user to input the lengths of the two shorter sides (the legs) of a right-angled triangle. Your program should then calculate and print the length of the hypotenuse using the formula: hypotenuse = sqrt(a^2 + b^2).

  • Use double for your variables to ensure precision.
  • Use the pow() function for the squaring part of the calculation.
  • Use the sqrt() function for the final result.
  • Ensure you compile your code with the -lm flag to avoid linker errors.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.