Math.h Examples
2002-01-17 23:04:22
Category: cpp:math
Description: Examples of the functions in math.h
Author: detour
Viewed: 7825
Rating: (40 votes)


/* math.cpp by detour detour@metalshell.com
 *
 * Examples of the functions in math.h
 *
 * http://www.metalshell.com/
 *
 */
 
#include <iostream.h>
#include <math.h>
 
#define PI 3.14159265
 
int main() {
  int num[3] = { 81, 1000, -503};
  double dnum = 5.356;
 
  cout << "The square root of 81 is " << sqrt(num[0]) << endl;
  cout << "The absolute value of -503 is " << abs(num[2]) << endl;
  cout << "Log base 10 of 100 is " << log10(num[1]) << endl;
  cout << "The ceiling of 5.356 is " << ceil(dnum) << endl;
  cout << "The floor of 5.356 is " << floor(dnum) << endl;
  cout << "The sine of PI / 2 is " << sin(PI / 2) << endl;
  cout << "The cosine of PI is " << cos(PI) << endl;
  cout << "The arcsine of .5 is " << asin(.5) * 180 / PI << " degrees\n";
  cout << "The arccosine of .5 is " << acos(.5) * 180 / PI << " degrees\n";
  cout << "The arctangent of .5 is " << atan(.5) * 180 / PI << " degrees\n";
  cout << "2 to the power of 5 is " << pow(2,5);
 
  return 0;
}