Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C For Dummies 2nd Ed 2004.pdf
Скачиваний:
43
Добавлен:
17.08.2013
Размер:
8.34 Mб
Скачать

Chapter 25: Math Madness! 317

Gotta link in that math library!

If you’re using Unix, Linux, or Mac OS X, you need to know that GCC isn’t normally configured to do high-level math functions, such as pow() and sqrt(). The reason is that the standard C library doesn’t come with those math functions in it. The advantage is that programs produced with the standard library are small. The disad­ vantage is that you need to remember to link in the math library when you write programs that use math.

The standard math library in C is named libm, but you can link it into your program by modify­ ing the gcc command:

gcc -lm source.c -o output

Follow gcc with the -lm switch. This switch tells GCC to also link in the libm math library along with the standard library. The input file is source.c, and the output file is output. To properly compile lights1.c:

gcc -lm lights1.c -o lights1

Again, this option is necessary only for pro­ grams that use high-level math functions. To confirm, use the man command to look up the function:

man sqrt

It says, right there on top, under the LIBRARY heading, that the standard math library must be linked in to make the function work.

Rooting out the root

Another math problem you may have to work out is the square root. For the life of me, I can’t cite a practical example because, honestly, I find all math stupid. In any event, no keyboard symbol in C is used to calculate the square root of a number. As with the power-of deal, you use a special func­ tion: sqrt().

The sqrt() function is used to divine the square root of a number — to find out which number, when multiplied by itself, equals the number you want the square root of. Something like that. In any event, it’s not the “squirt” function. Here’s the format:

value = sqrt(n)

The double variable value is equal to the square root of the double variable n. Yes, everything must be double here. You also need the following include at the beginning of your source code:

#include <math.h>

318 Part IV: C Level

The only limitation on the sqrt() function is that n, the number you’re find­ ing the root of, cannot be negative. Even though my Vulcan friends tell me that there is such a thing, I would be leery of attempting it in the C language.

Because you and Milton have this light thing going, you’re going to surprise him by telling him the square root of the number of lights you bought for Christmas. To make that process easier, you devise the LIGHTS2.C program.

Type this program into your editor. Be careful when you’re typing because that long printf() line was split in order to fit into this book’s format; the single backslash is used to show that the line continues on the following line:

#include <stdio.h> #include <math.h> #define TOOTH 253

int main()

{

double lights;

 

lights=sqrt(256);

/* square root of 256 */

printf(“Milton, I got your %0.f%c lights.\n”,\ lights,TOOTH);

return(0);

}

Save this puppy to disk as LIGHTS2.C:

Compile and run. (Unix users, refer to the preceding sidebar, “Gotta link in that math library!” for more details.)

Milton, I got your 162 lights.

The sqrt() function boasts that the square root of 256 is 16. Or, to put it another way, 162 (or 16 × 16) is equal to 256. Milton would be pleased.

The square root of 256 is 16, and 16 squared (162) is 256. Is math great or what?

Character code 253 is equal to the tiny 2 — the squared number.

Character code 251 is equal to the traditional square root symbol. Even if you can manage to get that symbol into your text editor, you still need the sqrt() function to wrangle up a square root.

In some cases, codes 253 and 251 may not display the same symbol as described in the two preceding notes. Oh, well.

The %c in the printf() formatting string is used to bring in the special character, 253, defined as TOOTH earlier in the source code.