 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Tue Jul 25, 2006 8:12 am Post subject: string to double conversion - atof() vs istringstream |
|
|
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. |
|
| Back to top |
|
 |
Guest
|
Posted: Tue Jul 25, 2006 8:22 am Post subject: Re: string to double conversion - atof() vs istringstream |
|
|
Forgot to mention this before. atof() behaviour is expected for -308
exp and that of istringstream is wrong.
Regards,
Harry.
sharmaharish (AT) gmail (DOT) com wrote:
| Quote: | 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. |
|
|
| Back to top |
|
 |
Guest
|
Posted: Sat Jul 29, 2006 9:10 am Post subject: Re: string to double conversion - atof() vs istringstream |
|
|
mlimber,
I tried compiling the same program with a later ver of MS C/C++
compiler, which is ...
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42
for 80x86
With this compiler the test program gives expected results. But the GCC
compiler that I am using is gcc version 3.3.1 (mingw special
20030804-1), and it still gives out garbage. And by the way I believe,
there is no garbage i/p in the test program and hence there is no
question of GIGO. Moreover, I don't think there is any need of
exception handling in the test code with the given valid input and I
don't understand how that extra piece of error checking will help. If
your compiler/lib gives you expected results with the given input, it
should behave the same without that as well.
Another simple test to confirm the problem is,
int main() { double d; cin >> d; cout << d; return 0; }
Just enter the value 2.22507-308 after compiling with your compiler and
it should show the expected result. But if you try this with the GCC
compiler version that I have mentioned, it gives out garbage. This
piece of code behaves similar to the earlier test program with both the
compilers that I used.
Lastly I tried your changes with GCC and it generates exception and
that tells me that there is some definite issue in the related version
of c++ lib that it is using.
Regards,
Harish Sharma |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Jul 29, 2006 9:10 am Post subject: Re: string to double conversion - atof() vs istringstream |
|
|
sharmaharish (AT) gmail (DOT) com wrote:
| Quote: | if your compiler/lib gives you expected results with the given input, it
should behave the same without that as well.
|
I actually meant, if your compiler/lib gives you expected results with
the give input AND THE ERROR HANDLING CODE, it should behave the same
without that (error handling code) as well.
Regards,
Harish Sharma |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|