atof.c
来自「avr单片机的一些实际应用源码」· C语言 代码 · 共 74 行
C
74 行
/* Functions to perform various conversions to and from single-precision */
/* floating point 2/ 8/00 E.M.Greene */
/* Adapted by rfm ImageCraft (see math2.c.save) */
/* Modified (slightly) to accept expressions without decimal point, 7th Jan 2001 A.L.E. */
#include <ctype.h>
#include <math.h>
float powi(int x, int y);
float atof(char *s)
{
float v = 0.0,
scale = 0.1;
char mneg = ' ',
eneg = ' ';
int e = 0;
while (isspace(*s))
s++;
if (*s == '-')
mneg = *s++;
else
if (*s == '+')
s++;
while (isdigit(*s))
v = 10.0 * v + *s++ - '0';
if (*s == '.')
s++;
while(isdigit(*s))
{
v += (*s++ - '0') * scale;
scale /= 10.0;
}
if (toupper(*s) == 'E')
{
s++;
if (*s == '-')
eneg = *s++;
else
if (*s == '+')
s++;
while (isdigit(*s))
e = 10 * e + *s++ - '0';
if (eneg == '-')
v = v / powi(10,e);
else
v = v * powi(10,e);
}
if (mneg == '-')
v = -v;
return v;
}
float powi(int x, int y)
// Determines x-raised-to-the-power-of-y and returns the
// result as a float
{
int d;
float p = 1;
for (d = 0; d < y; d++)
p *= x;
return p;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?