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 11 of 15.

PRIYANKA said:   10 years ago
Modulus support only integers. For ex: If we take 16.25/2.5 then in modulus it take only 16 from 16.25 and from 2.5 it takes 2 and gives answer 4. It performs on integers only.

fmod gives reminder in float.

Hope it clear.

Karri jagapathibabu said:   9 years ago
Please tell me what is the use of fmod?

Shikha said:   9 years ago
Is fmod used in PHP?

Bhanu said:   9 years ago
Nice explanation @Prem Kumar. Thank you.

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().

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.

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");
}

Gaurav said:   9 years ago
fmod() function is used to find out remainder of float numbers, not integer and normal operation like rem=12%7, it gives remainder only integer not float.

So option 1 is wrong and the correct is c.

I think you understand.

Karthi said:   9 years ago
What does the fmod represent here?

How could the answer be c but not a?

Vishal said:   9 years ago
C is the correct answer as % operator don't work on floating point numbers so A is incorrect.


Post your comments here:

Your comments will be displayed after verification.