简体   繁体   English

对R中的比例缩放的数字集求和时避免浮点零

[英]Avoid floating point zero when summing scaled set of numbers in R

I am rescaling a set of numbers and want to avoid getting a floating point zero for the sum of the rescaled numbers: 我正在重新缩放一组数字,并希望避免为重新缩放的数字之和得到一个浮点零:

x <- c(-5, 1, 8)
y <- scale(x)
sum(y)
# [1] 1.249001e-16

Is there a way to around this to force the sum to zero? 有没有办法解决这个问题,将总和强制为零? I do not care about precision beyond a three decimal places. 我不在乎小数点后三位的精度。

I think you should not just "switch" to integers at some point. 我认为您不应该在某个时候“转换”为整数。 The scaling was computed with floating point numbers and is therefore not 100% precise. 标度是使用浮点数计算的,因此不是100%精确的。 Forcing some values to 0 would indicate a precision that is not available and should thus be avoided. 将某些值强制为0将指示不可用的精度,因此应避免使用。

If you need to compare floating point values use isTRUE(all.equal(...)) as suggested by R documentation. 如果需要比较浮点值,请使用R文档建议的isTRUE(all.equal(...)) https://stat.ethz.ch/R-manual/R-devel/library/base/html/all.equal.html https://stat.ethz.ch/R-manual/R-devel/library/base/html/all.equal.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM