473,769 Members | 7,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to control the precision of double

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 6604
* 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
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
On Mar 16, 11:04 am, "Alf P. Steinbach" <a...@start.now rote:
* 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::prec ision(); functions

Mar 16 '07 #4
* Sarath:
[Quoting signature]

Please don't quote signatures -- corrected.
* Sarath:
On Mar 16, 11:04 am, "Alf P. Steinbach" <a...@start.now rote:
>* 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::prec ision(); 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
To express my trouble with an example.

In my app, I declare a variable

double test(){
double PI = 3.1415926535897 93;
return PI;
}

But I got an number with 3.14159265359.
Why?
Mar 16 '07 #6
To express my trouble with an example.

In my app, I declare a variable

double test(){
double PI = 3.1415926535897 93;
return PI;
}

But I got an number with 3.14159265359.
Why?
Mar 16 '07 #7
To express my trouble with an example.

In my app, I declare a variable

double test(){
double PI = 3.1415926535897 93;
return PI;
}

But I got an number with 3.14159265359.
Why?
Mar 16 '07 #8
On 16 Mar, 08:02, "Bo Yang" <struggl...@gma il.comwrote:
To express my trouble with an example.

In my app, I declare a variable

double test(){
double PI = 3.1415926535897 93;
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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
6262
by: BigMan | last post by:
How can I check if assignment of a float to a double (or vice versa) will result in loss of precision?
5
6092
by: DAVID SCHULMAN | last post by:
I've been trying to perform a calculation that has been running into an underflow (insufficient precision) problem in Microsoft Excel, which calculates using at most 15 significant digits. For this purpose, that isn't enough. I was reading a book about some of the financial scandals of the 1990s called "Inventing Money: The Story of Long-Term Capital Management and the Legends Behind it" by Nicholas Dunbar. On page 95, he mentions that...
2
5456
by: Ronny Mandal | last post by:
Is there a function that will do this task properly? -- Thanks Ronny Mandal
3
23755
by: Madan | last post by:
Hi all, I had problem regarding float/double arithmetic only with + and - operations, which gives inaccurate precisions. I would like to know how the arithmetic operations are internally handled by C# or they are hardware (processor) dependent. Basic addition operation errors, for ex: 3.6 - 2.4 = 1.19999999999 or 1.20000000003 There are the erroneous values I'm getting. I'm using C#.Net v1.1 Please reply me how these operations are...
2
4237
by: mdeaver2003 | last post by:
I'm trying to output a double using a precision that varies, governed by the value of a precision variable. In C I can do it like this: double pi = 3.14159; int prec = 4; printf( "%.*f", prec, pi ); // 3.1416 The * in "%.*f" says to use the value of the next
6
5717
by: R.Biloti | last post by:
Hi folks I wrote the naive program to show up the unit roundoff (machine precision) for single and double precision: #include <stdio.h> int main (void) { double x;
2
3032
by: rupert | last post by:
i've got the following code: #include <iostream> #include <string> #include <vector> #include <iomanip> using namespace std; int main(double argc, char* argv) { double r = 0.01;
9
6320
by: asdf | last post by:
I want to set the computation precision to quadruple precision, how can I do it in C++ coding? Thanks all.
137
6716
by: mathieu.dutour | last post by:
Dear all, I want to do multiprecision floating point, i.e. I want to go beyond single precision, double precision and have quadruple precision, octuple precision and the like, and possibly with high speed. What would be the possible alternatives? Thanks for any help
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.