C and C++ Programming Blog
RSS Feed

Math.h library in C Programming

Saturday, 9 February 2008 07:21

Today I will try to explore math.h (library) header file provided to the programmers by turbo C++ compiler. Mostly people try to implement their own implementation of these functions; this is due to the lack of knowledge about Math library provided by turbo C++. So here we go.

Find the Square root of a number

Function: double sqrt(double); It calculates the positive square root of the input value which is given as double data type.

#include <math.h>
#include <stdio.h>
int main(void)
{
double dSqrtNumber = 36.0
double dSqrtResult;   
dSqrtResult = sqrt(dSqrtNumber);
printf("The square root of the number ");
printf("%lf is: %lf\n",  dSqrtNumber, dSqrtResult);
return 0;
} 

Find the Power of a number

Function: double pow(double x, double y) It calculates the value of xy . If both the values are 0 then the value returned by the function is 0. If the result is a more bigger number; which a variable of type double can not contain; then the function return an error (HUGE_VAL).

#include <math.h>
#include <stdio.h>
int main(void)
{
double dValuex = 5.0, dValuey = 5.0;
double dResult;
result = pow(dValuex, dValuey);
printf("%lf power %lf is: %lf\n", dValuex, dValuey, dResult);
return 0;
}

Find Ceil and Floor values

Functions: double ceil(double x); double florr(double x);

ceil() finds the smallest integer value of the value passed as the parameter, but the returned values is not greater than the values passed to the function. Where as floor() finds the largest integer value not bigger than the value passed as the parameter.

#include <math.h>
#include <stdio.h>
int main(void)
{
double dNumber = 50.54;
double dFloorValue, dCeilValue;
dFloorValue = floor(dNumber);
dCeilValue = ceil(dNumber);€
printf("Original number is:     %5.2lf\n", dNumber);
printf("Number rounded down %5.2lf\n", dFloorValue);
printf("Number rounded up   %5.2lf\n", dCeilValue);
return 0;
}

 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Add comment

Add comment
(Will show your Gravatar icon)  

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



 
Copyright © by MYCPLUS. All rights reserved. #