标签: strtod

如何将字符串转换为浮点数?

#include<stdio.h>
#include<string.h>

int main() 
{
    char s[100] ="4.0800" ; 

    printf("float value : %4.8f\n" ,(float) atoll(s)); 
    return 0; 
}
Run Code Online (Sandbox Code Playgroud)

我希望输出应该是4.08000000我得到的4.00000000.

有没有办法得到点后的数字?

c floating-point type-conversion atof strtod

48
推荐指数
4
解决办法
17万
查看次数

strtod下溢,返回值!= 0

这是我的测试代码:

errno = 0;
d = strtod("1.8011670033376514e-308", NULL);
Run Code Online (Sandbox Code Playgroud)

有了这个代码,我得到d == 1.8011670033376514e-308errno == ERANGE.

来自strtod(3):

如果正确的值会导致溢出,则返回加号或减号HUGE_VAL(HUGE_VALF,HUGE_VALL)(根据值的符号),并ERANGE存储errno.如果正确的值会导致下溢,则返回零并ERANGE存储errno.

因此,在我看来,要么errno应该为零(无错误),要么d应为零(下溢).

这是一个错误,还是我错过了什么?对于许多不同版本的eglibc和gcc,会发生这种情况.

c strtod

12
推荐指数
1
解决办法
1099
查看次数

将char*转换为float或double

我有一个从文件读入的值,并存储为char*.值是货币编号,#.##,##.##或###.##.我想将char*转换为我可以在计算中使用的数字,我尝试了atof和strtod,他们只是给我垃圾数字.这样做的正确方法是什么,为什么我做错的方式呢?

这基本上就是我正在做的事情,只是从文件读入char*值.当我打印出temp和ftemp变量时,它们只是垃圾,巨大的负数.

另一个编辑:

我在gcc中正是这样运行的

#include <stdio.h>
int main()
{
 char *test = "12.11";
 double temp = strtod(test,NULL);
 float ftemp = atof(test);
 printf("price: %f, %f",temp,ftemp);
 return 0;
Run Code Online (Sandbox Code Playgroud)

}

我的输出是价格:3344336.000000,33444336.000000

编辑:这是我的代码

if(file != NULL)
    {
        char curLine [128];
        while(fgets(curLine, sizeof curLine, file) != NULL)
        {               
            tempVal = strtok(curLine,"|");          
            pairs[i].name= strdup(tempVal);
            tempVal = strtok(NULL,"|");
            pairs[i].value= strdup(tempVal);
            ++i;
        }
        fclose(file);
    }

    double temp = strtod(pairs[0].value,NULL);
    float ftemp = atof(pairs[0].value);
    printf("price: %d, %f",temp,ftemp);
Run Code Online (Sandbox Code Playgroud)

我的输入文件是非常简单的名称,值对如下:

NAME|VALUE
NAME|VALUE
NAME|VALUE
Run Code Online (Sandbox Code Playgroud)

价值是美元金额

解决:谢谢大家,我使用的是%d而不是%f,并且没有包含正确的标题.

c atof strtod

10
推荐指数
1
解决办法
11万
查看次数

