#include #include #include #include using namespace std; void printme (const char *fmt, float f) { union { float f; int i; } cvt; cvt.f = f; printf (fmt, f, cvt.i); } int main (void) { int rnd_mode; float fzero; float fpi; float falmostpi = 3.1415924; float fatan2, fasin; float fdiff; cout << "Please enter '0.0' and hit " << endl; cin >> fzero; cout << "--------------------" << endl; rnd_mode = fegetround(); cout << "Default round mode is " << rnd_mode << endl; fesetround(FE_TOWARDZERO); rnd_mode = fegetround(); cout << "TOWARDZERO round mode is " << rnd_mode << endl; fesetround(FE_TONEAREST); rnd_mode = fegetround(); cout << "TONEAREST round mode is " << rnd_mode << endl; cout << "--------------------" << endl; printme ("TONEAREST : (float) almostpi = %.7f (0x%08x)\n", float (falmostpi + fzero)); printme ("TONEAREST : (float) M_PI = %.7f (0x%08x)\n", float (M_PI + fzero)); fesetround(FE_TOWARDZERO); printme ("TOWARDZERO: (float) M_PI = %.7f (0x%08x)\n", float (M_PI + fzero)); printme ("TOWARDZERO: (float) almostpi = %.7f (0x%08x)\n", float (falmostpi + fzero)); cout << "--------------------" << endl; cout << "Computed with TONEAREST" << endl; fesetround(FE_TONEAREST); fpi = float (M_PI + fzero); fatan2 = atan2f (fzero, float (fzero - 1)); fdiff = fatan2 - fpi; printme ("atan2f (0.0f, -1.0f) = %.8f (0x%08x)\n", fatan2); printme ("atan2f (0.0f, -1.0f) - (float) M_PI = %.8f (0x%08x)\n", fdiff); fasin = 2.0f * asinf (float (1.0f + fzero)); fdiff = fasin - fpi; printme ("2*asinf (1.0f) = %.8f (0x%08x)\n", fasin); printme ("2*asinf (0.0f, -1.0f) - (float) M_PI = %.8f (0x%08x)\n", fdiff); cout << "--------------------" << endl; cout << "Computed with TOWARDZERO" << endl; fesetround(FE_TOWARDZERO); fpi = float (M_PI + fzero); fatan2 = atan2f (fzero, float (fzero - 1)); fdiff = fatan2 - fpi; printme ("atan2f (0.0f, -1.0f) = %.8f (0x%08x)\n", fatan2); printme ("atan2f (0.0f, -1.0f) - (float) M_PI = %.8f (0x%08x)\n", fdiff); fasin = 2.0f * asinf (float (1.0f + fzero)); fdiff = fasin - fpi; printme ("2*asinf (1.0f) = %.8f (0x%08x)\n", fasin); printme ("2*asinf (0.0f, -1.0f) - (float) M_PI = %.8f (0x%08x)\n", fdiff); cout << "--------------------" << endl; return 0; }