[comp.sys.sgi] Float to sign/exp/mantissa

paquette@cpsc.ucalgary.ca (Trevor Paquette) (04/04/89)

   I am sure that someone out there has a routine that given
a floating point number, what is the sign, exponent and mantissa
of the number.. I'd rather not re-invent the wheel if this has
already been done. If some kind soul out there has such a
function, and is willing to send it my way, I'd be very grateful.
    
    aTdHvAaNnKcSe
        Trev

==============================================================================
               Trevor Paquette/GraphicsLand, Calgary, Alberta                 
 ..uunet!{ubc-cs,utai,alberta}!calgary!paquette          ICBM:51 03 N/114 05 W
 calgary!paquette@cs.ubc.ca      Luminous beings we are, not this crude matter

ark@alice.UUCP (Andrew Koenig) (04/05/89)

In article <1026@cs-spool.calgary.UUCP>, paquette@cpsc.ucalgary.ca (Trevor Paquette) writes:

>    I am sure that someone out there has a routine that given
> a floating point number, what is the sign, exponent and mantissa
> of the number..

The widely available frexp() library function gives you most
of what you want.  After executing the following:

	double x;
	double frexp();
	double mant;
	int exp;

	x = /* some value */;

	mant = frexp(x, &exp);

you are guaranteed that x is equal to mant * 2^^exp (here, ^^ means
exponentiation), that 0.5 <= |mant| < 1, and that the sign of mant
is the same as the sign of x, except that if x is 0, mant and exp
will be 0 too.

That should make it easy for you to get what you want.
-- 
				--Andrew Koenig
				  ark@europa.att.com

rmyers@net1.ucsd.edu (Robert Myers) (04/05/89)

In article <1026@cs-spool.calgary.UUCP> paquette@cpsc.ucalgary.ca (Trevor Paquette) writes:
>   I am sure that someone out there has a routine that given
>a floating point number, what is the sign, exponent and mantissa
>of the number.. I'd rather not re-invent the wheel if this has
>    
>    aTdHvAaNnKcSe
>        Trev

The routines frexp(3) and modf(3) are already part of libm and 
may be what you are looking for.

Raul Rathmann, raul%sdnp1@ucsd.edu

john@polyof.UUCP ( John Buck ) (04/05/89)

In article <1026@cs-spool.calgary.UUCP>, paquette@cpsc.ucalgary.ca (Trevor Paquette) writes:
>    I am sure that someone out there has a routine that given
> a floating point number, what is the sign, exponent and mantissa
> of the number.. I'd rather not re-invent the wheel if this has
> already been done...

Most Unix manuals have the frexp(3C) and ldexp(3c) functions which do
what you want.  [man 3 frexp].  If your system does not have them under
frexp/ldexp, they MAY be called something else.  Check your permuted
index.

John Buck
john@polyof.poly.edu
trixie!polyof!john

paquette@cpsc.ucalgary.ca (Trevor Paquette) (04/06/89)

In article <1026@cs-spool.calgary.UUCP>, paquette@cpsc.ucalgary.ca (Trevor Paquette) writes:
> 
> 
>    I am sure that someone out there has a routine that given
> a floating point number, what is the sign, exponent and mantissa
> of the number.. I'd rather not re-invent the wheel if this has
> already been done. If some kind soul out there has such a
> function, and is willing to send it my way, I'd be very grateful.
>     
>     aTdHvAaNnKcSe
>         Trev
> 

   Maybe I should elaborate on this a bit..
    What I am doing is writing a Machine Independant Format (MIF) similar to
 XDR that Sun has. This arose out of the fact that some of the machines
 that we need to use do not have XDR support.. So I am writing my own.
 Since floats and doubles are represented the same way on different machines
 I have to come up with a way of representing a number in a file that is
 machine independant.
    I DO NOT want to do something like the following to represent an integer..

/*      FILE *fp;
       int num;    */
      fprintf(fp,"%d ",num);

  This is clearly a waste of disk space.. when we can do the following..

/*      char ch[4]; */
/*  to write the number */
 ch[0] = (char)((*num & 0xff000000) >> 24);
 ch[1] = (char)((*num & 0x00ff0000) >> 16);
 ch[2] = (char)((*num & 0x0000ff00) >> 8);
 ch[3] = (char)((*num & 0x000000ff));
 fwrite(ch, 1, 4, fp);

