今天看到这样一些代码,情不自禁的把它写入博客,想要好好研究一下这段代码,很优秀的代码,我要研究他们是怎么写出来的。
同时对自己寄予厚望,希望能够有帮助。呵呵
 * strtod.c --
 *
 *      Source code for the "strtod" library procedure.
 *
 * Copyright (c) 1988-1993 The Regents of the University of California.
 * Copyright (c) 1994 Sun Microsystems, Inc.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id$
 */

#include "tclInt.h"

#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#ifndef NULL
#define NULL 0
#endif

static const int maxExponent = 511;     /* Largest possible base 10 exponent.  Any
                                 * exponent larger than this will already
                                 * produce underflow or overflow, so there's
                                 * no need to worry about additional digits.
                                 */
static const double powersOf10[] = {    /* Table giving binary powers of 10.  Entry */
    10.,                        /* is 10^2^i.  Used to convert decimal */
    100.,                       /* exponents into floating-point numbers. */
    1.0e4,
    1.0e8,
    1.0e16,
    1.0e32,
    1.0e64,
    1.0e128,
    1.0e256
};

/*
 *----------------------------------------------------------------------
 *
 * strtod --
 *
 *      This procedure converts a floating-point number from an ASCII
 *      decimal representation to internal double-precision format.
 *
 * Results:
 *      The return value is the double-precision floating-point
 *      representation of the characters in string.  If endPtr isn't
 *      NULL, then *endPtr is filled in with the address of the
 *      next character after the last one that was part of the
 *      floating-point number.
 *
 * Side effects:
 *      None.
 *
 *----------------------------------------------------------------------
 */

double
strtod(
    const char *string,         /* A decimal ASCII floating-point number,
                                 * optionally preceded by white space. Must
                                 * have form "-I.FE-X", where I is the integer
                                 * part of the mantissa, F is the fractional
                                 * part of the mantissa, and X is the
                                 * exponent. Either of the signs may be "+",
                                 * "-", or omitted. Either I or F may be
                                 * omitted, or both. The decimal point isn't
                                 * necessary unless F is present. The "E" may
                                 * actually be an "e". E and X may both be
                                 * omitted (but not just one). */
    char **endPtr)              /* If non-NULL, store terminating character's
                                 * address here. */
{
    int sign, expSign = FALSE;
    double fraction, dblExp;
    const double *d;
    register const char *p;
    register int c;
    int exp = 0;                /* Exponent read from "EX" field. */
    int fracExp = 0;            /* Exponent that derives from the fractional
                                 * part. Under normal circumstatnces, it is
                                 * the negative of the number of digits in F.
                                 * However, if I is very long, the last digits
                                 * of I get dropped (otherwise a long I with a
                                 * large negative exponent could cause an
                                 * unnecessary overflow on I alone). In this
                                 * case, fracExp is incremented one for each
                                 * dropped digit. */
    int mantSize;               /* Number of digits in mantissa. */
    int decPt;                  /* Number of mantissa digits BEFORE decimal
                                 * point. */
    const char *pExp;           /* Temporarily holds location of exponent in
                                 * string. */

    /*
     * Strip off leading blanks and check for a sign.
     */

    p = string;
    while (isspace(UCHAR(*p))) {
        p += 1;
    }
    if (*p == '-') {
        sign = TRUE;
        p += 1;
    } else {
        if (*p == '+') {
            p += 1;
        }
        sign = FALSE;
    }

    /*
     * Count the number of digits in the mantissa (including the decimal
     * point), and also locate the decimal point.
     */

    decPt = -1;
    for (mantSize = 0; ; mantSize += 1)
    {
        c = *p;
        if (!isdigit(c)) {
            if ((c != '.') || (decPt >= 0)) {
                break;
            }
            decPt = mantSize;
        }
        p += 1;
    }

    /*
     * Now suck up the digits in the mantissa. Use two integers to collect 9
     * digits each (this is faster than using floating-point). If the mantissa
     * has more than 18 digits, ignore the extras, since they can't affect the
     * value anyway.
     */

    pExp  = p;
    p -= mantSize;
    if (decPt < 0) {
        decPt = mantSize;
    } else {
        mantSize -= 1;          /* One of the digits was the point. */
    }
    if (mantSize > 18) {
        fracExp = decPt - 18;
        mantSize = 18;
    } else {
        fracExp = decPt - mantSize;
    }
    if (mantSize == 0) {
        fraction = 0.0;
        p = string;
        goto done;
    } else {
        int frac1, frac2;

        frac1 = 0;
        for ( ; mantSize > 9; mantSize -= 1) {
            c = *p;
            p += 1;
            if (c == '.') {
                c = *p;
                p += 1;
            }
            frac1 = 10*frac1 + (c - '0');
        }
        frac2 = 0;
        for (; mantSize > 0; mantSize -= 1) {
            c = *p;
            p += 1;
            if (c == '.') {
                c = *p;
                p += 1;
            }
            frac2 = 10*frac2 + (c - '0');
        }
        fraction = (1.0e9 * frac1) + frac2;
    }

    /*
     * Skim off the exponent.
     */

    p = pExp;
    if ((*p == 'E') || (*p == 'e')) {
        p += 1;
        if (*p == '-') {
            expSign = TRUE;
            p += 1;
        } else {
            if (*p == '+') {
                p += 1;
            }
            expSign = FALSE;
        }
        if (!isdigit(UCHAR(*p))) {
            p = pExp;
            goto done;
        }
        while (isdigit(UCHAR(*p))) {
            exp = exp * 10 + (*p - '0');
            p += 1;
        }
    }
    if (expSign) {
        exp = fracExp - exp;
    } else {
        exp = fracExp + exp;
    }

    /*
     * Generate a floating-point number that represents the exponent. Do this
     * by processing the exponent one bit at a time to combine many powers of
     * 2 of 10. Then combine the exponent with the fraction.
     */

    if (exp < 0) {
        expSign = TRUE;
        exp = -exp;
    } else {
        expSign = FALSE;
    }
    if (exp > maxExponent) {
        exp = maxExponent;
        errno = ERANGE;
    }
    dblExp = 1.0;
    for (d = powersOf10; exp != 0; exp >>= 1, ++d) {
        if (exp & 01) {
            dblExp *= *d;
        }
    }
    if (expSign) {
        fraction /= dblExp;
    } else {
        fraction *= dblExp;
    }

  done:
    if (endPtr != NULL) {
        *endPtr = (char *) p;
    }

    if (sign) {
        return -fraction;
    }
    return fraction;
}

