how convert char* to double without lose precision?

see these char*:

char *chr="3.1415926";

if i use the atof(), it prints: "3.14159"
so how can i convert char* to double without lose precision?
If you use std::cout to print the value you can use std::setprecision.

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <iomanip>
#include <cstdlib>

int main()
{
	std::cout << std::setprecision(10);
	std::cout << std::atof("3.14159") << '\n'; // prints 3.14159
}

http://www.cplusplus.com/reference/iomanip/setprecision/
There is no "it". There is only default format and ways to modify it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>     // std::cout, std::fixed
#include <iomanip>      // std::setprecision
#include <cstdlib>      // atof
#include <string>       // std::stod

int main () {
  double f = 3.1415926;
  double g = atof("3.1415926");
  double h = std::stod("3.1415926");
  std::cout << f << " vs " << g << " vs " << h << '\n';
  std::cout << std::setprecision(5) << f << " vs " << g << '\n';
  std::cout << std::setprecision(10) << f << " vs " << g << '\n';
  std::cout << std::fixed;
  std::cout << std::setprecision(5) << f << " vs " << g << '\n';
  std::cout << std::setprecision(10) << f << " vs " << g << " vs " << h << '\n';
  return 0;
}


That being said, the binary floating point representation in memory has limits and is better to be described as "strange and weird" rather than as "accurate and intuitive".
i see. thank you so much for correct me. now works. i didn't knew about precision
Topic archived. No new replies allowed.