/* to read the number */
   fread(ch, 1, 4, fp);
   num = (int)(ch[3]) + (int)(ch[2] << 8) + (int)(ch[1] << 16) + (int)(ch[0] << 24);

 Much better.. saves lotsa disk space.. files tend to be MUCH smaller this way.

  Now the problem arises when trying to save a float or a double.. how?
 I want to keep as much precision in the number as possible.. 

    the following is a start but I still end up with a float to deal with..
   
   double d = some number;
   double mant;
   int exp;

   mant = frexp(d, &exp);

   I still have mant to deal with which is a float.. back to square one..

   Any help would be appreciated..

    Trev
==============================================================================
               Trevor Paquette/GraphicsLand, Calgary, Alberta                 
 ..uunet!{ubc-cs,utai,alberta}!calgary!paquette          ICBM:51 03 N/114 05 W
 calgary!paquette@cs.ubc.ca      Luminous beings we are, not this crude matter

paquette@cpsc.ucalgary.ca (Trevor Paquette) (04/06/89)

In article <1044@cs-spool.calgary.UUCP>, paquette@cpsc.ucalgary.ca (Trevor Paquette) writes:
> In article <1026@cs-spool.calgary.UUCP>, paquette@cpsc.ucalgary.ca (Trevor Paquette) writes:
] > 
] > 
] >    I am sure that someone out there has a routine that given
] > a floating point number, what is the sign, exponent and mantissa
] > of the number.. I'd rather not re-invent the wheel if this has
] > already been done. If some kind soul out there has such a
] > function, and is willing to send it my way, I'd be very grateful.
] >     
] >     aTdHvAaNnKcSe
] >         Trev
] > 
] 
]    Maybe I should elaborate on this a bit..
]     What I am doing is writing a Machine Independant Format (MIF) similar to
]  XDR that Sun has. This arose out of the fact that some of the machines
]  that we need to use do not have XDR support.. So I am writing my own.
]  Since floats and doubles are represented the same way on different machines

***
* The above line should read..                          
*>  Since floats and doubles are NOT represented the same way on different machines
* Sorry if the caused any confusion
***

]  I have to come up with a way of representing a number in a file that is
]  machine independant.
]     I DO NOT want to do something like the following to represent an integer..
] 
] /*      FILE *fp;
]        int num;    */
]       fprintf(fp,"%d ",num);
] 
]   This is clearly a waste of disk space.. when we can do the following..
] 
] /*      char ch[4]; */
] /*  to write the number */
]  ch[0] = (char)((*num & 0xff000000) >> 24);
]  ch[1] = (char)((*num & 0x00ff0000) >> 16);
]  ch[2] = (char)((*num & 0x0000ff00) >> 8);
]  ch[3] = (char)((*num & 0x000000ff));
]  fwrite(ch, 1, 4, fp);
] 
] /* to read the number */
]    fread(ch, 1, 4, fp);
]    num = (int)(ch[3]) + (int)(ch[2] << 8) + (int)(ch[1] << 16) + (int)(ch[0] << 24);
] 
]  Much better.. saves lotsa disk space.. files tend to be MUCH smaller this way.
] 
]   Now the problem arises when trying to save a float or a double.. how?
]  I want to keep as much precision in the number as possible.. 
] 
]     the following is a start but I still end up with a float to deal with..
]    
]    double d = some number;
]    double mant;
]    int exp;
] 
]    mant = frexp(d, &exp);
] 
]    I still have mant to deal with which is a float.. back to square one..
] 
]    Any help would be appreciated..

==============================================================================
               Trevor Paquette/GraphicsLand, Calgary, Alberta                 
 ..uunet!{ubc-cs,utai,alberta}!calgary!paquette          ICBM:51 03 N/114 05 W
 calgary!paquette@cs.ubc.ca      Luminous beings we are, not this crude matter

guy@auspex.auspex.com (Guy Harris) (04/09/89)

>    What I am doing is writing a Machine Independant Format (MIF) similar to
> XDR that Sun has. This arose out of the fact that some of the machines
> that we need to use do not have XDR support.. So I am writing my own.

You may want to grab Sun's XDR source from, say, "uunet", instead; it's
in the "comp.sources.unix" archives.  It may not have support for your
floating-point format, but at least it's a start.

Basically, the XDR form is big-endian IEEE.  The code is
machine-dependent; the VAX version represents the VAX format as a
structure with bit fields, and extracts and inserts the fields that way.