Concept

How do you use the C standard mathematics library?

ComputerScienceOne / Preprocessor Directives

"There are many other important standard libraries that we’ll touch on as needed; of immediate interest is the standard mathematics library, math.h. It includes many useful functions to compute common mathematical functions such as the square root and natural logarithm. Table 15.1 highlights several of these functions. To use them you’d include the math library in your source file\n\n#include <math.h>\n\nand then “call” them by providing input and assigning the output value to a variable. For example:\n\n1 double x = 1.5;\n\n2 double y, z;\n\n3 y = sqrt(x); //y now has the value √ x = √ 1 . 5\n\n4 z = sin(x); //z now has the value sin ( x ) = sin (1 . 5)\n\nIn both of the function calls above, the value of the variable x is “passed” to the math function which computes and “returns” the result which then gets assigned to another variable.\n\nFunction Description\n\nabs(x) Absolute value for int variables, | x |\n\nfabs(x) Absolute value for double variables\n\nceil(x) Ceiling function, d 46 . 3 e = 47 . 0\n\nfloor(x) Floor function, b 46 . 3 c = 46 . 0\n\ncos(x) Cosine function\n\nsin(x) Sine function\n\ntan(x) Tangent function\n\nexp(x) Exponential function, e x , e = 2 . 71828 . . .\n\nlog(x) Natural logarithm, ln ( x )\n\nlog10(x) Logarithm base 10, log 10 ( x )\n\npow(x,y) The power function, computes x y\n\nsqrt(x) Square root function\n\nThe absolute value function is actually in the standard library, stdlib.h. All trigonometric functions assume input is in radians, not degrees. Input for log(x) and log10(x) is assumed to be positive, x >"

Related Ideas