/***********************************************/ /* T.Kouya's GSL sample program collection */ /* View and control */ /* IEEE754 floating-point arithmetic */ /* Written by Tomonori Kouya */ /* */ /* Version 0.01: 2007-10-04 */ /***********************************************/ #include #include #include // since C99 #include int main(void) { double a, b = cos(1.0 / 10.0), c = sin(1.0 / 11.0); printf("b -> Decimal = %25.17e\n", b); printf(" Binary = "); gsl_ieee_printf_double(&b); printf("\n"); printf("c -> Decimal = %25.17e\n", c); printf(" Binary = "); gsl_ieee_printf_double(&c); printf("\n\n"); /* setup ronding mode */ /* set IEEE754 double precition floating-point number */ printf("-- Round to -Infinity\n"); fesetround(FE_DOWNWARD); // Round to -Infinity a = b + c; printf("b + c -> Decimal = %25.17e\n", a); printf(" Binary = "); gsl_ieee_printf_double(&a); printf("\n\n"); printf("-- Round to +Infinity\n"); fesetround(FE_UPWARD); // Round to +Infinity a = b + c; printf("b + c -> Decimal = %25.17e\n", a); printf(" Binary = "); gsl_ieee_printf_double(&a); printf("\n\n"); printf("-- Round to Zero\n"); fesetround(FE_TOWARDZERO); // Round to Zero a = b + c; printf("b + c -> Decimal = %25.17e\n", a); printf(" Binary = "); gsl_ieee_printf_double(&a); printf("\n\n"); printf("-- Round to Nearest (default)\n"); fesetround(FE_TONEAREST); // Round to Nearest (default) a = b + c; printf("b + c -> Decimal = %25.17e\n", a); printf(" Binary = "); gsl_ieee_printf_double(&a); printf("\n\n"); return 0; }