将ACII码转为浮点数相关推荐

  1. CTF每日一题之ACII码编码

    好久没写了...因为好久没做题了,最近又开始偷懒了,愁人.今天发现了一个新的CTF平台,很有意思,不过缺点是没有中文,不过这没有关系,谁叫我谷歌翻译用的贼6,今天是个签到题,就不多说了. 题目链接:h ...

  2. 一些特殊ACII码的用法 在控制台中覆盖显示、刷新显示和删除字符

    很好奇怎么实现在控制台中不换行直接显示新的信息把旧的替换掉,于是找到了两个ACII码字符,他们可以帮助实现. 一个是'\b'字符,这个字符是backspace,即删除上一个字符,于是可以清除以显示的旧 ...

  3. C#之判断字母大小、字母转ACII码

    using System; using System.Text; namespace ValueType {class Program{static void Main(){Console.Write ...

  4. 剪辑视频的教程视频,分享视频转码转为序列图片

    所谓的视频转换成图片的意思就是把视频中的经典画面以图片的形式进行保存.如果是用普通的截图工具对视频画面进行截取,会相对来说比较费时,小编分享一个方法有需要的朋友接着往下看吧! 第一步,运行软件&quo ...

  5. 计组(COA) Programming1: 整数和二进制补码、整数和NBCD码、浮点数(float)和二进制码(IEEE 754)的相互转换。

    1.整数转化为二进制补码. public String intToBinary(String numStr) {//判断符号boolean isNeg = numStr.charAt(0) == '- ...

  6. 遗传算法综述(三、 流程结构与编码解码(二进制编码,格雷码编码浮点数编码、符号编码))

    遗传算法流程结构: 遗传算法是从代表问题可能潜在的解集的一个种群(population)开始的,而一个种群则由经过基因(gene)编码的一定数目的个体(individual)组成.每个个体实际上是染色 ...

  7. 微信小程序也许会用到上传视频,针对视频转码转为m3U8格式即web端可以使用的解决办法

    媒体处理这部分的视频转码涉及到 阿里云服务的对象存储OSS服务,消息服务MNS,及媒体处理MTS服务 可以利用阿里云服务的MTS服务即媒体处理  https://mts.console.aliyun. ...

  8. js 将微信二维码转为url,qrcodeJs解析二维码,qrcode.decode is not a function报错

    前言 工作中遇到的需求:用户上传相册中选中的图片,判断这个图片里的二维码是不是微信二维码,如果是则上传到服务器:不是,则提示用户重新上传. 百度了下,qrcode.js是一个用于生成二维码的 Java ...

  9. 2字节十六进制浮点数 qt_Qt中如何实现十六进制“41A4 0000”十六进制转为浮点数20.5呢?...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 对于浮点类型的数据采用单精度类型(float)和双精度类型(double)来存储,float数据占用 32bit,double数据占用 64bit,我们在 ...

最新文章

  1. node编写定时任务,for循环只执行一遍的解决办法
  2. 10000字的Pandas核心操作知识大全!
  3. SAP QM初阶之事务代码CR05查询QM Work Center List
  4. android网页打开摄像头,在android上,用WEB页面打开手机摄像头
  5. 修改注册表给windows防火墙添加例外 ------------ 转
  6. Java锤子剪刀布大家应该都会玩“锤子剪刀布”的游戏: 现给出两人的交锋记录,请统计双方的胜、平、负次数,并且给出双方分别出什么手势的胜算最大。
  7. CCIE-LAB-第五篇-SDN-SD-WAN-BGP-OMP(sdwan版的路由协议)
  8. 封装属性_「Python基础学习」之封装大法
  9. 在 Redis 上实现的分布式锁
  10. Hadoop的I/O操作
  11. IE10 访问 ASP.NET 站点的问题
  12. css3 HTML5 效果
  13. 跨站脚本攻击(反射型xss)笔记(一)
  14. 宝峰c1对讲机写频软件_宝峰对讲机写频软件下载7.01 官方正式版-宝峰BF480,BF520,F25,F26对讲机写频软件西西软件下载...
  15. 程序人生 - 汽车后视镜锁车自动折叠为啥失灵?
  16. push_back()函数的用法
  17. bootloader 详细介绍
  18. 我的大学 --- 郭天祥【4】
  19. 黑苹果驱动hd4000
  20. matlab 相关性分析 相关系数地图生成

热门文章

  1. 云原生(什么是云原生?云原生的四要素)
  2. 微信文件删除了怎么恢复,2个实测有效的办法推荐
  3. SSD固态硬盘能否恢复--争议
  4. js 判断变量是否为空
  5. C++三目运算符(简述)
  6. diagrams 一个完全可以代替visio的软件
  7. 查看linux系统CPU和内存命令
  8. Docker系列 搭建个人云盘服务nextcloud
  9. python设置excel单元格宽度
  10. SMTP客户端python实现