delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/24/13:23:57

From: Phil Galbiati <galbiati AT cse DOT ogi DOT edu>
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: <Pine.SUN.3.95.970621175042.1188A-100000@blue.cse.ogi.edu>
References: <33A69C83 DOT 3374 AT ix DOT netcom DOT com>
Reply-To: Phil Galbiati <galbiati AT cse DOT ogi DOT edu>
NNTP-Posting-Host: blue.cse.ogi.edu
Mime-Version: 1.0
In-Reply-To: <33A69C83.3374@ix.netcom.com>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 <netinet/in.h> 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 <stdio.h>
#include <netinet/in.h>
#include <string.h>

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


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019