home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
EFFO
/
pd3.lzh
/
SBPROLOG2.2
/
SIM
/
mathutil.c
< prev
next >
Wrap
Text File
|
1991-08-10
|
2KB
|
84 lines
#ifdef OS9
/* This file implements ldexp() and frexp() functions.
It is totally OS9 Microware C dependent and relies on
doubles being IEEE 64-bit and long ints being 32-bit. */
#define EXP_MASK 0x7ff00000
#define EXP_LOC 20
#define BIAS 1023
#define SIZE 64
#define MANTISSA_BITS 52
struct double_word { unsigned long lower, upper; };
union hack {
double float_num;
struct double_word int_num;
};
/* double ldexp(mantissa,exponent)
double mantissa;
int exponent;
Returns the value of mantissa * 2 ^ exponent */
double ldexp(mantissa,exponent)
double mantissa;
int exponent;
{
union hack magic;
register int exp;
magic.float_num = mantissa;
if (mantissa != 0.0)
{
/* retrieve exponent */
exp = (magic.int_num.lower & EXP_MASK) >> EXP_LOC;
magic.int_num.lower &= ~EXP_MASK;
/* add new exponent */
exp += exponent;
magic.int_num.lower |= (exp << EXP_LOC) & EXP_MASK;
}
return (magic.float_num);
}
/* double frexp(num,exponent)
double num;
int *exponent;
frexp() returns the mantissa of num (of magnitude < 1), which
gives num when multiplied with 2 ^ exponent. */
double frexp(num,exponent)
double num;
int *exponent;
{
union hack magic;
register unsigned long mant;
register int i, exp;
/* IEEE 64-bit double precision format :
sign : 1 bit
exponent : 11 bit
mantissa : 52 bit
=> num = 2 ^ (exponent-bias) * 1.mantissa (1 hidden bit),
where bias = 1024.
0.0 is represented by exponent = mantissa = 0 */
magic.float_num = num;
if (num != 0.0)
{
/* retrieve exponent */
exp = (magic.int_num.lower & EXP_MASK) >> EXP_LOC;
*exponent = exp-BIAS+1;
exp = BIAS-1;
magic.int_num.lower &= ~EXP_MASK;
magic.int_num.lower |= (exp << EXP_LOC) & EXP_MASK;
} else *exponent = 0;
return (magic.float_num);
}
#endif