找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1749|回復: 0
打印 上一主題 下一主題
收起左側(cè)

gcvt函數(shù)源碼 如果ccs的stdlib庫函數(shù)中沒有,可以看過來

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:996033 發(fā)表于 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;
}



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表