Xref: utzoo comp.sources.wanted:6990 comp.unix.wizards:15421 comp.sys.sgi:915 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!decwrl!wyse!mips!prls!philabs!philmtl!ncc!alberta!calgary!cpsc!paquette From: paquette@cpsc.ucalgary.ca (Trevor Paquette) Newsgroups: comp.sources.wanted,comp.unix.wizards,comp.sys.sgi Subject: Re: Float to sign/exp/mantissa Summary: ellaborate Message-ID: <1044@cs-spool.calgary.UUCP> Date: 5 Apr 89 17:24:55 GMT References: <1026@cs-spool.calgary.UUCP> Sender: news@calgary.UUCP Lines: 62 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