Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: double-precision dissection Message-ID: <4420@goanna.cs.rmit.oz.au> Date: 3 Dec 90 07:32:59 GMT References: <12131@life.ai.mit.edu> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 48 In article <12131@life.ai.mit.edu>, bson@rice-chex.ai.mit.edu (Jan Brittenson) writes: > I'm looking for a fairly portable way of extracting the exponent of > a double, such that: > (int) EXP_OF(1.2345e-17) == -17 > > I'm using SunOS 4.1, and didn't find any clues at all as to how to accomplish > this in the man pages. (I.e. no functions for dissecting doubles.) Use > of pure -declared functions/defined would be perfect. I can tell from this that you didn't look at man floating_to_decimal which covers single_to_decimal(), double_to_decimal(), extended_to_decimal(). Those functions are not portable, but if you don't mind an #if in your program it would be silly not to avail yourself of correct rounding when you can get it. Also in the SunOS 4.1 manual pages is the ANSI C function frexp(): double frexp(double x, int *pexp) which returns y and sets *pexp such that y*2**(*pexp) == x and y .in. [1/2,1) or x == y == 0 which counts as "dissecting" a double in my book. The very simplest dodge is to exploit the fact that you apparently want a decimal exponent. Very well, what's wrong with sprintf()? #include /* defines DBL_DIG */ #include /* defines sprintf() */ #include /* defines atoi() */ int decimal_exponent(double x) { char buffer[sizeof "+1.E-99999" + DBL_DIG]; (void) sprintf(buffer, "%+.*e", DBL_DIG, x); return atoi(buffer + 3 + DBL_DIG); } Beware: I haven't tested that. -- I am not now and never have been a member of Mensa. -- Ariadne. Brought to you by Super Global Mega Corp .com