在 R 中控制打印输出中的十进制位数 [英] Controlling number of decimal digits in print output in R

查看:17
本文介绍了在 R 中控制打印输出中的十进制位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R 中有一个选项可以控制数字显示.例如:

There is an option in R to get control over digit display. For example:

options(digits=10)

应该以 10 位数字给出计算结果,直到 R 会话结束.在R的帮助文件中,digits参数的定义如下:

is supposed to give the calculation results in 10 digits till the end of R session. In the help file of R, the definition for digits parameter is as follows:

digits:控制位数打印数值时打印.这只是一个建议.有效值是 1...22 与默认 7

digits: controls the number of digits to print when printing numeric values. It is a suggestion only. Valid values are 1...22 with default 7

所以,它说这只是一个建议.如果我喜欢总是显示 10 位数字,而不是更多或更少怎么办?

So, it says this is a suggestion only. What if I like to always display 10 digits, not more or less?

我的第二个问题是,如果我喜欢显示超过 22 位的数字,即更精确的计算,如 100 位数字,该怎么办?是否可以使用基础 R,或者我是否需要额外的包/函数?

My second question is, what if I like to display more than 22 digits, i.e. for more precise calculations like 100 digits? Is it possible with base R, or do I need an additional package/function for that?

感谢 jmoy 的建议,我尝试了 sprintf("%.100f",pi) 并且它给了

Thanks to jmoy's suggestion, I tried sprintf("%.100f",pi) and it gave

[1] "3.1415926535897931159979634685441851615905761718750000000000000000000000000000000000000000000000000000"

有 48 位小数.这是 R 可以处理的最大限制吗?

which has 48 decimals. Is this the maximum limit R can handle?

推荐答案

这只是一个建议的原因是你可以很容易地编写一个忽略选项值的打印函数.内置的打印和格式化功能确实使用 options 值作为默认值.

The reason it is only a suggestion is that you could quite easily write a print function that ignored the options value. The built-in printing and formatting functions do use the options value as a default.

关于第二个问题,由于 R 使用有限精度算术,您的答案在小数点后 15 位或 16 位后不准确,因此通常不需要更多.gmprcdd 包处理多精度算术(通过与 gmp 库的交互),但这主要与大整数而不是双精度数的更多小数位.

As to the second question, since R uses finite precision arithmetic, your answers aren't accurate beyond 15 or 16 decimal places, so in general, more aren't required. The gmp and rcdd packages deal with multiple precision arithmetic (via an interace to the gmp library), but this is mostly related to big integers rather than more decimal places for your doubles.

MathematicaMaple 将允许您提供任意数量的小数位.

Mathematica or Maple will allow you to give as many decimal places as your heart desires.


考虑小数位和有效数字之间的差异可能很有用.如果您进行的统计检验依赖于 15 位有效数字以外的差异,那么您的分析几乎肯定是垃圾.


It might be useful to think about the difference between decimal places and significant figures. If you are doing statistical tests that rely on differences beyond the 15th significant figure, then your analysis is almost certainly junk.

另一方面,如果你只是处理非常小的数字,那问题不大,因为 R 可以处理小到 .Machine$double.xmin(通常是 2e-第308页.

On the other hand, if you are just dealing with very small numbers, that is less of a problem, since R can handle number as small as .Machine$double.xmin (usually 2e-308).

比较这两种分析.

x1 <- rnorm(50, 1, 1e-15)
y1 <- rnorm(50, 1 + 1e-15, 1e-15)
t.test(x1, y1)  #Should throw an error

x2 <- rnorm(50, 0, 1e-15)
y2 <- rnorm(50, 1e-15, 1e-15)
t.test(x2, y2)  #ok

在第一种情况下,数字之间的差异仅出现在许多有效数字之后,因此数据几乎不变".在第二种情况下,虽然数字之间的差异大小相同,但与数字本身的大小相比,它们是大的.

In the first case, differences between numbers only occur after many significant figures, so the data are "nearly constant". In the second case, Although the size of the differences between numbers are the same, compared to the magnitude of the numbers themselves they are large.

如 e3bo 所述,您可以使用 Rmpfr 包使用多精度浮点数.

As mentioned by e3bo, you can use multiple-precision floating point numbers using the Rmpfr package.

mpfr("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825")

与常规(双精度)numeric 向量相比,这些向量使用起来更慢且占用更多内存,但如果您遇到条件不佳的问题或算法不稳定,则可能会很有用.

These are slower and more memory intensive to use than regular (double precision) numeric vectors, but can be useful if you have a poorly conditioned problem or unstable algorithm.

这篇关于在 R 中控制打印输出中的十进制位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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