Thursday, June 23, 2011

C- Maths Function!


We already seen some string built-in-function. All programming languages having certain amount of built-in-function, which written as subroutine or function to solve a certain requirement, while writing a program you can, call these functions by sending arguments that required or without any arguments. It is not that we cannot solve these by writing our own routine but these functions will save your time and space. Built-in-functions come in different types depending upon data types – Numeric, Date, String, Database etc. Numeric or math function to solve a mathematics routine like square root, power and others. Many math functions in C, the return value is float or double type but you can take help of explicit type casting to get it in integer. In C or C++, you have to include "math.h". but for min() and max() in C you require "stdlib.h" or often "complex.h"

Note : Num - is the Integer numeric argument and Fnum is floating point arguments that you have to pass. For larger values, declare the variable as double.


abs() - abs (a macro) gets the absolute value of an integer, All of these routines return the absolute value of their argument.
 
Declaration:   int abs(int x);

 Return Value:    An integer in the range 0 to 32,767;
  
 /* Example: abs */

#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
 {
          int num = -456;
          clrscr();
          printf("Number: %d  absolute value: %d\n", number, abs(num));
          getch();
 }

Output :
Number: -456 absolute value: 456; / * Example: pow()  eithout built-in */




ceil() / floor() - ceil finds the smallest integer not < x. floor finds the largest integer not > x. ceil round up the given value where floor round down the same.
Return Value: Both ceil and floor return the integer.

/* Example: ceil() / floor() example */

#include <math.h>
#include <stdio.h>
#include <conio.h>

 void main()
  {
          float num = 123.54,fl,cl;
          cl = ceil(num);
          fl = floor(num);     
          clrscr();
          printf("Original Number     %5.2f\n", num);
          printf("Number after ceil :  %5.2f\n", cl);
          printf("Number after floor :  %5.2f\n", fl);
          num=-123.54;
          printf("\nOriginal Number     %5.2f\n", num);
          printf("Number after ceil %5.2f\n", cl);
          printf("Number after floor %5.2f\n", fl);
          getch();
  }


Output :

Original Number     123.54
Number after ceil :  124.00
Number after floor :  123.00

Original Number     -123.54
Number after ceil -123.00
Number after floor -124.00


cos() / sin() / tan() -  Cosine, sine, and tangent functions. cos compute the cosine of the input value. sin the sine of the input value and tan calculate the tangent of the input value

 Declaration:   Real:     cos(x);sin(x);tan(x);
 Remarks:  Angles are specified in radians.

 Return Value:   On success, cos  return the cosine of the input value (in the range -1 to 1).  sin and sinl return the sine of the input value (in the range -1 to 1).  tan returns the tangent of x, sin(x)/cos(x).


/* Example: cos/sine/tan example */

#include <stdio.h>
#include <conio.h>
#include <math.h>

 void main()
  {
            int ang;
            float rad,sn,cs,tn;
            clrscr();
            printf("\nEnter an angle : ");
            scanf("%d",&ang);
            rad=ang*3.1415/180.0;
            sn=sin(rad);
            cs=cos(rad);
            tn=tan(rad);
            printf("\nAngle : %d", ang);
            printf("\nRadian : %8.5f", rad);
            printf("\nSine : %5.2f", sn);
            printf("\nCos : %5.2f", cs);
            printf("\nTan : %5.2f", tn);
            getch();
  }



Output:

Enter an angle : 45

Angle : 45
Radian :  0.78537
Sine :  0.71
Cos :  0.71
Tan :  1.00


exp() -  exp calculates e to the xth power. exp calculates the exponential function e**x.
 
 Declaration: float exp(float x);

Return Value:  On success, exp returns e**x


 /* Example: exp() */

 #include <stdio.h>
 #include <conio.h>
 #include <math.h>

 void main()
   {
          float result;
          float x = 2.0;
          clrscr();
          result = exp(x);
          printf("'e' raised to the power \ of %f (e ^ %f) = %f\n", x, x, result);
          getch();
  }


Output :

'e' raised to the power  of 2.000000 (e ^ 2.000000) = 7.389056



fmod() -  Calculates x modulo y, the remainder of x/y. To find remainder of two real value.

 Declaration:  fmod(float x, float y);

/* Example: fmod() */
 #include <conio.h>
 #include <stdio.h>
 #include <math.h>

 void main()
   {
          double x = 5.0, y = 2.0;
          double result;
          clrscr();
          result = fmod(x,y);
          printf("The remainder of (%lf / %lf) is %lf\n", x, y, result);
          getch();
    }


Output :

The remainder of (5.000000 / 2.000000) is 1.000000

