Friday, June 22, 2012

Basic Mathematical Functions in ANSI C

To use the basic mathematical functions described below, you will need to include the mathematics header:
#include<math.h>
1.Absolute value of a numeric variable
ANSI C provides a wide range of functions that can be used for obtaining the absolute value of any numeric primitive data type.

Example:
 int iNumber = -200;
 long int liNumber = -200l;
 long long int lliNumber = -200L;
 float fNumber = -200.0f;
 double dNumber = -200.0;
 long double ldNumber = -200.0l;
 
 /*Absolute value of an int*/
 int absiNumber = abs(iNumber);
 /*Absolute value of a long int*/
 long int absliNumber = labs(liNumber);
 /*Absolute value of a long long int*/
 long long int abslliNumber = llabs(lliNumber);
 /*Absolute value of a float*/
 float absfNumber = fabsf(fNumber);
 /*Absolute value of a double*/
 double absdNumber =  fabs(dNumber);
 /*Absolute value of a long double*/
 long double absldNumber = fabsl(ldNumber);
2.Computing the quotient and remainder of a division
ANSI C mathematical library also provides support for finding the quotient and remainder of a integer division.

In ANSI C (and the majority of programming languages) the division of 2 integers by using the divide operator (\) returns only the quotient of the division, while the remainder is lost.

When it comes to floating point numbers, there are available two functions for remainder calculations:
  • double fmod(double x, double y) and its variants for float and long double (fmodf, fmodl) which computes the unsigned remainder of the division x/y.
  • double remainder(double x, double y) and its variants for float and long double (remainderf, remainderl) which computes the signed remainder of the division x/y (the result will be negative if (x/y)>(y/2) 
Example:
#include<math.h>
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char** argv)
{
   int ia = 256, ib = 44;
   long int lia = 2560, lib = 440;
   long long int llia = 256000, llib = 44000;
   float fa = 256.3f, fb = 44.9f;
   double da = 2560.33, db = 440.99;
   long double lda = 25600.333l, ldb = 4400.999l;
   /* Computes the remainder and quotient of 2 int numbers*/
   div_t  ria  = div(ia,ib);
   /* Computes the remainder and quotient of 2 long int numbers*/
   ldiv_t rlia = ldiv(lia,lib);
   /* Computes the remainder and quotient of 2 long long int numbers*/
   lldiv_t rllia = lldiv(llia, llib);
   /* Computes the remainder of floating point numbers using fmod*/
   float fRem = fmodf(fa,fb);
   float dRem = fmod(da,db);
   float ldRem = fmodl(lda,ldb);
   /* Computes the remainder of floating point numbers using remainder*/
   float fRem2 = remainderf(fa,fb);
   float dRem2 = remainder(da,db);
   float ldRem2 = remainderl(lda,ldb);
   /* Prints the results */
   printf("[int] Remainder: %d  Quotient: %d\n",
          ria.rem, ria.quot);
   printf("[long] Remainder: %ld  Quotient: %ld\n",
          rlia.rem, rlia.quot);
   printf("[long long] Remainder: %lld  Quotient: %lld\n",
          rllia.rem, rllia.quot);
   printf("[float] Remainder1: %f Remainder2: %f\n",fRem, fRem2);
   printf("[double] Remainder1: %f Remainder2: %f\n",dRem, dRem2);
   printf("[long double] Remainder1: %lf Remainder2: %lf\n",ldRem, ldRem2);
   return 0;
}
/*Output
[int] Remainder: 36  Quotient: 5
[long] Remainder: 360  Quotient: 5
[long long] Remainder: 36000  Quotient: 5
[float] Remainder1: 31.799980 Remainder2: -13.100021
[double] Remainder1: 355.380005 Remainder2: -85.610001
[long double] Remainder1: 3595.337891 Remainder2: -805.661011
 */
3.Other useful floating-point functions
ANSI C mathematical library also provides a few other floating point functions which may prove very useful when building your applications:
 float fa = 0.3f, fb = 0.5f;
 double da = 0.5, db = 0.6;
 long double la = 0.5l, lb=0.5l;
 /*Returns the smallest of the two parameters*/
 float fmin = fminf(fa,fb);
 double dmin = fmin(da,db);
 long double ldmin = fminl(la,lb);
 /*Returns the largest of the two parameters*/
 float fmax = fmaxf(fa,fb);
 double dmax = fmax(da,db);
 long double lmax = fmaxl(la,lb);
 /*Returns the positive difference between the two parameters
  *If x <= y, the result is always equals to 0, otherwise it is x - y.*/
 float fdist = fdimf(fa,fb);
 double ddist = fdim(da,db);
 long double ldist = fdiml(la,lb);
Mathematical notions:

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger