strtod

Odd behavior when converting C strings to/from doubles

吃可爱长大的小学妹 提交于 2020-01-14 08:17:14
问题 I'm having trouble understanding C's rules for what precision to assume when printing doubles, or when converting strings to doubles. The following program should illustrate my point: #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

return value of strtod() if string equals to zero

南笙酒味 提交于 2019-12-20 03:16:32
问题 As per MSDN: strtod returns 0 if no conversion can be performed or an underflow occurs. What if my string equals to zero (i.e., 0.0000)? How can I know if there is no error from the conversion? OK, I use the following code to verify the idea: char *Y = "XYZ"; double MyNum; char *MyEndPtr; int Err_Conversion = 0; errno = 0; //reset MyNum = strtod (Y, &MyEndPtr); if ( (MyNum == 0) && (errno != 0) && (strcmp(Y, MyEndPtr) == 0) ) { Err_Conversion = 1; } I see that MyNum = 0, but never see the

return value of strtod() if string equals to zero

泪湿孤枕 提交于 2019-12-20 03:16:15
问题 As per MSDN: strtod returns 0 if no conversion can be performed or an underflow occurs. What if my string equals to zero (i.e., 0.0000)? How can I know if there is no error from the conversion? OK, I use the following code to verify the idea: char *Y = "XYZ"; double MyNum; char *MyEndPtr; int Err_Conversion = 0; errno = 0; //reset MyNum = strtod (Y, &MyEndPtr); if ( (MyNum == 0) && (errno != 0) && (strcmp(Y, MyEndPtr) == 0) ) { Err_Conversion = 1; } I see that MyNum = 0, but never see the

What is the result of `strtod(“3ex”, &end)` supposed to be? What about `sscanf`?

别来无恙 提交于 2019-12-18 13:28:25
问题 In my experiments this expression double d = strtod("3ex", &end); initializes d with 3.0 and places end pointer at 'e' character in the input string. This is exactly as I would expect it to behave. The 'e' character might look as a beginning of the exponent part, but since the actual exponent value (required by 6.4.4.2) is missing, that 'e' should be treated as a completely independent character. However, when I do double d; char c; sscanf("3ex", "%lf%c", &d, &c); I notice that sscanf

Can't get a NaN from the MSVCRT strtod/sscanf/atof functions

 ̄綄美尐妖づ 提交于 2019-12-12 22:11:10
问题 Is there any way to get NaN s from the Windows CRT string to float functions? Why: I'm writing an IEEE float to string converter in C with no information loss ( strtod , sscanf or atof return the original float ) provided the rounding mode doesn't change. I'm under MinGW or Visual C++, so these calls go to the MSVC++ runtime. The problem is that I can't get it to parse any special values (like "Inf" or "NaN" ). Inf is OK (it's returned after parsing a value that doesn't fit in a float , such

GNU C: atof(), strtof() and strtod() fail (Debian for BeagleBoard)

大兔子大兔子 提交于 2019-12-11 08:00:00
问题 I have some C code which converts an ASCII string to a double with strtod(...). The program gets compiled for x86 (debugging), ARM and PowerPC (embedded target systems). The ARM board is actually a BeagleBoard xM running the Debian which is available for it. I've discovered that strtod() does not convert the values correctly on the ARM / Debian system. In fact all values I tried came out as 0.000. To demonstrate, I wrote the following very simple test program: #include <stdio.h> #include

strtod with limited string length

自闭症网瘾萝莉.ら 提交于 2019-12-10 09:35:30
问题 If I want to parse the first 3 characters from the char array as a double, ignoring the following characters, do I really need to do this? int main() { const char a[] = "1.23"; char *b = malloc(sizeof(char) * 4); memcpy(b, a, sizeof(char) * 3); b[3] = '\0'; printf("%f\n", strtod(b, NULL)); // Prints 1.20000, which is what I want free(b); } Isn't there a function like strtod that allows you to specify the maximum string length it should be searching for digits? Edit: I want it to print 1.2

Why isn't Darwin's strtod thread safe?

北慕城南 提交于 2019-12-07 06:04:03
问题 The following test always produces failures or bus errors for me on my Intel Mac Mini. Compiler: 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=

Why isn't Darwin's strtod thread safe?

£可爱£侵袭症+ 提交于 2019-12-05 10:59:41
The following test always produces failures or bus errors for me on my Intel Mac Mini. Compiler: 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

string.h中的常用函数

☆樱花仙子☆ 提交于 2019-12-04 18:48:51
字符串在我们程序中出现的频率很高,关于字符串的函数也是很多的,我们今天来介绍 string.h 头文件中的几个常用的函数: 1.strlen 函数名:strlen 功 * 能:求得字符串的长度 说 * 明:strlen(str)为字符串str的长度 实 * 例: #include <stdio.h> #include <string.h> int main() { char str[] = "I love Acm" ; printf ( "%d\n" , strlen (str)); //输出结果为 10 return 0 ; } 2.strcpy 函数名:strcpy 功 * 能:将一个字符串赋值给另一个字符串 说 * 明:strlen(str1,str2)表示将str2赋值给str1 实 * 例: #include <stdio.h> #include <string.h> int main() { char str1[] = "I love Acm" ; char str2[ 15 ]; strcpy (str2,str1); printf ( "%s\n" ,str2); //输出结果为 I love Acm return 0 ; } 3.strncpy 函数名:strncpy 功 * 能:将一个字符串的前一部分字符赋给另一字符串 说 * 明:strncpy(str1