From: Phil Galbiati Newsgroups: comp.os.msdos.djgpp Subject: Re: FLOATING NUMBERS:Motoral-intel Date: Sat, 21 Jun 1997 20:35:59 -0700 Organization: Oregon Graduate Institute - Computer Science & Engineering Lines: 76 Message-ID: References: <33A69C83 DOT 3374 AT ix DOT netcom DOT com> Reply-To: Phil Galbiati NNTP-Posting-Host: blue.cse.ogi.edu Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII In-Reply-To: <33A69C83.3374@ix.netcom.com> To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk On Tue, 17 Jun 1997, Kris wrote: > > They use 4 byte, I believe, > motoral formated floating point numbers, well fincaily I'm limeted to > working with the Intel again... Im using the commands proparly, but Im > just not familary with the number format. When I look at in Binary, the > first word seems to me to be the exponent, and the rest of the 3 words > are a number for the decemil point. I think you are confusing byte-order with Floating Point format. I'm not familiar with the software package to which you refered, but it's probably safe to assume that its floating point numbers conform to IEEE-754. Single-precision numbers are represented in 32 bits as follows: The MSb is the sign The next 8 bits are the exponent The remaining 23 bits are the mantissa There are also representations for positive & negative infinity, as well as NaN (not-a-number). But you don't need to worry about any of that. I'm assuming you are trying to transfer data from a 68000-based machine (e.g.. a Macintoy or a prehistoric Sun workstation) to a PC. If this is the case, the native floating point representations are both IEEE-754 compliant, so you don't have to worry about that. You *will*, however, have to concern yourself with byte-ordering. If you are moving from a *very* old motorola processor (e.g. 6800), then format *may* be an issue since these processors predate the standard. Motorola's 68000 family of processors are Big-endian -- they store the most-significant byte in a word first. Intel's x86 families are Little-endian -- they store the least-significant byte first. Fortunately, you are not the first person to encounter this problem, so libc already contains the functions you need to convert from big-endian to little-endian and vice-versa. You must include in your program, and call ntohl() to do the conversion. Read the ntohl() info page for the gory details. Here's a short piece of sample code to demonstrate: /* program to demonstrate use of ntohl() reads a big-endian single precision floating point number from the file named "data.dat" in the current directory, converts it from big-endian to little-endian using ntohl(), and prints the result to stdout. */ #include #include #include int main() { unsigned long bigEndian; unsigned long littleEndian; float fI; FILE *inFile; inFile = fopen ("data.dat", "rb"); fread (&bigEndian, sizeof (bigEndian), 1, inFile); littleEndian = ntohl(bigEndian); memcpy (&fI, &littleEndian, sizeof(fI)); printf ("%f\n", fI); return (0); } Hope this helps --Phil Galbiati