C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - General Questions (Q.No. 1)
1.
Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1 ?
rem = 3.14 % 2.1;
rem = modf(3.14, 2.1);
rem = fmod(3.14, 2.1);
Remainder cannot be obtain in floating point division.
Answer: Option
Explanation:

fmod(x,y) - Calculates x modulo y, the remainder of x/y.
This function is the same as the modulus operator. But fmod() performs floating point divisions.

Example:


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

int main ()
{
  printf ("fmod of 3.14/2.1 is %lf\n", fmod (3.14,2.1) );
  return 0;
}

Output:
fmod of 3.14/2.1 is 1.040000

Discussion:
141 comments Page 1 of 15.

Ron said:   9 years ago
@Ajith and @Rey

rem is just the variable name for Remainder.It is not a function or anything and it's not compulsory to write rem. You can use any variable name. Like R, A, Remain, B etc like literally anything expect keywords.

@Anjali

return 0 is used to denote that you are not returning any arguments from that function.
Eg; you declare int main it means that your main is of int datatype so it must return some value.
but if you declare return 0 it says you are not returning any value. You can use void instead of writing return 0.

Example 1:

int main()
{
printf("My name is Ron\n");
return 0;
}

Example 2:

void main()
{
printf("My name is Ron\n");
}

Siva Krishna said:   1 decade ago
@Vani :

In the inner printf statement, there are 4 characters.
The output of a printf stmt is a integer. that number is equal to the number of Characters that the printf statement printed.

For example:

printf("%d",printf("ABC"));
o/p= ABC4
-----------
printf("%d",printf("A"));
o/p= A1
-----------
printf("%d",printf("AB"));
o/p= AB2
-----------
printf("%d",printf("ABC10"));
o/p= ABC105
(here 10 is taken as two different characters.)
-----------

Prem kumar said:   1 decade ago
modf->Breaks x into two parts: the integer part (stored in the object pointed by intpart) and the fractional part (returned by the function).

Each part has the same sign as x.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
/* modf example */
#include <stdio.h>
#include <math.h>

int main ()
{
double param, fractpart, intpart;

param = 3.14159265;
fractpart = modf (param , &intpart);
printf ("%lf = %lf + %lf \n", param, intpart, fractpart);
return 0;
}


Output:

3.141593 = 3.000000 + 0.141593

Ron said:   9 years ago
@Gayathridhiravidamani.

In your first program, you have declared the variable a, b & c as float but you have used the format specifier as %d. You cannot use %d for floating point numbers. You have to use %f for float. That is why your program is showing error.

In your second program, you have done it rite, but you took % for finding the remainder of 3.14% 2.1% in for int, for float you have to use fmod () and you have use %d there. So, the compiler will take 3 from 3.14 and 2 from 2.1. So, 3%2 is 1.

Angela said:   9 years ago
This is the synopsis for fmod and fmodf:

#include <math.h>
double fmod(double x, double y)
float fmodf(float x, float y)

//The fmod() function computes the floating-point remainder of x/y.

An application wishing to check for error situations should set error number to 0 before calling fmod(). If an error is non-zero in return, or the return value is NaN, an error has occurred.

The fmodf() function is a single-precision version of fmod().

Subrat said:   8 years ago
@Dinesh Kumar.

#include<stdio.h>
int main()
{
int a=1;
printf("%d %d %d",a,a++,++a);
return 0;
}
Output : 3 2 3.
But logically 3 2 2.

Can any explain how 3 2 3 comes?

Your logic is true,function data passing is from right to left. So first ++a execute then a++,then a.
But the current value of a will be substitute in pre-increment operator always.
So the output will be 3 2 3.

Afsia said:   8 years ago
Difference between modf and fmod is fmod gives remainder when x divided y. Fmod stands for floating modulus while in modf the parts spilt in two modfvalue into an integer and a fractional part. The fraction is returned by the modf function and the integer part is stored in the iptr variable. Iptr means pointer to the variable stored in an integer.

Venkat said:   8 years ago
#include<stdio.h>
#include<math.h>
main()
{
float a=1.2,b=2.3,c;
c=a%b;
printf("%f\n",fmod(a,b));
}
~

Test1.c:6:4: error: invalid operands to binary % (have \'float\' and \'float\')
user@user:~/c/test$

I have tested in GCC it gives error so finally conclusion is % this operator not possible for floating points

Vinod kumar said:   1 decade ago
fmod(x,y) is floating point remainder of x/y,with the same sign as x.If y=0 ,the result is implementation-defined
e.g.

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

int main ()
{
printf ("fmod of 3.14/2.1 is %lf\n", fmod (3.14,2.1) );
return 0;
}
ans output:fmod of 3.14/2.1 is 1.040000

Trupti Patil said:   1 decade ago
Function written inside block is called local function & used only within that block. Function declared before main program starts below the header files is called global function.

Scope of local function is only within that part of the program it can not be access in other function.


Post your comments here:

Your comments will be displayed after verification.