user1775213
user1775213

Reputation: 521

C/C++ : double constants in class are inaccurate

I'm writing a class in C++ that needs some constants that are accessible throughout the whole class. Normally I would use a #define, a const- or a static declaration but there is something strange happening: So for example I write

#define Rm 8.3144621

but std::cout << Rm << std::endl;

prints 8.31446.

I also tried

#define Rm 831.44621e-2

and const double Rm = 8.3144621

and static double Rm = 8.3144621

and every possible way to initialize or cast to a double constant.

Is there a solution for that or do I have to use fields?

Upvotes: 2

Views: 126

Answers (2)

cxyzs7
cxyzs7

Reputation: 1227

Try

std::cout << std::setprecision(8) << Rm << std::endl

Upvotes: 0

taocp
taocp

Reputation: 23644

If you need to output the double with all digits, you need to set correct precision:

std::cout << std::fixed;
std::cout << std::setprecision(7) << Rm << std::endl;

See a live demo here: Double print Demo

Upvotes: 6

Related Questions