fmod( ) – Modulus function for fractions – CSKC
Sign In

Search

Code Learning #CPP#2143    siteicon   siteicon  

fmod( ) – Modulus function for fractions

Using the specially created modulus function fmod() for fractions like float and double data type.

Learning Objectives

  • Learning to use fmod() function of math.h  for fractions as well as for integers.

Source Code

TC++ #2143

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Code Understanding

double n=7.32, d=5.12;
variables for testing  fmod() on fractions are declared here with n to be used in numerator and d to be used in denominator.

int nn=7, nd=5;
variables for testing  fmod() on integers are declared here with nn to be used in numerator and nd to be used in denominator.

cout<<“fmod(“<<n<<“,”<<d<<“) = “<<fmod(n,d)<<endl;
Here fmod(n,d) will print the remainder of n/d if fractional numbers of double and float format are used.

cout<<“fmod(“<<n<<“,”<<0<<“) = “<<fmod(n,0)<<endl;
This is to test the divide by zero condition of the function. In the case NaN (Not a number) will result.

cout<<“fmod(“<<nn<<“,”<<nd<<“) = “<<fmod(nn,nd)<<endl;
Here we are testing fmod() for integer case of nn and nd. This works in the same way as nn%nd would have worked.

Notes

  • Basic fmod() function is designed for double data type. For other equivalent functions of fmodf() fmodl() are called automatically when float or long double data type is encountered. The difference here is that function return types will be float and long double when the inputs are like that.
  • It is preferable to use the header cmath for modern compilers so that full compatibility of  data types can be maintained.


Suggested Filename(s): fmod.cpp



Share

Created: 3-Jan-2018 19:30 | Updated: 3-Jan-2018 19:30|