// filename:c2011-F-10-3-4-ex.c // original examples and/or notes: // (c) ISO/IEC JTC1 SC22 WG14 N1570, April 12, 2011 // http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf // C2011 Annex F.10.3.4 The frexp functions // compile and output mechanism: // (c) Ogawa Kiyoshi, kaizen@gifu-u.ac.jp, January.04, 2014 // compile errors and/or wornings: // 1 (c) Apple LLVM version 4.2 (clang-425.0.27) (based on LLVM 3.2svn) // Target: x86_64-apple-darwin11.4.2 //Thread model: posix // (c) LLVM 2003-2009 University of Illinois at Urbana-Champaign. // 2 gcc-4.9 (GCC) 4.9.0 20131229 (experimental) // Copyright (C) 2013 Free Software Foundation, Inc. #include <stdio.h> #include <math.h> #define MAX 5 double frexp_a(double value, int *exp){ *exp = (value == 0) ? 0 : (int)(1 + logb(value)); printf("logb(%f)=%d, scalbn(value, -(%d))=%f \n", value, (int)(1 + logb(value)), *exp, scalbn(value, -(*exp))); return scalbn(value, -(*exp)); } int main(void) { int exp,exp_a; double value[]={3.14159,-2.5,0,FP_INFINITE,FP_NAN },ret,ret_a; for(int i=0;i<MAX;i++){ ret = frexp( value[i], &exp ); ret_a = frexp_a( value[i], &exp_a ); printf("frexp( %f, exp ) = %f, exp = %d, frex_a(,)=%f exp_a=%d, true=%d \n", value[i], ret, exp,ret_a,exp_a, ((ret==ret_a)&(exp==exp_a)) ); } printf("Annex F.10.3.4 The frexp functions \n"); } // 1. output LLVM 3.2 & GCC4.9 //logb(3.141590)=2, scalbn(value, -(2))=0.785397 //frexp( 3.141590, exp ) = 0.785397, exp = 2, frex_a(,)=0.785397 exp_a=2, true=1 //logb(-2.500000)=2, scalbn(value, -(2))=-0.625000 //frexp( -2.500000, exp ) = -0.625000, exp = 2, frex_a(,)=-0.625000 exp_a=2, true=1 //logb(0.000000)=-2147483648, scalbn(value, -(0))=0.000000 //frexp( 0.000000, exp ) = 0.000000, exp = 0, frex_a(,)=0.000000 exp_a=0, true=1 //logb(2.000000)=2, scalbn(value, -(2))=0.500000 //frexp( 2.000000, exp ) = 0.500000, exp = 2, frex_a(,)=0.500000 exp_a=2, true=1 //logb(1.000000)=1, scalbn(value, -(1))=0.500000 //frexp( 1.000000, exp ) = 0.500000, exp = 1, frex_a(,)=0.500000 exp_a=1, true=1 //Annex F.10.3.4 The frexp functions // logb(3.141590)=2, scalbn(value, -(-266631570))=1072243193