Note that there are some explanatory texts on larger screens.

plurals
  1. POUnexpected results using istringstream for string to double conversion in C++
    primarykey
    data
    text
    <p>I'm currently trying to take a string ("0.1") and convert it to a double using C++ in Xcode on 10.6 with gcc4.2.</p> <p>I'm using a function I pinched from <a href="https://stackoverflow.com/questions/392981/how-can-i-convert-string-to-double-in-c">another question</a>, but when I try to use the function, my input according to gdb is (string)"0.1", but my output is (double)2.1220023981051542e-314.</p> <p>Here is my snippet copied straight out the code:</p> <pre><code>double strToDouble(std::string const &amp;numberString) { istringstream inStream(numberString); double doubleValue; inStream &gt;&gt; doubleValue; if(!(inStream &amp;&amp; (inStream &gt;&gt; std::ws).eof())) { return 0.0; } return doubleValue; }; </code></pre> <p>I'm using C++ rather than Obj-C, as it will probably have to be compiled on a *nix or windows machine eventually.</p> <p>I'm naturally a PHP programmer, but have some number crunching I need to speed up, so I'm doing it in a compiled language. It's been a long time since University dealing with a lower level language =P. So, if anyone has an ideas, it would be greatly appreciated...</p> <p>Drew J. Sonne.</p> <p>Full code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;sstream&gt; using namespace std; /* * @var delimiters the string to define a break by * @var str the string to break * @var tokens where to store the broken string parts. */ void explode(const string&amp; delimiters, const string&amp; str, vector&lt;string&gt;&amp; tokens) { // Skip delimiters at beginning. string::size_type lastPos = str.find_first_not_of(delimiters, 0); // Find first "non-delimiter". string::size_type pos = str.find_first_of(delimiters, lastPos); while (string::npos != pos || string::npos != lastPos) { // Found a token, add it to the vector. tokens.push_back(str.substr(lastPos, pos - lastPos)); // Skip delimiters. Note the "not_of" lastPos = str.find_first_not_of(delimiters, pos); // Find next "non-delimiter" pos = str.find_first_of(delimiters, lastPos); } }; /* * @var numberString double to be converted as a string. */ double strToDouble(std::string const &amp;numberString) { istringstream inStream(numberString); double doubleValue; inStream &gt;&gt; doubleValue; if(!(inStream &amp;&amp; (inStream &gt;&gt; std::ws).eof())) { return 0.0; } return doubleValue; }; class Cluster { private: vector&lt;double&gt; latStore; vector&lt;double&gt; lngStore; public: Cluster(int,string,string); }; /* * @var latString a comma seperated list of doubles for the latitudes. * @var lngString a comma separated list of doubles for the longitudes. */ Cluster::Cluster(int newLocationLength, string latString, string lngString) { // mark our vectors vector&lt;string&gt; latStoreString; vector&lt;string&gt; lngStoreString; // Explode our input strings. explode(",", latString, latStoreString); explode(",", lngString, lngStoreString); for( int i = 0; i &lt; latStoreString.size(); i++) { // Grab the latitude and store it. string tempLat = latStoreString[i]; double latDouble = strToDouble(tempLat); latStore.push_back( strToDouble(tempLat) ); // Grab the longitude and store it. string tempLng = lngStoreString[i]; lngStore.push_back( strToDouble(tempLng) ); } } int main (int argc, char * const argv[]) { Cluster *rect = new Cluster("0.1,0.4","0.1,0.4"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload