If you like my posts please leave a comment.

Sunday, September 9, 2012

MATHEMATICAL FUNCTIONS


C++ provides various mathematical functions, which can be used as an aid to mathematical calculations in your program. To use these mathematical factions you need to include math.h header file in your program. Table given below summarizes some of the important mathematical functions used in C++.
Function
Description
Example
int abs(int x)
abs() function returns the absolute value of the integer argument x. If abs is called when stdlib.h has been included, it's treated as a macro that expands to inline code. If you want to use the abs function instead of the macro, include #undef abs
in your program, after the #include <stdlib.h>.
int number = -1234;
cout<<"\n Absolute value of "<<number<<" = "<<abs(number);
Output:
double fabs(double x)
Returns the absolute value of a floating-point number. fabs calculates the absolute value of x, a double.
float  number = -1234.05;
cout<<"number:"<<number<<" Absolute value: "<<fabs(number);
Output:
double fmod(double x, double y)
fmod() function, calculates x modulo y, the remainder of x/y.
fmod calculates x modulo y (the remainder f, where x = ay + f for some integer a, and 0 < f < y).
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
#include <conio.h>

int main(void)
{
   double x = 5.0, y = 2.0;
   double result;

   result = fmod(x,y);
   cout.setf(ios::showpoint);
   cout<<"The remainder of "<<setprecision(2)<<x<<" / "<<y<<" = "<<result;
getch();
   return 0;
}
Output:
double modf(double x, double *ipart)
modf() function, breaks the double x into two parts: the integer and the fraction. modf stores the integer in ipart and returns the fraction.
#include <iostream.h>
#include <math.h>
#include <iomanip.h>
#include <conio.h>
int main(void)
{
   double fraction, integer;
    double number = 100000.567;

    fraction = modf(number, &integer);
    cout.setf(ios::fixed);
    cout<<"The whole and fractional parts of"<<setprecision(3)<<number<<" are "<<", "<<integer<<" and "<<fraction;
   getch();
   return 0;
}
Output:
double log(double x)
log() function calculates the natural logarithm of x.
int main(void)
{
   double result;
   double x = 8.6872;

   result = log(x);
   cout<<"The natural log of "<<x<<" = "<<result;
   getch();
   return 0;
}
Output:
double pow(double x, double y)
pow() function, calculates x to the power of y. If the argument x passed to pow is real and less than 0, and y is not a whole number, or you call pow(0,0), the global variable errno is set to
EDOM               Domain error
double x = 2.0, y = 3.0;
cout<<x<<" is raised to "<<y<<" = "<<pow(x, y);
 Output:
double sqrt(double x);
sqrt() function, calculates the positive square root of the argument x. On success, sqrt return the value calculated, the square root of x. If x is real and positive, the result is positive. If x is real and negative, the global variable errno is set to
EDOM               Domain error
double x = 4.0, result;
result = sqrt(x);
cout.setf(ios::fixed);
cout<<"\n The Square root of "<<setprecision(1)<<x<<" = "<<result;
Output:
double sin(double x)
sin() function computes the sine of the input value. Angles are specified in radians.
double result, x = 0.5;
result = sin(x);
cout.setf(ios::fixed);
cout<<"\n The sin of "<<setprecision(2)<<x<<" = "<<result;
Output:
double cos(double x)
cos() function calculates the cosine of the input value. The angle is specified in radians.
double result, x = 0.5;
result = cos(x);
cout.setf(ios::fixed);
cout<<"\n The cosine of "<<setprecision(2)<<x<<" = "<<result;
Output:
double ceil(double x)
Rounds up
ceil() finds the smallest integer not less than x.
double number = 123.54;
double up,down;
    up = ceil(number);
    down = floor(number);
    cout<<"\n The original number: "<<number;
    cout<<"\n number rounded up to: "<<up;
    cout<<"\n number rounded down to: "<<down;
Output:
double floor(double x)
Rounds down.
floor() finds the largest integer not greater than x.
double poly(double x, int degree, double coeffs[])
poly() function generates a polynomial in x, of degree degree, with coefficients coeffs[0], coeffs[1], ..., coeffs[degree]. For Example if x = 3, the generated polynomial is : coffes[3]x3 + coffes[2]x2 + coffes[1]x1 + coffes[0]x0
poly() returns the value of the polynomial as evaluated for the given x.
#include <iostream.h>
 #include <conio.h>
 #include <math.h>
//polynomial:  x**3 - 2x**2 + 5x - 1
int main(void)
{
  double array[] = { -1.0, 5.0, -2.0, 1.0};
  double result;

  result = poly(2.0, 3, array);
  cout<<"\nThe polynomial: x**3 - 2.0x**2 + 5x - 1 at 2.0 = "<<result;
  getch();
  return 0;
}
Output:

No comments:

Post a Comment