如何控制双精度 [英] How to control the precision of double

查看:97
本文介绍了如何控制双精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++中的double类型感到困惑。我不知道

以下是合法的还是可能的:


双倍PI = 3.141592675932;


现在,我可以从精确度较低的PI获得另一个双变量,



例如,3.1415926。


或者,在另一种方式,如何在计算机中呈现双重,并且

我可以从同一个变量获得不同的精确表示吗?


提前感谢!

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!

推荐答案

* Bo Yang:
* Bo Yang:

我被双重困惑用C ++输入。我不知道

以下是合法的还是可能的:


double PI = 3.141592675932;
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''主义:在C ++中保留宏名称的全部大写。

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


现在,我可以从PI获得另一个双精度变量,精度更低,



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



这不是精度不高,这是十进制表示法的截断。


floor(10000000.0 * PI )/10000000.0

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?



取决于你的意思。

-

答:因为它弄乱了人们的订单通常阅读文字。

问:为什么这么糟糕?

A:热门帖子。

问:什么是最多的在usenet和电子邮件中烦人的事情?

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?


Bo Yang写道:
Bo Yang wrote:

我很困惑通过C ++中的double类型。我不知道

以下是合法的还是可能的:


双倍PI = 3.141592675932;


现在,我可以从PI获得另一个双精度变量,精度更低,



例如,3.1415926。
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 .



浮点数的精度是该类型的属性,而不是价值(或更确切地说,给定一个内置) -in浮点F类型和

实数x,然后错误| x - x_rep |在数字x和

之间,其最佳近似表示为值x_rep类型F不是你选择的
,它由F和x确定。如果你需要别的东西,

你必须离开内置类型并使用高精度库,

区间算术,或其他一些花哨的数字技术。

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?



您可以在输出变量时选择要打印的有效位数

。但是,这不会影响随后在程序中使用的变量值的

精度。

最佳


Kai-Uwe Bux

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


3月16日上午11:04,Alf P. Steinbach < a ... @ start.nowrote:
On Mar 16, 11:04 am, "Alf P. Steinbach" <a...@start.nowrote:

* Bo Yang:
* Bo Yang:

我我对C ++中的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;
double PI = 3.141592675932;



Java''主义:在C ++中保留宏名称的全部大写。


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


现在,我可以从PI获得另一个双变量,精度较低,



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



这不是精度不高,这是十进制表示法的截断。


floor(10000000.0 * PI )/10000000.0


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中呈现双倍。


Wikipedia.


,和

我可以从同一个变量获得不同的精确表示吗?
, and
can I get different precision presentation from the same variable?



取决于你的意思。


-

答:因为它搞砸了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问: usenet和电子邮件中最烦人的事情是什么?


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?



您可以使用setprecision函数来控制浮点数

精度。


如果你使用Visual C ++,您可以使用_control87,_controlfp

函数来设置所需的浮点精度。有关详细信息,请参阅MSDN



如果您尝试使用cout或某些其他流操作符来控制输出精度

,它可以通过

std :: cout :: precision();功能

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


这篇关于如何控制双精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