double类型小数位数的问题

howema 2012-12-27 11:58:25
string s = "156.358123";
double d = atof(s.c_str());
转换后为什么后面几位小数被去掉了
怎样能全部转过来?
即确保把s整个转换成double型的
如何实现?

...全文
394 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-12-28
  • 打赏
  • 举报
回复
float.h
...
#define DBL_DIG         15                      /* # of decimal digits of precision */
#define DBL_EPSILON     2.2204460492503131e-016 /* smallest such that 1.0+DBL_EPSILON != 1.0 */
#define DBL_MANT_DIG    53                      /* # of bits in mantissa */
#define DBL_MAX         1.7976931348623158e+308 /* max value */
#define DBL_MAX_10_EXP  308                     /* max decimal exponent */
#define DBL_MAX_EXP     1024                    /* max binary exponent */
#define DBL_MIN         2.2250738585072014e-308 /* min positive value */
#define DBL_MIN_10_EXP  (-307)                  /* min decimal exponent */
#define DBL_MIN_EXP     (-1021)                 /* min binary exponent */
#define _DBL_RADIX      2                       /* exponent radix */
#define _DBL_ROUNDS     1                       /* addition rounding: near */

#define FLT_DIG         6                       /* # of decimal digits of precision */
#define FLT_EPSILON     1.192092896e-07F        /* smallest such that 1.0+FLT_EPSILON != 1.0 */
#define FLT_GUARD       0
#define FLT_MANT_DIG    24                      /* # of bits in mantissa */
#define FLT_MAX         3.402823466e+38F        /* max value */
#define FLT_MAX_10_EXP  38                      /* max decimal exponent */
#define FLT_MAX_EXP     128                     /* max binary exponent */
#define FLT_MIN         1.175494351e-38F        /* min positive value */
#define FLT_MIN_10_EXP  (-37)                   /* min decimal exponent */
#define FLT_MIN_EXP     (-125)                  /* min binary exponent */
#define FLT_NORMALIZE   0
#define FLT_RADIX       2                       /* exponent radix */
#define FLT_ROUNDS      1                       /* addition rounding: near */
...
ForestDB 2012-12-27
  • 打赏
  • 举报
回复
浮点数天生有精度问题 对精度有要求改用别的库
rocktyt 2012-12-27
  • 打赏
  • 举报
回复
float精度太低了,十进制只能保证6位数字正确,如果用默认输出只会输出6位还是7位
rocktyt 2012-12-27
  • 打赏
  • 举报
回复
引用 1 楼 telihe 的回复:
atof应该是转换成float吧,试试strtod看看 不过这些我没用过,仅供参考
atof返回值类型是double
引用 楼主 howema 的回复:
string s = "156.358123"; double d = atof(s.c_str()); 转换后为什么后面几位小数被去掉了 怎样能全部转过来? 即确保把s整个转换成double型的 如何实现?
你是怎么查看所谓的“后面几位小数被去掉”的?
telihe 2012-12-27
  • 打赏
  • 举报
回复
atof应该是转换成float吧,试试strtod看看 不过这些我没用过,仅供参考
prajna 2012-12-27
  • 打赏
  • 举报
回复
引用 7 楼 mymtom 的回复:
double有效数字在16位之间。楼主的例子用atof完全没有问题的,而且atof的返回值是double, 而不是字面float: #include <stdlib.h> double atof(const char *nptr); 只是printf("%f\n", ...)只打印6位小数, C/C++ code?1234567891011121314#……
++ Declaration: double atof(const char *str); The string pointed to by the argument str is converted to a floating-point number (type double).
赵4老师 2012-12-27
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string>
using namespace std;
int main() {
	string s = "156.358123";
	double d = atof(s.c_str());
	printf("%.15lg\n",d);
	return 0;
}
wangeen 2012-12-27
  • 打赏
  • 举报
回复
你确定是没有完全转换,还是因为print出来的时候被截断了,因为cout在print float的时默认会截断
telihe 2012-12-27
  • 打赏
  • 举报
回复
引用 2 楼 rocktyt2 的回复:
引用 1 楼 telihe 的回复:atof应该是转换成float吧,试试strtod看看 不过这些我没用过,仅供参考atof返回值类型是double 引用 楼主 howema 的回复:string s = "156.358123"; double d = atof(s.c_str()); 转换后为什么后面几位小数被去掉了 怎样能全部转过来? 即确保把……
THX,其实最好还是自己写个比较好
mymtom 2012-12-27
  • 打赏
  • 举报
回复
double有效数字在16位之间。楼主的例子用atof完全没有问题的,而且atof的返回值是double, 而不是字面float: #include <stdlib.h> double atof(const char *nptr); 只是printf("%f\n", ...)只打印6位小数,

#include <string>
using namespace std;

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    string s = "156.358123";
    double d = atof(s.c_str());
    printf("%.16f\n", d);
    printf("%f\n", d);
    return 0;
}
zilaishuichina 2012-12-27
  • 打赏
  • 举报
回复
你要完整的精度的话 那就自己写个 double myatof(char *)

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