How to control the precision of double


B

Bo Yang

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?

Thanks in advance!
 
Ad

Advertisements

A

Alf P. Steinbach

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

double PI = 3.141592675932;

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

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

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

floor( 10000000.0*PI )/10000000.0


Or, in another way, how does the double presented in computer
Wikipedia.


, and
can I get different precision presentation from the same variable?

Depends what you mean.
 
K

Kai-Uwe Bux

Bo said:
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 .

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.

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

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
 
S

Sarath

* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

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::precision(); functions
 
A

Alf P. Steinbach

* Sarath:
[Quoting signature]

Please don't quote signatures -- corrected.


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

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

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

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

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

The setprecision manipulator calls the precision member function.
 
B

Bo Yang

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?
 
Ad

Advertisements

B

Bo Yang

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?
 
B

Bo Yang

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?
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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?

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.
 
R

Ron Natalie

Alf said:
* Bo Yang:

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

C-ism!

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

Advertisements

M

Marcus Kwok

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

double PI = 3.141592675932;

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.
 
Ad

Advertisements


Top