From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14766 invoked by alias); 7 May 2006 17:42:59 -0000 Received: (qmail 14750 invoked by uid 22791); 7 May 2006 17:42:56 -0000 X-Spam-Check-By: sourceware.org Received: from wr-out-0506.google.com (HELO wr-out-0506.google.com) (64.233.184.224) by sourceware.org (qpsmtpd/0.31) with ESMTP; Sun, 07 May 2006 17:41:45 +0000 Received: by wr-out-0506.google.com with SMTP id 36so845065wra for ; Sun, 07 May 2006 10:41:44 -0700 (PDT) Received: by 10.54.118.4 with SMTP id q4mr1381790wrc; Sun, 07 May 2006 10:40:35 -0700 (PDT) Received: from ?192.168.2.51? ( [141.157.250.229]) by mx.gmail.com with ESMTP id 67sm1077453wra.2006.05.07.10.41.42; Sun, 07 May 2006 10:41:42 -0700 (PDT) Message-ID: <445E3155.20209@gmail.com> Date: Sun, 07 May 2006 17:42:00 -0000 From: Jim Cromie User-Agent: Thunderbird 1.5.0.2 (X11/20060420) MIME-Version: 1.0 To: "Nelson H. F. Beebe" CC: gcc-help@gcc.gnu.org Subject: Re: compile-time conversion of floating-point expressions to long longs References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2006-05/txt/msg00077.txt.bz2 Nelson H. F. Beebe wrote: > Jim Cromie asks on Wed, 03 May 2006 12:17:38 -0400: > > >>> ... >>> Are there any macros that can tear into a floating point number and >>> pull out the exponent and mantissa ? >>> ... >>> > > Yes, the C89 and C99 ISO C Standards include ldexp(x,n), which forms x > * 2**n, and "f = frexp(x,&n);", which returns the fraction as a value > in [1/2,1) (or 0 if x is zero) and the exponent of 2 in n. Both are > exact, and can be implemented reasonably efficiently. > thank you for the thorough answer. yes, frexp() is almost what I want. heres basically what I tried to do with it: #include #include #include /* evaluate these constants at compile-time ?!? */ float f = 3.14159 * 2 * 100; int myexp; double mant = frexp(f,&myexp); /* this confirms gcc's unwillingness to run libmath routines at compile-time */ float ff = ldexp(2,8); int main(int c, char** v) { printf("float %g mantissa %f exp %d \n", f, mant, myexp); } as Id feared, it wouldnt compile; $ make fp-comp cc -g fp-comp.c -o fp-comp fp-comp.c:37: error: initializer element is not constant make: *** [fp-comp] Error 1 (the code above is slightly edited for clarity - err line numbers dont match, but result does ;-( I also tried these additions to compile cmd: -fkeep-static-consts -fmerge-constants -fmerge-all-constants -fsingle-precision-constant So it seems that the compiler just wont call the function for me at compile-time, and fold the constant into the code for me. Id love to hear differently. tia jimc