C++
  Home arrow C++ arrow Page 6 - First Steps in (C) Programming, conclusion
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

First Steps in (C) Programming, conclusion
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 54
    2005-12-01

    Table of Contents:
  • First Steps in (C) Programming, conclusion
  • More Numeric Data Types
  • Try It Out: Character Building
  • Unsigned Integers: Using Positive Integers
  • Try It Out: Finding the Limits
  • Mathematical Functions
  • Designing a Program
  • The Solution
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    Function

    Operation

    floor(x)

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

    ceil(x)

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

    fabs(x)

    Returns the absolute value of x

    log(x)

    Returns the natural logarithm (base e) of x

    log10(x)

    Returns the logarithm to base 10 of x

    exp(x)

    Returns the value of ex

    sqrt(x)

    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

    Function

    Operation

    sin(x)

    Sine of x expressed in radians

    cos(x)

    Cosine of x

    tan(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.

    More C++ Articles
    More By Apress Publishing


       · This article is an excerpt from the book "Beginning C", published by Apress. We hope...
     

    Buy this book now. This article is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530). Check it out at your favorite bookstore. Buy this book now.

    C++ ARTICLES

    - Paths and Files
    - Directories in C++
    - Focusing on C++ Files
    - Const Correctness in C++
    - Manipulating Streams and Files with C++
    - Streams and Files
    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT