python - 乘法时R十进制精度发生变化
问题描述
R中的精度问题
当我在 R 中乘以高精度双值数字时,精度似乎发生了变化或发生了一些奇怪的事情。查看最后 4 位数字。
x <- 1608781598.186771296
y<-1000000000.00
print(x*y)
#1608781598186771456
#turns out that R is converting the precision like so
print(as.double(1608781598.186771296),digits=19)
#1608781598.186771393
#if I use the above value and multiply by a billion I can reproduce the incorrect value
print(1608781598.186771393*1000000000.0)
#1608781598186771456
python中的类似问题
事实上,类似的事情也在 Python 中发生
import numpy as np
print("{:.0f}".format(np.float128(1608781598.186771296)*np.float128(1000000000)))
#1608781598186771456
而我期望的值为 160878159818677 1296。这到底是怎么回事?
解决方案
在 R 中,每个双精度值或十进制值都精确到 16 位有效数字。您的“x”有超过 16 个有效数字,因此在存储时已经对“x”变量进行了四舍五入。因此出现差异。
推荐阅读
- android - camera.isEmpty null 颤振
- ios - 在应用程序生产时更改标签位置
- wordpress - Flexslider 无法正常工作 - Wordpress
- r - pheatmap:更改文本颜色
- regex - 查找所有可能的首字母缩略词
- cassandra - cassandra:插入或更新后是否/可以返回新版本 ID?
- python-3.x - python 记录器级别和处理程序级别优先级
- java - 如何通过 IP 地址查找来识别 AWS 组件?
- r - Keras,LIME:“解释”函数中没有为“lstm__input”提供数据
- python - 将工件上传到 JFrog Artifactory