String to Double (Precise Convertion)

Feb 3, 2012 at 2:19am
Hello, I am working on a calculator. I want to take data from the user as a string and then convert it into a double to preform calculations. I need to know the best way to convert a string to a double with little loss from the data. I am aware of atof convertion. This convertion truncates the data by rounding. I need a more precise way doing converting. The example below shows how the data is being truncated. Any help would be greatly appreciated!

1
2
3
4
5
6
7
8
        string number;
	double an_number;

	number="9994324.34324324343242";
	an_number=atof(number.c_str());
	cout<<"string: "<<number<<endl;
	cout<<"double: "<<an_number;
	cin.ignore();


string: 9994324.34324324343242
double: 9.99432e+006 
Feb 3, 2012 at 2:26am
The decimal fraction 9994324.34324324343242 does not correspond to any valid value of a double The nearest valid double is 9994324.34324324317276477813720703125, which is 5365662024980801 * 2-29, and that is exactly the value now stored in your an_number.

The rounding occurs at output. Try std::cout << std::setprecision(100) << an_number << '\n'; to see all digits.
Last edited on Feb 3, 2012 at 2:31am
Feb 3, 2012 at 2:36am
I'm not sure I understand. Even if the value of number is changed the conversion is not correct. For example:

1
2
3
4
5
6
7
      string number;
	double an_number;

	number="1.3432432431727647781";
	an_number=atof(number.c_str());
	cout<<"string: "<<number<<endl;
	cout<<"double: "<<setprecision(100)<<an_number<<endl;



string: 1.3432432431727647781
double: 1.34324324317276480000000000000000000000000000000000000000000


Last edited on Feb 3, 2012 at 2:37am
Feb 3, 2012 at 2:50am
Oh, you must be using Visual Studio. That's as far as it will let you go with doubles. You can get more precision from long doubles if you need it.
Feb 3, 2012 at 2:57am
Yes I am using visual studio. So the convertion is correct but complier specific? On other compilers will I get more precision?
Feb 3, 2012 at 3:07am
On most compilers you'd get 1.34324324317276477813720703125, which is the exact value of the double that's closest to your original decimal fraction. VS doesn't bother with digits beyond the 16th because they'll never equal the digits beyond the 16th in your decimal number.
Feb 3, 2012 at 3:20am
Thanks for the great information! I think this solves the mystery! I ran the same code on Xcode and got:

string: 1.3432432431727647781
double: 1.34324324317276477813720703125


So you are right! Thanks again!
Feb 3, 2012 at 3:27am
why not write the new code to convert by yourself? then you can control all thing
Feb 3, 2012 at 3:38am
Great idea! Any idea on how would I go about doing this?
Feb 3, 2012 at 4:07am
closed account (o1vk4iN6)
Microsoft's compiler doesn't support extended floating precision (which is what long double usually is interpreted as, 80 bits) it's only there to support C libraries/programs, kind of a shame really.
Last edited on Feb 3, 2012 at 4:08am
Topic archived. No new replies allowed.