OSSP CVS Repository

ossp - ossp-pkg/xds/float2xds-float.c
Not logged in
[Honeypot]  [Browse]  [Directory]  [Home]  [Login
[Reports]  [Search]  [Ticket]  [Timeline
  [Raw

ossp-pkg/xds/float2xds-float.c
#include "xds_p.h"

int float2xds_float(xds_float_t* new_num, float num)
    {
    const static unsigned int base = 2;
    size_t i;
    float  tmp;

    /* Handle zero as a special case. */

    if (num == 0.0)
	{
	new_num->sign     = 0;
	new_num->fraction = 0;
	new_num->exponent = -127;
	return 0;
	}

    /* Determine the sign of the number. */

    if (num < 0)
	{
	new_num->sign = 1;
	num = 0 - num;
	}
    else
	new_num->sign = 0;

    /* Canonify the number before we convert it. */

    new_num->exponent = 0;
    while (num < 1)
	{
	num *= base;
	--new_num->exponent;
	}

    /* Find the exponent. */

    for (i = 0, tmp = 1; i <= 128; ++i, tmp *= base)
	{
	if (tmp*base > num)
	    break;
	}
    if (i <= 128)
	{
	num = num / tmp - 1;
	new_num->exponent += i;
	}
    else
	return 1;

    /* Calculate the fraction part. */

    for (new_num->fraction = 0, i = 0; i < 23; ++i)
	{
	new_num->fraction *= base;
	if (num >= 1.0 / base)
	    {
	    new_num->fraction |= 1;
	    num = num * base - 1;
	    }
	else
	    {
	    new_num->fraction |= 0;
	    num *= base;
	    }
	}

    return 0;
    }

CVSTrac 2.0.1