 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Andrew Guest
|
Posted: Thu Feb 19, 2004 2:08 pm Post subject: std::ios_base::fixed fails for +(1.79769E+308) |
|
|
Hi
I've got the following piece of code that is failing when I pass it a
double value of +(1.79769E+308). If I change the fixed notation to
scientific, std::ios_base::scientific, the code succeeds. Can anyone
explain why this happens...is it a default precision issue for fixed
notation and what is the precision compared to scientific notation.
void StringTokenizer::addToken(SVR::Double token)
{
std::strstream conversionStream;
SVR::String literal;
conversionStream.setf(std::ios_base::fixed);
conversionStream << token;
conversionStream >> literal;
addLiteral(literal);
}
Many thanks
Andrew
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Billy O'Connor Guest
|
Posted: Fri Feb 20, 2004 1:17 pm Post subject: Re: std::ios_base::fixed fails for +(1.79769E+308) |
|
|
[email]awylie (AT) netcomuk (DOT) co.uk[/email] (Andrew) writes:
| Quote: | Hi
I've got the following piece of code that is failing when I pass it a
double value of +(1.79769E+308). If I change the fixed notation to
scientific, std::ios_base::scientific, the code succeeds. Can anyone
explain why this happens...is it a default precision issue for fixed
notation and what is the precision compared to scientific notation.
void StringTokenizer::addToken(SVR::Double token)
{
std::strstream conversionStream;
SVR::String literal;
conversionStream.setf(std::ios_base::fixed);
conversionStream << token;
conversionStream >> literal;
addLiteral(literal);
}
|
This works, modified a bit:
void addToken(double token)
{
std::strstream conversionStream;
string literal;
conversionStream.setf(std::ios_base::fixed);
conversionStream << token;
conversionStream >> literal;
cout << literal << endl;
}
int main(void)
{
addToken(1.79769E+308);
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Fri Feb 20, 2004 5:28 pm Post subject: Re: std::ios_base::fixed fails for +(1.79769E+308) |
|
|
[email]awylie (AT) netcomuk (DOT) co.uk[/email] (Andrew) wrote in message
news:<4f8979bf.0402181241.56a18d95 (AT) posting (DOT) google.com>...
| Quote: | I've got the following piece of code that is failing when I pass it a
double value of +(1.79769E+308).
|
What do you mean by fails: it doesn't compile, it generates an error
message at runtime, it crashes?
| Quote: | If I change the fixed notation to
scientific, std::ios_base::scientific, the code succeeds.
|
While it shouldn't "fail", converting such a value to a fixed notation
doesn't make much sense. Unless your goal is to generate a string with
a couple of hundred 0's.
| Quote: | Can anyone explain why this happens...
|
Error in the library implementation?
| Quote: | is it a default precision issue for fixed notation and what is the
precision compared to scientific notation.
|
Both have a default precision of 6 digits after the decimal. With
scientific notation, this means a total of 7 digits, always. With
fixed, of course, the actual number of significant digits depends on the
actual value -- in your case, you have asked for 314 total digits. A
double doesn't have this precision, of course (at least not on any
machine I've used), but the library should generate the digits anyway,
generating 0's once you've gone beyond the actual precision (typically
about 17 digits).
(Formally, the C++ standard refers to the C standard for the
signification of formatting flags. And the C standard has the somewhat
curious sentence that "The number of characters that can be produced by
any single conversion shall be at least 4095." Which means that
something like "s << std::string( 5000, 'x' )" is perhaps undefined
behavior, but what you are trying to do should work, even if it is
senseless.)
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Sat Feb 21, 2004 4:11 am Post subject: Re: std::ios_base::fixed fails for +(1.79769E+308) |
|
|
Andrew wrote:
| Quote: | Hi
I've got the following piece of code that is failing when I pass it a
double value of +(1.79769E+308).
|
Presumably that is DBL_MAX on your system?
Also what do you mean by failing? I don't see any way in which
the code can indicate failure, so I guess you mean that the string
it produces is incorrect.
| Quote: | If I change the fixed notation to
scientific, std::ios_base::scientific, the code succeeds. Can anyone
explain why this happens...is it a default precision issue for fixed
notation and what is the precision compared to scientific notation.
|
I think it is a bug in your implementation, but without knowing
what the failure is it's hard to say.
| Quote: | void StringTokenizer::addToken(SVR::Double token)
{
std::strstream conversionStream;
SVR::String literal;
conversionStream.setf(std::ios_base::fixed);
conversionStream << token;
conversionStream >> literal;
addLiteral(literal);
}
|
Also, please don't post code that depends on classes or functions that
aren't well-known (and preferably part of the standard library). The
following program should demonstrate the problem where it exists:
#include <cfloat>
#include <iostream>
#include <ostream>
#include <string>
#include <strstream>
int main()
{
std::strstream conversionStream;
std::string literal;
conversionStream.setf(std::ios_base::fixed);
conversionStream << DBL_MAX;
std::cout.write(conversionStream.str(), conversionStream.pcount())
<< 'n';
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| 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
|
|