怎么编写一涵数,将float 转化成string

shn228 2006-05-21 10:59:26
怎么编写一涵数,将float 转化成string
...全文
364 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
yjgx007 2006-05-22
  • 打赏
  • 举报
回复
IEEE float standard:

32位浮点结构:

32 sign bit, 31 - 23 biased exponent = true exponent + 0x7F, 22 - 0 fraction

明白这个,用汇编写真是piece of cake
trublemaker 2006-05-22
  • 打赏
  • 举报
回复
晕,标准太多了
laiwusheng 2006-05-21
  • 打赏
  • 举报
回复
/* 1.3 10-08-85 (ftoa.c)
************************************************************************
* Robert C. Tausworthe *
* Jet Propulsion Laboratory *
* Pasadena, CA 91009 1985 *
************************************************************************/

#include "defs.h"
#include "ctype.h"
#include "stdtyp.h"
#include "mathcons.h"

/************************************************************************/
STRING
ftoa(s, x, pr, mode) /* Convert x to ascii string s with precision pr
in mode 'e', 'f', or 'g' format.
Return pointer to s. */
/*----------------------------------------------------------------------*/
STRING s;
double x;
{
BOOL minus;
STRING p, astoi();
int d;
double normalize();

p = s; /* save string pointer for returns */
*s = NULL;
if (pr > DPRECISION)
pr = DPRECISION;
if (minus = ((x < 0.0) ? TRUE : FALSE)) /* work with pos. numbers */
x = -x;
x = normalize(x, &d);
*s++ = (minus ? '-' : ' '); /* put sign into string */
switch (tolower(mode))
{ case 'e':
case 0:
expform(s, x, d, pr);
break;
case 'f':
case 1:
floatform(s, x, d, pr);
break;
case 'g':
case 2:
minform(s, x, d, pr);
break;
default:
;
}
return p;
}

/*\p*********************************************************************/
LOCAL double
normalize(x, d) /* return x * 10^-d in [1, 10), with d set so
this condition is met. */
/*----------------------------------------------------------------------*/
double x;
int *d;
{
/****************************************************************
* This array contains 2^n powers of 10 up to n = BIG10X-1 *
****************************************************************/

LOCAL double tentothe2tothe[BIG10X] =
{ 10., 100., 10000., 100000000., 10000000000000000.,
100000000000000000000000000000000.
#ifdef AZTEC
, 1.0e64, 1.0e128
#endif

#ifdef SUN
, 1.0e64, 1.0e128, 1.0e256
#endif
};

/********************************************************
* Also, make this array be 2^n up to 2^(BIG10X-1) *
********************************************************/

LOCAL int twoto[BIG10X] =
{ 1, 2, 4, 8, 16, 32
#ifdef AZTEC
, 64, 128
#endif

#ifdef SUN
, 64, 128, 256
#endif
};

int i;

*d = 0;
if (x ISNT 0.0)
if (x < 1.0)
{ for (i = 1; x < SYMLEAST; i++)
x *= 10.0;
x = 10.0 / normalize(1.0 / x, d);
*d = -*d - i;
if (x >= 10.0) /* compensate if on the edge. */
{ x /= 10.0;
++*d;
}
}
else
for (i = BIG10X; x >= 10.0 AND --i >= 0; )
if (x >= tentothe2tothe[i])
{ x /= tentothe2tothe[i];
*d += twoto[i];
}
return x;
}

/*\p*********************************************************************/
LOCAL VOID
floatform(s, x, d, pr) /* put x * 10^d (precision pr) into string s
in f-format. */
/*----------------------------------------------------------------------*/
STRING s;
double x;
{
double roundoff();
int i;

x = roundoff(x, &d, pr + ((d > 0) ? d : 0));
if (d < 0)
*s++ = '0';
else
while (d >= 0)
{ *s++ = pickdigit(&x);
d--;
}
if ((i = pr) >= 0)
*s++ = '.';
while (++d < 0 AND i-- > 0)
*s++ = '0';
while (i-- > 0)
*s++ = pickdigit(&x);
*s = NULL;
}

