double-precision dissection

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Mon Dec 3 18:32:59 AEST 1990


In article <12131 at life.ai.mit.edu>, bson at 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 <math.h>-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 <float.h>
	/* defines DBL_DIG */
	#include <stdio.h>
	/* defines sprintf() */
	#include <stdlib.h>
	/* 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.



More information about the Comp.lang.c mailing list