home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / gnu / libsrc87 / rounder.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-30  |  2.9 KB  |  79 lines

  1. Return-Path: <bungia!midgard!syntel!dal@src.honeywell.com>
  2. Posted-Date: Sat Sep 23 21:26:44 1989 CST
  3. Path: syntel!dal
  4. Date: Sat Sep 23 21:26:44 1989 CST
  5. To: bammi@dsrgsun.ces.CWRU.edu
  6. From: dal@syntel.mn.org (Dale Schumacher)
  7. Subject: How good is this?
  8. Reply-To: syntel!dal (Dale Schumacher)
  9. X-Member-Of: STdNET (ST Developer's Network)
  10.  
  11. >From dal  Fri Sep 22 13:36:08 1989 remote from midgard
  12. Received: by midgard.Midgard.MN.ORG (smail2.5)
  13.     id AA27643; 22 Sep 89 13:36:08 CDT (Fri)
  14. Path: midgard!com50!tcnet!nic.MR.NET!uakari.primate.wisc.edu!uwm.edu!csd4.csd.uwm.edu!mrsvr.UUCP!kohli@gemed.med.ge.com
  15. From: kohli@gemed (Jim Kohli, but my friends call me)
  16. Newsgroups: comp.lang.c
  17. Subject: Here's an IEEE to int routine
  18. Message-ID: <1051@mrsvr.UUCP>
  19. Date: 21 Sep 89 22:17:15 GMT
  20. Sender: news@mrsvr.UUCP
  21. Reply-To: kohli@gemed.med.ge.com (Jim Kohli, but my friends call me)
  22. Organization: GE Medical (Applied Science Lab)
  23. Lines: 46
  24. To: dal
  25.  
  26.  
  27. #include <stdio.h>
  28. #include <math.h>
  29.  
  30. /****************************************************************
  31.  * F.P. numbers are assumed normalized.  Note: magnitude        *
  32.  * values in excess of sizeof(int) precision are mathematically *
  33.  * limited to sizeof(int)                                       *
  34.  *                                                              *
  35.  *                          Jim Kohli                           *
  36.  *                          GE Medical Systems                  *
  37.  ****************************************************************/
  38.  
  39. /* No copyright is expressed or implied, nor is accuracy guaranteed */
  40.                  /* but it works for me */
  41.  
  42. IEEE_TO_INT( r1, r2, n, round)
  43. unsigned long *r1;      /* really IEEE floating point input */
  44. int *r2;                /* integer output */
  45. long int *n;            /* # of numbers to convert, * so F77 callable */
  46. long int *round;        /* if non-zero, round x.5->x+1, else truncate only */
  47. {
  48. int i,sign;
  49. unsigned long t;
  50. float x,rounder,exponent,mantissa;
  51.  
  52. rounder = *round ? 0.5 : 0.0;
  53.  
  54. for (i = 0; i < (* n) ; i++) {
  55.     t = r1[i];
  56.     if (!t) r2[i] = 0;
  57.     else {
  58.             /* Get exponent and remove bias of 126 (normal to 1.0) */
  59.         exponent = (float)((t >> 23) & 0xff) - 126.0;
  60.  
  61.             /* Note: we gain a bit of precision in the mantissa */
  62.         mantissa = (float)((t & 0x007fffff) | 0x00800000);
  63.  
  64.             /* get the number */
  65.         x = (mantissa/(float)0x00ffffff)*pow(2.0,exponent) + rounder;
  66.  
  67.             /* Combine results, don't forget the mantissa sign bit */
  68.         r2[i] = (t & 0x80000000) ? -(int)x : (int)x;
  69.         }
  70.     }
  71. }
  72.  
  73. \\   /  Dale Schumacher                         399 Beacon Ave.
  74.  \\ /   (alias: Dalnefre')                      St. Paul, MN  55104-3527
  75.   ><    ...umn-cs!midgard.mn.org!syntel!dal     United States of America
  76.  / \\   "What is wanted is not the will to believe, but the will to find out,
  77. /   \\  which is the exact opposite." -Bertrand Russell
  78.  
  79.