In C, which statement correctly obtains the remainder when dividing the floating values 5.5 by 1.3? Select the standard-library function designed for floating remainders.
-
Arem = (5.5 % 1.3)
-
Brem = modf(5.5, 1.3)
-
Crem = fmod(5.5, 1.3)
-
DError: we can't divide
Answer
Correct Answer: rem = fmod(5.5, 1.3)
Explanation
Introduction:This question checks knowledge of the correct function to compute the remainder of a floating-point division in C.
Given Data / Assumptions:
- Dividend: 5.5
- Divisor: 1.3
- Language: C with math.h available
Concept / Approach:The % operator is defined only for integer operands in C. For floating-point remainders, the standard library provides fmod in math.h, which computes the remainder with the same sign as the dividend according to its specification.
Step-by-Step Solution:1) Include math.h.2) Call fmod: double r = fmod(5.5, 1.3);3) The result r satisfies 5.5 = q * 1.3 + r where q is truncated toward zero and |r| < |1.3|.
Verification / Alternative check:You can verify by computing q = trunc(5.5 / 1.3) and then r = 5.5 - q * 1.3 and comparing with fmod's output.
Why Other Options Are Wrong:(5.5 % 1.3): % is not defined for floating types.modf(5.5, 1.3): modf splits a float into fractional and integer parts; it does not compute a division remainder.Error: we can't divide: Division is valid; we just need the proper remainder function.
Common Pitfalls:Confusing modf with fmod and forgetting to link with the math library on some toolchains. Also watch platform-defined behaviors for negative operands; fmod's remainder sign follows the dividend.
Final Answer:rem = fmod(5.5, 1.3)