এখানে আমরা C++ এ fesetround() এবং fegetround() পদ্ধতি দেখতে পাব। এই পদ্ধতিগুলো cfenv লাইব্রেরিতে পাওয়া যাবে।
fesetround() পদ্ধতিটি নির্দিষ্ট ফ্লোটিং পয়েন্ট রাউন্ডিং দিককে বর্তমান রাউন্ডিং দিকনির্দেশে সেট করতে ব্যবহৃত হয়। এটি rint(), nearbyint() এবং C++ এ কিছু অন্যান্য রাউন্ডিং ফাংশনের সাথে ব্যবহৃত হয়।
সিনট্যাক্স নিচের মত -
int fesetround(int round);
রাউন্ডটি এই FE_TONEAREST, FE_DOWNWARD, FE_UPWARD ইত্যাদির মধ্যে হতে পারে৷ এই ফাংশনটি 0 প্রদান করে যখন রাউন্ডিং দিকনির্দেশ সফলভাবে প্রয়োজনীয় পদ্ধতিতে প্রয়োগ করা হয়৷
উদাহরণ
#include <cfenv >
#include <cmath>
#include <iostream>
using namespace std;
main() {
double x = 4.7, ans;
fesetround(FE_TONEAREST); //round to nearest integer
ans = rint(x);
cout << "Nearest Integer is: " << ans << endl;
fesetround(FE_TOWARDZERO); //rounding towards zero
ans = rint(x);
cout << "Rounding towards 0, value is: " << ans << endl;
fesetround(FE_DOWNWARD); //rounding to downwards
ans = rint(x);
cout << "Nearest Integer below the number: " << ans << endl;
fesetround(FE_UPWARD); //rounding to upwards
ans = rint(x);
cout << "Nearest Integer above the number: " << ans << endl;
} আউটপুট
Nearest Integer is: 5 Rounding towards 0, value is: 4 Nearest Integer below the number: 4 Nearest Integer above the number: 5
এখন দেখা যাক fegetround() পদ্ধতিটি ফ্লোটিং পয়েন্ট রাউন্ডিং ম্যাক্রো পেতে ব্যবহৃত হয় যা বর্তমান রাউন্ডিং দিকনির্দেশের সাথে মিলে যায়। এই ফাংশনটি rint(), nearbyint() এবং C++ এ কিছু অন্যান্য রাউন্ডিং পদ্ধতির সাথে ব্যবহার করা হয়।
সিনট্যাক্স নিচের মত -
int fegetround();
এটি ফ্লোটিং পয়েন্ট রাউন্ডিং ম্যাক্রোর সাথে সম্পর্কিত সংখ্যা প্রদান করে।
- FE_DOWNWARD
- FE_TONEAREST
- FE_TOWARDZERO
- FE_UPWARD
উদাহরণ
#include <cfenv >
#include <cmath>
#include <iostream>
using namespace std;
void float_direction() {
switch (fegetround()) {
case FE_TONEAREST:
cout << "Macro is: FE_TONEAREST";
break;
case FE_DOWNWARD:
cout << "Macro is: FE_DOWNWARD";
break;
case FE_UPWARD:
cout << "Macro is: FE_UPWARD";
break;
case FE_TOWARDZERO:
cout << "Macro is: FE_TOWARDZERO";
break;
default:
cout << "unknown";
};
cout << endl;
}
main() {
double x = 4.7, ans;
fesetround(FE_TONEAREST); //round to nearest integer
ans = rint(x);
cout << "Nearest Integer is: " << ans << endl;
float_direction();
fesetround(FE_TOWARDZERO); //rounding towards zero
ans = rint(x);
cout << "Rounding towards 0, value is: " << ans << endl;
float_direction();
fesetround(FE_DOWNWARD); //rounding to downwards
ans = rint(x);
cout << "Nearest Integer below the number: " << ans << endl;
float_direction();
fesetround(FE_UPWARD); //rounding to upwards
ans = rint(x);
cout << "Nearest Integer above the number: " << ans << endl;
float_direction();
} আউটপুট
Nearest Integer is: 5 Macro is: FE_TONEAREST Rounding towards 0, value is: 4 Macro is: FE_TOWARDZERO Nearest Integer below the number: 4 Macro is: FE_DOWNWARD Nearest Integer above the number: 5 Macro is: FE_UPWARD