By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

How to control the precision of double

P: n/a
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!

Mar 16 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
* 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.
--
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?
Mar 16 '07 #2

P: n/a
Bo Yang wrote:
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
Mar 16 '07 #3

P: n/a
On Mar 16, 11:04 am, "Alf P. Steinbach" <a...@start.nowrote:
* 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.

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

Mar 16 '07 #4

P: n/a
* Sarath:
[Quoting signature]

Please don't quote signatures -- corrected.
* Sarath:
On Mar 16, 11:04 am, "Alf P. Steinbach" <a...@start.nowrote:
>* 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.

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.
--
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?
Mar 16 '07 #5

P: n/a
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?
Mar 16 '07 #6

P: n/a
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?
Mar 16 '07 #7

P: n/a
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?
Mar 16 '07 #8

P: n/a
On 16 Mar, 08:02, "Bo Yang" <struggl...@gmail.comwrote:
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.

--
Erik Wikström

Mar 16 '07 #9

P: n/a
Alf P. Steinbach wrote:
* 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.

C-ism!

In C++ eschew macros. Use uppercase where it is logically appropriate
(manifest constants like PI are a good use0.
Mar 16 '07 #10

P: n/a
Bo Yang <st********@gmail.comwrote:
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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 16 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.