/************************************************************************/
LOCAL VOID
expform(s, x, d, pr) /* put x * 10^d (precision pr) into string s
in e-format */
/*----------------------------------------------------------------------*/
STRING s;
double x;
{
double roundoff();
int i;

x = roundoff(x, &d, i = pr);
*s++ = pickdigit(&x);
*s++ = '.';
while (i-- > 0)
*s++ = pickdigit(&x);
*s++ = 'e';
itoa(s, d, 0); /* minimum width */
}

/*\p*********************************************************************/
LOCAL VOID
minform(s, x, d, pr) /* put x * 10^d into string s in minimum format
with precision pr */
/*----------------------------------------------------------------------*/
STRING s;
double x;
{
int i, j;

if (d < 5)
{ floatform(s, x, d, pr);
i = strlen(s) - 1;
while (s[i] IS '0')
s[i--] = NULL;
if (s[i] IS '.')
s[i--] = NULL;
}
else
{ expform(s, x, d, pr);
j = strlen(s) - 1;
while (s[j] ISNT 'e')
j--;
for (i = j - 1; s[i] IS '0'; i--)
;
strcpy(&s[++i], &s[j]);
}
}

/************************************************************************/
LOCAL
pickdigit(x) /* return ascii leading digit of x, strip it off, and
prepare x for extraction of next digit. */
/*----------------------------------------------------------------------*/
double *x;
{
int n;

n = *x; /* take integer part. */
*x = 10 * ( *x - n); /* strip it off, and renormalize x */
return (n + '0');
}
/*\p*********************************************************************/
LOCAL double
roundoff(x, d, p) /* return x * 10^*d rounded to precision p */

/*----------------------------------------------------------------------*/
double x;
int *d;
{ /*************************************/
LOCAL double rounder[21] = /* NOTE: DPRECISION must be no larger*/
/* than the number of elements in */
/* this array. */
/*************************************/
{ 0.5, 0.5e-1, 0.5e-2, 0.5e-3, 0.5e-4, 0.5e-5,
0.5e-6, 0.5e-7, 0.5e-8, 0.5e-9, 0.5e-10, 0.5e-11,
0.5e-12, 0.5e-13, 0.5e-14, 0.5e-15, 0.5e-16, 0.5e-17,
0.5e-18, 0.5e-19, 0.5e-20
};
double y;

if ((y = x) ISNT 0.0)
if (p <= DPRECISION)
if ((x += rounder[p]) >= 10.0)
{ x = y / 10.0 + rounder[p];
++*d;
}
return x;
}
lj860603 2006-05-21
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/4765/4765674.xml?temp=.6886713
shn228 2006-05-21
  • 打赏
  • 举报
回复
给个地址吧
jackwater 2006-05-21
  • 打赏
  • 举报
回复
因为你在设置float型时就因该设置小数 有多少位,然后乘上一个1000000,然后再放入数组
jackwater 2006-05-21
  • 打赏
  • 举报
回复
是不是应该先把float转化成为一个整型的,然后转入数组就行了。
ctu_85 2006-05-21
  • 打赏
  • 举报
回复
一个一个的移位
把高位移出来之后+的值加上48保存到char数组中就好了吧
lj860603 2006-05-21
  • 打赏
  • 举报
回复
怎么又是这问题?!
yuanchuang 2006-05-21
  • 打赏
  • 举报
回复
sprintf()
问问题怎么没分啊?
xl5338870 2006-05-21
  • 打赏
  • 举报
回复
编写这个函数应该对IEEE754浮点标准很熟悉
laiwusheng 2006-05-21
  • 打赏
  • 举报
回复
接分!
shn228 2006-05-21
  • 打赏
  • 举报
回复
谢谢各位了,虽然还不能完全理解

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