446,260 Members | 1,305 Online
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
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"

 P: n/a * Sarath: [Quoting signature] Please don't quote signatures -- corrected. * Sarath: On Mar 16, 11:04 am, "Alf P. Steinbach" * Bo Yang: >>Hi, I am confused by the double type in C++. I don't know whetherbelow 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,forexample, 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. >>, andcan 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"

 P: n/a Alf P. Steinbach wrote: * Bo Yang: >Hi, I am confused by the double type in C++. I don't know whetherbelow 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

### This discussion thread is closed

Replies have been disabled for this discussion.