Float to sign/exp/mantissa

Trevor Paquette paquette at cpsc.ucalgary.ca
Thu Apr 6 03:24:55 AEST 1989


In article <1026 at cs-spool.calgary.UUCP>, paquette at 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 at cs.ubc.ca      Luminous beings we are, not this crude matter



More information about the Comp.sys.sgi mailing list