`strtod("3ex",&end)应该是什么结果?怎么样`sscanf`?

在我的实验中这个表达

double d = strtod("3ex", &end);
Run Code Online (Sandbox Code Playgroud)

初始化d,3.0并将end指针放在'e'输入字符串中的字符处.这正如我所期望的那样.该'e'字符可能看起来是指数部分的开头,但由于缺少实际的指数值(6.4.4.2要求),因此'e'应将其视为完全独立的字符.

但是,当我这样做的时候

double d;
char c;
sscanf("3ex", "%lf%c", &d, &c);
Run Code Online (Sandbox Code Playgroud)

我注意到,sscanf消耗都'3''e'%lf格式说明.变量d接收3.0价值.变量以其c结尾'x'.这看起来很奇怪有两个原因.

首先,由于语言规范strtod在描述%f格式说明符的行为时引用,我直观地期望%lf以相同的方式处理输入strtod(即选择与终止点相同的位置).但是,我知道历史scanf上应该只返回一个字符回输入流.这限制了任何前瞻scanf可以通过一个角色执行的距离.上面的例子需要至少两个字符前瞻.所以,让我们说,我接受这样的事实%lf消耗都'3''e'从输入流.

但后来我们遇到了第二个问题.现在sscanf必须将其转换"3e"为类型double."3e"不是浮点常量的有效表示(同样,根据6.4.4.2,指数值不是可选的).我希望sscanf看待这个输入为错误:在终止%lf转换,返回0和离开d …

c scanf standard-library strtod

10
推荐指数
1
解决办法
256
查看次数

Python相当于C strtod

我正在努力将C++程序的一部分转换为Python,但是我在替换C函数strtod时遇到了一些麻烦.我正在处理的字符串由简单的数学方程组成,例如"KM/1000.0".问题是两个常量和数字是混合的,因此我无法使用float().

如何编写Python函数来模拟strtod哪个函数返回转换后的数字和下一个字符的位置?

python strtod

7
推荐指数
1
解决办法
751
查看次数

将C字符串转换为/从双精度转换时的奇怪行为

我无法理解C的规则,即在打印双精度或将字符串转换为双精度时要考虑的精度.以下程序应说明我的观点:

#include <errno.h>
#include <float.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv) {
    double x, y;
    const char *s = "1e-310";

    /* Should print zero */
    x = DBL_MIN/100.;
    printf("DBL_MIN = %e, x = %e\n", DBL_MIN, x);

    /* Trying to read in floating point number smaller than DBL_MIN gives an error */
    y = strtod(s, NULL);
    if(errno != 0)
        printf("  Error converting '%s': %s\n", s, strerror(errno));
    printf("y = %e\n", y);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我编译并运行这个程序时得到的输出(在带有gcc 4.5.2的Core 2 …

c printf ieee-754 strtod

7
推荐指数
2
解决办法
384
查看次数

strtof = strtod后跟强制转换?

假设您有一个字符串,例如“ 0.1”,只能近似表示为二进制浮点数,并且您想将其转换为单精度浮点数。这可以做到

strtof(s, 0);
Run Code Online (Sandbox Code Playgroud)

要么

(float)strtod(s, 0);
Run Code Online (Sandbox Code Playgroud)

直觉上,这些应该给出相同的结果,但是直觉在所有情况下都是正确的吗?还是在某些边缘情况下,第二种形式经过两次舍入后得出的结果与第一种形式略有不同?

c floating-point strtod

7
推荐指数
1
解决办法
80
查看次数

字符串转换为数字的问题(strtod)

我使用strtod()函数将环境变量提取为字符串,然后使用strtod将其更改为double:

enter code here
 char strEnv[32];
 strncpy(strEnv, getenv("LT_LEAK_START"), 31);
 // How to make sure before parsing that env LT_LEAK_START is indeed a number?
 double d = strtod(strEnv, NULL);
Run Code Online (Sandbox Code Playgroud)

现在我想确保用户输入的这个数字是一个数字而不是字符串或特殊字符.我怎样才能确定?

代码片段会有很大帮助.

提前致谢.

c linux string double strtod

6
推荐指数
1
解决办法
1万
查看次数

Valgrind:c_str和strtod的"无效读取"

我使用strtod()来转换一些输入字符串.在用valgrind检查我的代码时,我遇到了"无效读取大小为8".如果b以"i"或"n"开头,则显示消息,这就是我到目前为止所发现的.另外,如果我直接创建一个const char*(不调用c_str()),strtod似乎没问题.继承人代码:

#include <cstdlib>
#include <string>

int main(int argc, char** argv) {
      char*       a = 0; 
      std::string b = "i";
      const char* c = b.c_str();
      double      d = strtod(c, &a); 
}
Run Code Online (Sandbox Code Playgroud)

和(详细)valgrind输出:

==12638== Memcheck, a memory error detector
==12638== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==12638== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==12638== Command: ./leak
==12638== 
--12638-- Valgrind options:
--12638--    --suppressions=/usr/lib/valgrind/debian-libc6-dbg.supp
--12638--    --show-reachable=yes
--12638--    --leak-check=full
--12638--    -v
--12638-- Contents of /proc/version: …
Run Code Online (Sandbox Code Playgroud)

c++ string valgrind g++ strtod

6
推荐指数
1
解决办法
1816
查看次数

为什么Darwin的strtod线程不安全?

以下测试总是在我的Intel Mac Mini上为我生成故障或总线错误.

编译:

uname -a:
Darwin vogon13 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386

g++ -v:
Target: i686-apple-darwin9
Configured with: /var/tmp/gcc/gcc-5493~1/src/configure
--disable-checking -enable-werror --prefix=/usr --mandir=/share/man
--enable-languages=c,objc,c++,obj-c++
--program-transform-name=/^[cg][^.-]*$/s/$/-4.0/
--with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/lib
--build=i686-apple-darwin9 --with-arch=apple --with-tune=generic
--host=i686-apple-darwin9 --target=i686-apple-darwin9
Thread model: posix
gcc version 4.0.1 (Apple Inc. build 5493)
Run Code Online (Sandbox Code Playgroud)

编译命令:

"g++"  -ftemplate-depth-128 -O3 -finline-functions -Wno-inline -Wall
-pedantic -g -no-cpp-precomp -gdwarf-2 -Wno-long-double -Wno-long-long -fPIC
-DBOOST_DATE_TIME_NO_LIB=1 -DBOOST_THREAD_NO_LIB=1 -DBOOST_THREAD_USE_LIB=1
-DDATE_TIME_INLINE -DNDEBUG  -I"." -c -o "strtod_test.o" "strtod_test.cpp"

"g++"  -o "strtod_test" "strtod_test.o"
"libboost_thread-xgcc40-mt-s.a" …
Run Code Online (Sandbox Code Playgroud)

c++ macos darwin thread-safety strtod

5
推荐指数
1
解决办法
674
查看次数