找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 820|回复: 0
收起左侧

gcvt函数源码 如果ccs的stdlib库函数中没有,可以看过来

[复制链接]
ID:996033 发表于 2023-5-25 16:40 | 显示全部楼层 |阅读模式
#define PSH(X) (*(buf++)=(X))
#define PSH1(X) (*(buf--)=(X))
#define PEEK() buf[-1]
#define POP() *(--buf) = '\0'
#define PLUS 1
#define SPACE 2

char *gcvt(float f, uint16_t ndigit, char *buf)
{
     int i;
     uint32_t z,k;
     //int exp = 0;
     char *c = buf;
     float f2,t,scal;
     int   sign = 0;

     if((int)ndigit == -1)
       ndigit = 5;

     /* Unsigned long long only allows for 20 digits of precision
      * which is already more than double supports, so we limit the
      * digits to this.  long double might require an increase if it is ever
      * implemented.
      */
     if (ndigit > 20)
         ndigit = 20;

     if (f < 0.0) {
       sign = 1;
       f = -f;
        buf++;
     }

     scal = 1;
     for (i=ndigit; i>0; i--)
         scal *= 10;
     k = f + 0.1 / scal;
     f2 = f - k;
     if (!f) {
       PSH('0');
       if(ndigit > 0)
         PSH('.');
       for (i=0;i<ndigit;i++)
         PSH('0');
          PSH(0);
        return c;
     }

     i = 1;
     while (f >= 10.0) {
       f /= 10.0;
       i++;
     }

     buf += i + ndigit + 1;

     PSH1(0);

     if(ndigit > 0) {
         t = f2 * scal;
        z = t + 0.5;
       for (i = 0;i < ndigit;i++)
       {
         PSH1('0'+ (z % 10));
          z /= 10;
       }
       PSH1('.');
     }
     else
       PSH1(0);

     do {
       PSH1('0'+ (k % 10));
       k /= 10;
     }while (k);

     if (sign)
       PSH1('-');
     return c;
}



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表