Bifalgorithm post · Computer science one · Preprocessor Directives

How do you use the C standard mathematics library?

How do you use the C standard mathematics library? (Computer science one · Preprocessor Directives) — "There are many other important standard libraries that w…

Ideas in this flowchart

  • Using the standard mathematics library math.h
  • math.h provides functions for common mathematical operations, including square roots and natural logarithms.
  • Include the library in the source file with #include <math.h>.
  • Call the functions by providing input and assigning the returned output to a variable.
  • A function call passes the value of a variable to the function, which computes and returns a result.
  • The example initializes x to 1.5 and declares variables y and z.
  • The example assigns sqrt(x) to y and sin(x) to z.
  • The library includes numerical functions such as abs, fabs, ceil, floor, and pow.
  • The library includes the trigonometric functions cos, sin, and tan.
  • The library includes exp, log, and log10 for exponential and logarithmic calculations.
  • The absolute value function for integers, abs(x), is defined in stdlib.h rather than math.h.
  • All trigonometric functions expect their input in radians rather than degrees.
  • The input for log(x) and log10(x) must be positive.
Feed/

How do you use the C standard mathematics library?

No cover

Channel

Computer science one

1 page

"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 >"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.