有关使用double的快速问题 [英] Quick question about the use of double

查看:81
本文介绍了有关使用double的快速问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用用户输入的值来处理需要进行计算的一段代码.

但是奇怪的是,这些值是近似的.例如,如果用户输入28和32.8,则应为12.8的差计算为12.7777778.但是我要处理的数据类型需要精确的值.

我已将变量声明为double.
有任何建议吗??

Hi,

Im working on piece of code where the a calculation needs to be done, using the values entered by the user.

But strangely, the values are being approximated. For example if the user enters 28 and 32.8, the difference which should be 12.8 is calculated as 12.7777778.. But the kind of data I''m dealing with, needs precise values.

I have declared the variable as double.
Any suggestions????

推荐答案

请参阅我的评论.您是否听说过,例如,实数集的某些无限子集是先验数?示例是πe.您是否知道以字符串形式表示的每个此类数字的任何表示形式都包含无数个数字.假设您具有任意有限长度的任意文本(例如Romeo和Juliette);以任何方式将其编码为十进制数字字符串形式.事实是:在π的十进制表示中,可以找到完全相同的子序列.这是经过数学证明和众所周知的事实.你感到惊讶吗?

这是一个合理的准确性.请参阅:
http://en.wikipedia.org/wiki/Double-precision_floating-point_format [ ^ ].

—SA
Please see my comment. Did you ever heard that, for example, some infinite subset of the set of real numbers are transcendental numbers? The examples are π and e. Do you know that any representations of each of such numbers in the form of the string contains infinite number of digits. Suppose you have any arbitrary text of any finite length (say, Romeo and Juliette); code it in the form of, say, a string of decimal digits in any way. The fact is: in the decimal representation of, say π, one can find exact same sub-sequence. This is mathematically proven and well-know fact. Do you feel surprised?

This is a reasonable accuracy. Please see:
http://en.wikipedia.org/wiki/Double-precision_floating-point_format[^].

—SA


尝试使用 GNU多精度算术库 [ ^ ].

GMP的精度仅受可用内存的限制.

最好的问候
Espen Harlinn
Try using the GNU Multiple Precision Arithmetic Library [^].

The precision of GMP is only limited by available memory.

Best regards
Espen Harlinn


对于您的应用程序,我不会太担心精度.如果您在普通的Intel机器上使用C ++数据类型 double 表示纬度和经度,则精度将至少为15个十进制数字.以弧度表示的弧秒为5 * 10 **-6.因此,您的精度约为10 **-9弧秒,每个精度对应于表面上的30m.因此,您的结果精确到大约10 **-7米,即十分之一微米.对于大多数应用程序来说,这听起来已经足够了.

另一个问题是如何避免结果中的所有9s. 89.999999999度的纬度看起来很难看.成功的秘诀是四舍五入.使用printf时,如果您指定一定数量的小数,它将为您舍入,例如:

I would not worry about precision too much for your application. If you are expressing latitude and longitude with C++ data type double on a normal Intel machine you will have at least 15 decimal digits of precision. An arc second represented as radian is 5 * 10**-6. So you have a precision of about 10**-9 arc seconds, each of which corresponds to 30m on the surface. So your results are precise to about 10**-7 meters, which is a tenth of a micrometer. That sounds like enough for most applications.

Another question is how to avoid all those 9s in results. 89.999999999 degrees latitude simply looks ugly. The secret to success here is rounding. When using printf, it will do the rounding for you if you specify a certain number of decimals, for example:

printf ("%.6f", 89.9999999999999);


将产生


will yield

90.000000



现在,如果您将纬度和经度转换为更易理解的格式,例如N53 34''47,那么请注意实现正确的舍入机制.



Now, if you convert your latitudes and longitudes to more human readable formats, like N53 34''47" then pay attention to implement the correct rounding mechanisms.


这篇关于有关使用double的快速问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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