Problem with stod function in C++

Hello,

I'd like to convert the string to double. after convert, s is not the same of d.

Could you please help me?

https://ibb.co/fMD1LT2

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include<fstream>
#include<math.h>
#include<string>
using namespace std;

int main()
{
	string s = "0.810911540966214";

	double d = stod(s); // not the same of s  //  0.81091154096621398	

	

	return 0;
}
Last edited on
after convert, s is not the same of d


True. As expected. Round-tripping double->string->double is fraught with rounding issues as per the way numbers are stored internally.

Also see std::from_chars() https://en.cppreference.com/w/cpp/utility/from_chars

Topic archived. No new replies allowed.