|
-
Feb 13th, 2006, 07:35 PM
#1
Thread Starter
PowerPoster
String Tokenizer -- nextDouble()
This is a method within a class I wrote that is used to split up a string into tokens given the delimeter. I wanted functionality similiar to the Java equivelant.
PHP Code:
/**
* Get the next double token -- descructive
* @return double - The double returned
* @throw - NumberFormatException
*/
double StringTokenizer::nextDouble()
{
string strToken = next();
if (!strToken.empty()) {
istringstream dblCheckStream(strToken);
double dValue = 0.0;
if (dblCheckStream >> dValue) {
return dValue;
} else {
m_strData = strToken + m_strData;
throw(NumberFormatException("The next token does not contain a correct double value!"));
}
}
return 0.0;
}
Now the output is not as expected:
When the string "34.21249" is read out as a double I get 34.2125
Wow, look at the rounding... It rounds any number with more than 4 decimal places, it rounds to the 4rth decimal place.
Is there perhaps a more accurate way of parsing a double out of a string?
Edit:
Actually looking at the numbers through a debugger it seems nothing is accurate:
"12.5644" = 12.564399999999999
"34.21249" = 34.21249000000003
"56.000005" = 56.000005000002
Last edited by Halsafar; Feb 13th, 2006 at 07:40 PM.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Feb 14th, 2006, 12:35 AM
#2
Re: String Tokenizer -- nextDouble()
Because of the way doubles and floats are stored, this inaccuracy past certain decimal places is unavoidable. That's why when comparing floating point values, it is often best to not do direct equality, but rather check if the difference is smaller than a given value.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Feb 14th, 2006, 02:34 PM
#3
Thread Starter
PowerPoster
Re: String Tokenizer -- nextDouble()
So this behaviour is normal then?
Unavoidable.
Thanks,
Halsafar
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Feb 16th, 2006, 01:08 PM
#4
Frenzied Member
Re: String Tokenizer -- nextDouble()
the culprit is most likey the function that converts the float to string. while in memory the float is correct, it appears incorrect because it gets rounded when you convert it back to text. try this:
Code:
double hey = atof("34.2124956");
if (hey == 34.2124956) // the value was parsed correctly, it's 34.2124956
cout << hey; // outputs 34.2125, which would appear to be wrong
else
cout << "no";
Last edited by dis1411; Feb 16th, 2006 at 01:13 PM.
-
Feb 16th, 2006, 01:09 PM
#5
Re: String Tokenizer -- nextDouble()
Or use the setprecision() output modifier to increase output precision.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 16th, 2006, 01:42 PM
#6
Thread Starter
PowerPoster
Re: String Tokenizer -- nextDouble()
I use:
istringstream dblCheckStream(strToken);
double dValue = 0.0;
dblCheckStream >> dValue
To parse the double, this same method also works for ints. Could this be causing any more problems?
SetPrecision, would that just allow me greater accuracy with less decimal points?
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|