# How to control the precision of double

Discussion in 'C++' started by Bo Yang, Mar 16, 2007.

1. ### Bo YangGuest

Hi, I am confused by the double type in C++. I don't know whether
below is legal or possible:

double PI = 3.141592675932;

Now, can I get another double variable from PI with lower precision,
for
example, 3.1415926 .

Or, in another way, how does the double presented in computer, and
can I get different precision presentation from the same variable?

Bo Yang, Mar 16, 2007

2. ### Alf P. SteinbachGuest

* Bo Yang:
Java'ism: in C++ reserve all uppercase for macro names.

That's not lower precision, that's a truncation in decimal notation.

floor( 10000000.0*PI )/10000000.0

Depends what you mean.

Alf P. Steinbach, Mar 16, 2007

3. ### Kai-Uwe BuxGuest

The precision of a floating point number is a property of the type, not of
the value (or more precisely, given a built-in floating point F type and a
real number x, then the error | x - x_rep | between the number x and
its best-approximating representation as a value x_rep of type F is not
yours to chose, it is determined by F and x). If you need something else,
you have to leave the built-in types and use a high-precision library,
interval arithmetic, or some other fancy numerics technology.

You can choose the number of significant digits that you want to print at
the time you output the variable. This, however, will not affect the
precision of the value of the variable used subsequently in the program.

Best

Kai-Uwe Bux

Kai-Uwe Bux, Mar 16, 2007
4. ### SarathGuest

You can use setprecision function to control the floating point
precision.

If you are using Visual C++, you can use _control87, _controlfp
funcitons to set the desired floating point precision. See MSDN for
more details.

If you are trying to control the output precision using cout or some
other stream operators, it can be controlled to through
std::cout: recision(); functions

Sarath, Mar 16, 2007
5. ### Alf P. SteinbachGuest

* Sarath:
[Quoting signature]

Please don't quote signatures -- corrected.

* Sarath:
No, it controls output formatting, not floating point precision; see below.

Yes, but off-topic since it's platform-specific.

The setprecision manipulator calls the precision member function.

Alf P. Steinbach, Mar 16, 2007
6. ### Bo YangGuest

To express my trouble with an example.

In my app, I declare a variable

double test(){
double PI = 3.141592653589793;
return PI;
}

But I got an number with 3.14159265359.
Why?

Bo Yang, Mar 16, 2007
7. ### Bo YangGuest

To express my trouble with an example.

In my app, I declare a variable

double test(){
double PI = 3.141592653589793;
return PI;
}

But I got an number with 3.14159265359.
Why?

Bo Yang, Mar 16, 2007
8. ### Bo YangGuest

To express my trouble with an example.

In my app, I declare a variable

double test(){
double PI = 3.141592653589793;
return PI;
}

But I got an number with 3.14159265359.
Why?

Bo Yang, Mar 16, 2007
9. ### =?iso-8859-1?q?Erik_Wikstr=F6m?=Guest

Got as in printed out? As others have pointed out, the number you get
when you use std::cout << test() is not the number used in
calculations but rather an approximation created while printing. Using
for example setprecision() you can control how close to the real
number returned by test() you will print, and if you set it high
enough you get the number used.

This number might not be the one you assigned since computers can't
represent every number exactly and only have a limited precision,
search the web or Wikipedia for "floating point" and you'll find a
description of how it all works. There is not much you can do to
affect the precision since it depends on the hardware, however you can
use a different type, in C++ there are three different types you can
use, float, double and long double, where float has the least
precision and long double has the most. For most kinds of calculations
double will suffice.

=?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 16, 2007
10. ### Ron NatalieGuest

C-ism!

In C++ eschew macros. Use uppercase where it is logically appropriate
(manifest constants like PI are a good use0.

Ron Natalie, Mar 16, 2007
11. ### Marcus KwokGuest

Just FYI: Your value for PI is inaccurate. Using the same number of
digits, it should be:

double PI = 3.141592653589;

or

double PI = 3.141592653590;

depending if you want to truncate or round the value.

Marcus Kwok, Mar 16, 2007