সি-এ ইন্টিজারের জন্য % অপারেটর আছে, কিন্তু ফ্লোটিং-পয়েন্ট নম্বর (float, double, long double) এর জন্য % কাজ করে না। ফ্লোটিং-পয়েন্ট রিমেইন্ডার বের করতে <math.h>-এর remainder(), fmod(), বা remquo() ফাংশন ব্যবহার করা হয়।
remainder() ফাংশন
double remainder(double x, double y);
float remainderf(float x, float y);
long double remainderl(long double x, long double y);
ফর্মুলা: remainder(x, y) = x - rquote × y
এখানে rquote = x/y নিকটতম পূর্ণসংখ্যায় রাউন্ড করা (ties to even)।
উদাহরণ
#include <stdio.h>
#include <math.h>
int main() {
double x = 14.5, y = 4.1;
double res = remainder(x, y);
printf("Remainder of %lf/%lf is: %lf\n", x, y, res);
x = -34.50; y = 4.0;
res = remainder(x, y);
printf("Remainder of %lf/%lf is: %lf\n", x, y, res);
x = 65.23; y = 0.0;
res = remainder(x, y);
printf("Remainder of %lf/%lf is: %lf\n", x, y, res); // ডিভাইড বাই জিরো
return 0;
}
আউটপুট
Remainder of 14.500000/4.100000 is: -1.900000
Remainder of -34.500000/4.000000 is: 1.500000
Remainder of 65.230000/0.000000 is: -1.#IND00 // NaN (Not a Number)
fmod() vs remainder() পার্থক্য
| ফাংশন | ফর্মুলা | কোতাহলে রাউন্ড | সাইন |
|---|---|---|---|
fmod(x,y) | x - trunc(x/y) × y | ট্রান্কেট (শূন্যের দিকে) | x-এর সাইন অনুসরণ করে |
remainder(x,y) | x - round(x/y) × y | নিকটতম পূর্ণসংখ্যা (ties to even) | ফলাফলের নিজস্ব সাইন |