Return-Path: <bungia!midgard!syntel!dal@src.honeywell.com>
Posted-Date: Sat Sep 23 21:26:44 1989 CST
Path: syntel!dal
Date: Sat Sep 23 21:26:44 1989 CST
To: bammi@dsrgsun.ces.CWRU.edu
From: dal@syntel.mn.org (Dale Schumacher)
Subject: How good is this?
Reply-To: syntel!dal (Dale Schumacher)
X-Member-Of: STdNET (ST Developer's Network)

>From dal  Fri Sep 22 13:36:08 1989 remote from midgard
Received: by midgard.Midgard.MN.ORG (smail2.5)
	id AA27643; 22 Sep 89 13:36:08 CDT (Fri)
Path: midgard!com50!tcnet!nic.MR.NET!uakari.primate.wisc.edu!uwm.edu!csd4.csd.uwm.edu!mrsvr.UUCP!kohli@gemed.med.ge.com
From: kohli@gemed (Jim Kohli, but my friends call me)
Newsgroups: comp.lang.c
Subject: Here's an IEEE to int routine
Message-ID: <1051@mrsvr.UUCP>
Date: 21 Sep 89 22:17:15 GMT
Sender: news@mrsvr.UUCP
Reply-To: kohli@gemed.med.ge.com (Jim Kohli, but my friends call me)
Organization: GE Medical (Applied Science Lab)
Lines: 46
To: dal


#include <stdio.h>
#include <math.h>

/****************************************************************
 * F.P. numbers are assumed normalized.  Note: magnitude        *
 * values in excess of sizeof(int) precision are mathematically *
 * limited to sizeof(int)                                       *
 *                                                              *
 *                          Jim Kohli                           *
 *                          GE Medical Systems                  *
 ****************************************************************/

/* No copyright is expressed or implied, nor is accuracy guaranteed */
                 /* but it works for me */

IEEE_TO_INT( r1, r2, n, round)
unsigned long *r1;      /* really IEEE floating point input */
int *r2;                /* integer output */
long int *n;            /* # of numbers to convert, * so F77 callable */
long int *round;        /* if non-zero, round x.5->x+1, else truncate only */
{
int i,sign;
unsigned long t;
float x,rounder,exponent,mantissa;

rounder = *round ? 0.5 : 0.0;

for (i = 0; i < (* n) ; i++) {
    t = r1[i];
    if (!t) r2[i] = 0;
    else {
            /* Get exponent and remove bias of 126 (normal to 1.0) */
        exponent = (float)((t >> 23) & 0xff) - 126.0;

            /* Note: we gain a bit of precision in the mantissa */
        mantissa = (float)((t & 0x007fffff) | 0x00800000);

            /* get the number */
        x = (mantissa/(float)0x00ffffff)*pow(2.0,exponent) + rounder;

            /* Combine results, don't forget the mantissa sign bit */
        r2[i] = (t & 0x80000000) ? -(int)x : (int)x;
        }
    }
}

\\   /  Dale Schumacher                         399 Beacon Ave.
 \\ /   (alias: Dalnefre')                      St. Paul, MN  55104-3527
  ><    ...umn-cs!midgard.mn.org!syntel!dal     United States of America
 / \\   "What is wanted is not the will to believe, but the will to find out,
/   \\  which is the exact opposite." -Bertrand Russell

