C Quiz – 102 | Question 4
In the context of modulo operation (i.e. remainder on division) for floating point (say 2.1 and 1.1), pick the best statement.
(A) For floating point, modulo operation isn’t defined and that’s why modulo can’t be found.
(B) (2.1 % 1.1) is the result of modulo operation.
(C) fmod(2.1,1.1) is the result of module operation.
(D) ((int)2.1) % ((int)1.1) is the result of modulo operation.
Explanation: % works on integer types only not for floating types. Typecasting to integer type might approximate the intended result but it won’t produce the correct result.
Basically the fmod(x,y) function returns the value x − ny, for some integer n such that, if y is nonzero, the result has the same sign as x and magnitude less than the magnitude of y. fmod() is declared in “math.h” and its prototype is “double fmod(double x, double y)“. For float and long double also, modulo has been implemented in math.h library through fmodf() and fmodl().
Quiz of this Question