Home arrow C++ arrow Page 6 - First Steps in (C) Programming, conclusion

First Steps in (C) Programming, conclusion

If you're a beginning programmer and want to get more deeply into programming with variables, you've come to the right place. This article, the third of three parts, is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530).

Author Info:
By: Apress Publishing
Rating: 4 stars4 stars4 stars4 stars4 stars / 63
December 01, 2005
  1. · First Steps in (C) Programming, conclusion
  2. · More Numeric Data Types
  3. · Try It Out: Character Building
  4. · Unsigned Integers: Using Positive Integers
  5. · Try It Out: Finding the Limits
  6. · Mathematical Functions
  7. · Designing a Program
  8. · The Solution
  9. · Summary

print this article

First Steps in (C) Programming, conclusion - Mathematical Functions
(Page 6 of 9 )

The math.h header file includes declarations for a wide range of mathematical functions. To give you a feel for whatís available, youíll take a look at those that are used most frequently. All the functions return a value of type double.

You have the set of functions shown in Table 2-7 available for numerical calculations of various kinds. These all require arguments to be of typedouble.

Table 2-7. Functions for Numerical Calculations




Returns the largest integer that isnít greater than x as type double


Returns the smallest integer that isnít less than x as type double


Returns the absolute value of x


Returns the natural logarithm (base e) of x


Returns the logarithm to base 10 of x


Returns the value of ex


Returns the square root of x

pow(x, y)

Returns the value of xy

Here are some examples of using these functions:

double x = 2.25;
double less = 0.0;
double more = 0.0;
double root = 0.0;
less = floor(x); /* Result is 2.0 */
more = ceil(x);  /* Result is 3.0 */
root = sqrt(x);  /* Result is 1.5 */

You also have a range of trigonometric functions available, as shown in Table 2-8. Arguments and values returned are again of typedouble and angles are expressed in radians.

Table 2-8. Functions for Trigonometry




Sine of x expressed in radians


Cosine of x


Tangent of x

If youíre into trigonometry, the use of these functions will be fairly self-evident. Here are some examples:

double angle = 45.0; /* Angle in degrees */ double pi = 3.14159265;
double sine = 0.0;
double cosine = 0.0;
sine = sin(pi*angle/180.0);   /* Angle converted to radians */
cosine = sin(pi*angle/180.0); /* Angle converted to radians */

Because 180 degrees is the same angle as π radians, dividing an angle measured in degrees by 180 and multiplying by the value of π will produce the angle in radians, as required by these functions.

You also have the inverse trigonometric functions available:asin(),acos(), andatan(), as well as the hyperbolic functionssinh(),cosh(), andtanh(). Donít forget, you must includemath.hinto your program if you wish to use any of these functions. If this stuff is not your bag, you can safely ignore this section.

blog comments powered by Disqus

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials