字符串到双重转换 - atof()vs istringstream [英] string to double conversion - atof() vs istringstream

查看:52
本文介绍了字符串到双重转换 - atof()vs istringstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个转换函数,将值从string转换为

特定类型。为此我有一个模板功能,看起来像

这个...


模板< class T>

T值( const string& s)

{

istringstream(s);

T val;

是> val ;

返回val;

}


现在对于double类型,最低正值是2.22507-308我得到了当我使用这个函数vs atof()函数时,
不同的答案。我不是在这里得到理由的b $ b。有趣的是,如果我将指数值从-308减少到-307,我会得到类似的结果。


问候,

哈利。

I need a conversion function that converts values from string to a
particular type. For this I have a template function that looks like
this ...

template<class T>
T value(const string& s)
{
istringstream(s);
T val;
is >val;
return val;
}

Now for double type the lowest positive value is 2.22507-308 and I get
different answers when I use this function vs atof() function. I am not
getting the reason here. Interestingly, if I reduce the exponent value
from -308 to -307, I get similar results.

Regards,
Harry.

推荐答案

忘记提及此事。 atof()行为预计为-308

exp和istringstream的行为是错误的。


问候,

Harry。

sh**********@gmail.com 写道:
Forgot to mention this before. atof() behaviour is expected for -308
exp and that of istringstream is wrong.

Regards,
Harry.

sh**********@gmail.com wrote:

我需要一个转换函数,将值从字符串转换为

特定类型。为此我有一个模板功能,看起来像

这个...


模板< class T>

T值( const string& s)

{

istringstream(s);

T val;

是> val ;

返回val;

}


现在对于double类型,最低正值是2.22507-308我得到了当我使用这个函数vs atof()函数时,
不同的答案。我不是在这里得到理由的b $ b。有趣的是,如果我将指数值从-308减少到-307,我会得到类似的结果。


问候,

哈利。
I need a conversion function that converts values from string to a
particular type. For this I have a template function that looks like
this ...

template<class T>
T value(const string& s)
{
istringstream(s);
T val;
is >val;
return val;
}

Now for double type the lowest positive value is 2.22507-308 and I get
different answers when I use this function vs atof() function. I am not
getting the reason here. Interestingly, if I reduce the exponent value
from -308 to -307, I get similar results.

Regards,
Harry.


sh ****** **** @gmail.com 写道:

我需要一个转换函数,将值从字符串转换为

特殊类型。为此我有一个模板功能,看起来像

这个...


模板< class T>

T值( const string& s)

{

istringstream(s);

T val;

是> val ;

返回val;

}


现在对于double类型,最低正值是2.22507-308我得到了当我使用这个函数vs atof()函数时,
不同的答案。我不是在这里得到理由的b $ b。有趣的是,如果我将指数值

从-308减少到-307,我会得到类似的结果。
I need a conversion function that converts values from string to a
particular type. For this I have a template function that looks like
this ...

template<class T>
T value(const string& s)
{
istringstream(s);
T val;
is >val;
return val;
}

Now for double type the lowest positive value is 2.22507-308 and I get
different answers when I use this function vs atof() function. I am not
getting the reason here. Interestingly, if I reduce the exponent value
from -308 to -307, I get similar results.



结果有何不同?众所周知,浮点数很难实现。参见,例如,这些常见问题解答:

http://www.parashift.com/c++-faq-lit...html#faq-29.16
http://www.parashift.com/c++-faq-lit...html#faq-29.18


干杯! --M

How different are the results? Floating point numbers are notoriously
tricky. See, e.g., these FAQs:

http://www.parashift.com/c++-faq-lit...html#faq-29.16
http://www.parashift.com/c++-faq-lit...html#faq-29.18

Cheers! --M


2006年7月24日20:12:21 -0700, sh ********** @ gmail.com 写道:
On 24 Jul 2006 20:12:21 -0700, sh**********@gmail.com wrote:

>我需要一个将值从字符串转换为特定类型的转换函数。为此,我有一个模板功能,看起来像
这个...

模板< class T>
T值(const string& s)
{

istringstream(s);

T val;

是> val;

返回val;
}
>I need a conversion function that converts values from string to a
particular type. For this I have a template function that looks like
this ...

template<class T>
T value(const string& s)
{
istringstream(s);
T val;
is >val;
return val;
}



您不能检查该函数中的任何返回值或错误值。此外,

我更喜欢模板的显式stringToDoulble()函数。

You don''t check any return or error value in that function. Moreover,
I would prefer an explicit stringToDoulble() function to a template.


>现在对于double类型,最低正值是2.22507-308当我使用这个函数和atof()函数时,我得到了不同的答案。我不是在这里得到理由。有趣的是,如果我将指数值从-308减少到-307,我会得到类似的结果。
>Now for double type the lowest positive value is 2.22507-308 and I get
different answers when I use this function vs atof() function. I am not
getting the reason here. Interestingly, if I reduce the exponent value
from -308 to -307, I get similar results.



使用strtod

http://www.opengroup.org/onlinepubs/...ns/strtod.html

并检查endptr,错误和回报价值。


祝福,

Roland Pibinger

Use strtod
(http://www.opengroup.org/onlinepubs/...ns/strtod.html)
and check endptr, errno and return value.

Best wishes,
Roland Pibinger


这篇关于字符串到双重转换 - atof()vs istringstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