modf () - splits double into integer and fraction parts.  modf breaks the double x into two parts: the integer and the fraction. It stores the integer in ipart and returns the fraction.

 Declaration: modf(double x, double *ipart);


/* Example: modf() */

 #include <conio.h>
 #include <math.h>
 #include <stdio.h>

 void main(void)
   {
          double f, i, num = 1000.567;
          clrscr();
          f = modf(num, &i);
          printf("The whole and fractional parts of  %lf are %lf and %lf\n",num, i, f);
          getch();
   }


Output :

The whole and fractional parts of 100000.570312 are 0.000000 and 0.570312


log() / log10() - Natural logarithm function, log10  = common logarithm function

 Return Value:   On success, log return the natural log of x, log10 the return log (base 10) of x

/* Example:  log()/log10() */

#include <conio.h>
#include <math.h>
#include <stdio.h>

 void main()
  {
          float result;
          float x = 9.5672;
          clrscr();
          result = log(x);
          printf("\nNatural log :\n");
          printf("The natural log of %f is %f\n", x, result);
          x = 8.9023;
          result = log10(x);
          printf("\nLog base 10 :\n");
          printf("The natural log of %f is %f\n", x, result);
          getch();
  }

Output:

Natural log :
The natural log of 9.567200 is 2.258341

Log base 10 :
The natural log of 8.902300 is 0.949502



 pow() -  Power function calculate, x to the y (x**y)

 Declaration: pow(float x, float y);
                                     
 Return Value: On success, pow return the value  calculated, x**y.

/*  Example: pow() */

 #include <conio.h>
 #include <math.h>
 #include <stdio.h>

  void main(void)
    {
           float x = 2.0, y = 3.0;
          clrscr();
           printf("%lf raised to %f is %f\n", x, y, pow(x, y));
           getch();
   }


Output :
2.000000 raised to 3.000000 is 8.000000



sqrt() -  Calculates square root. sqrt calculates the positive square root of the input value.

 Declaration: sqrt(float x);

 / * Example: sqrt() */

 #include <math.h>
 #include <conio.h>
 #include <stdio.h>

  void main()
    {
          float x = 4.0, result;
          clrscr();
          result = sqrt(x);
          printf("The square root of  %f  is  %f\n", x, result);
          getch();
    }

Output :

The square root of  4.000000  is  2.000000



We can write our own subroutine for solving some of those maths function. I will put a few like pow(), sqrt(), sin() and abs for it.



/ * Example: sqrt()  without built-in */

#include<stdio.h>
#include<conio.h>
 void main()
  {
          float a,b,c=0.00001,prod,k;
          clrscr();
          printf("\nEnter the Value : ");
          scanf("%f",&k);
          a=k;
          prod=a*a;
          while(prod-k>=c)
               {

                        b=(a+(k/a))/2;
                        a=b;
                        prod =a*a;
               }
          printf("Square Root  Of  %f  is =  %f ",k,a);
          getch();
  }

Output :

Enter the Value : 15
Square Root  Of  15.000000  is =  3.872984



/ * Example: abs()  without built-in */

#include<stdio.h>
#include<conio.h>

 void main()
  {
          float a,b;
          clrscr();
          printf("\nEnter the Value : ");
          scanf("%f",&a);
          b=a;
          if(b<0)
            {
                b=b*-1;
            }
          printf("Input Value %f and ab;ousute value is =  %f ",a,b);
          getch();
}

Output :

Enter the Value : -12
Input Value -12.000000 and ab;ousute value is =  12.000000

Enter the Value : 23
Input Value 23.000000 and ab;ousute value is =  23.000000


/ * Example: pow()  without built-in */

#include<stdio.h>
#include<conio.h>
 void main()
  {
          float x,y,s=1;
          int i;
          clrscr();
          printf("\nEnter X and Y for x^y : ");
          scanf("%f%f",&x,&y);
          for(i=1;i<=y;i++)
            {
                 s=s*x;
            }
          printf("%f ^ %f = %f ",x,y,s);
          getch();
 }

Output :

Enter X and Y for x^y : 2  4

2.000000 ^ 4.000000 = 16.000000



/ * Example: sin()  without built-in */

#include<stdio.h>
#include <math.h>
#include<conio.h>
 void main()
  {
          float rad,f=1,s;
          int ang,i;
          clrscr();
          printf("\nEnter an Angle : ");
          scanf("%d",&ang);
          rad=ang*3.1415/180.0;
          s=rad;
          for(i=3;i<=11;i+=2)
            {
                 f=f*i*(i-1)*(-1);
                 s=s+pow(rad,i)/f;
            }
          printf("Sine of %d angle = %f",ang,s);
          getch();
}


Output :

Enter an Angle : 90
Sine of 90 angle = 1.000000




No comments: