可能重复:
在 R 中,这两者有什么区别?
R中的浮点问题?
这是我创建的代码的一部分。我花了几天时间寻找问题,最终意识到应该为 TRUE 的比较被 R 计算为 FALSE。我在 Windows 上使用 R 2.14.2 64 位。这是重现问题的代码。
concList= c(1.15, 1.15, 1.15 ,1.15 ,1.15 ,1.15 )
concList=concList-0.4
a=sum(concList)
b=length(concList)*0.75
str(a)
str(b)
print(a==b)
Run Code Online (Sandbox Code Playgroud)
即使它们显示为完全相同的数字,最后一次打印也会导致 FALSE。我认为这可能是 R 的浮点数值表示上的一些问题,所以我添加了下面的代码来解决这个问题。
a=round(a,1)
b=round(b,1)
print(a==b)
Run Code Online (Sandbox Code Playgroud)
我的问题是,有没有更优雅的解决方案?这是一个应该报告的错误吗?
谢谢你的时间。
我想获得如下所示的舍入值。
round (1.4) = 1
round (1.5) = 1
round (1.6) = 2
Run Code Online (Sandbox Code Playgroud)
如何使用 Java 代码将 (1.5) 轮的结果变为 1 而不是 2?
我想让我的公式结果保留 1 位小数但四舍五入。有没有我可以添加打印的公式?也许有修剪?这也可以。
在 Python 3.8 中,我试图让浮点值显示如下:
我知道“圆”。如果我有这个程序:
print ('input a number')
chuu = float(input())
chuu = round(chuu,2)
print (chuu)
Run Code Online (Sandbox Code Playgroud)
我也知道我可以这样做:
print ('input a number')
chuu = float(input())
print('{0:.2f}'.format(chuu))
Run Code Online (Sandbox Code Playgroud)
这也没有得到我想要的:
我该如何解决这个问题?
为什么toFixed()在以下情况下不能按预期工作?解决办法是什么?
代码:
var num = 5.56789;
var n = num.toFixed(16);
console.log(num)
console.log(n)Run Code Online (Sandbox Code Playgroud)
n 的期望值: 5.5678900000000000
n 的实际值: 5.5678900000000002
笔记:
我试图将一个数字四舍五入到最接近的十(而不是十分之一)。例如,52 会转到 50,29 会转到 30,325 会转到 330,等等。我该怎么做??
我想一次生成一个达到特定精度的随机数(所以我不是在寻找矢量化解决方案)。
我在 stackoverflow 上的这个 QnA 中找到了一个方法,它按照承诺给了我这些基准。该方法绝对快两倍。现在,这就是让我感到困惑的地方。
%timeit int(0.5192853551955484*(10**5)+0.5)/(10.**5) #> 149 ns ± 5.76 ns per loop
%timeit round(0.5192853551955484, 5) #> 432 ns ± 11.7 ns per loop
## Faster as expected
fl = random.random()
pr = 5
%timeit int(fl*(10**pr)+0.5)/(10.**pr) #> 613 ns ± 27.9 ns per loop
%timeit round(fl, pr) #> 444 ns ± 9.25 ns per loop
## Slower?!
%timeit int(random.random()*(10**5)+0.5)/(10.**5) #> 280 ns ± 29.3 ns per loop
%timeit round(random.random(), 5) #> 538 ns ± 17.5 …Run Code Online (Sandbox Code Playgroud) 我尝试在 Stack Overflow 上搜索,但找不到答案。
这是代码:
#include <stdio.h>
int main(void) {
double y;
printf("Enter a number: ");
scanf("%lf", &y);
printf("Your number when rounded is: %.2lf", y);
//If user inputs 5.05286, how can i round off this number so as to get the output as 5.00
//I want the output to be rounded as well as to be 2 decimal places like 10.6789 becomes 11.00
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想对一个数字进行四舍五入,例如,如果数字是5.05286,则应四舍五入为5.00,如果是5.678901,则将四舍五入到6.00带2小数位。数字5.678901正在四舍五入,5.05 …
有一系列完整的正数,例如
827818
6574762
685038
55326902
Run Code Online (Sandbox Code Playgroud)
例如,我需要四舍五入到数百才能得到相应的结果
827800
6574700
685000
55326900
Run Code Online (Sandbox Code Playgroud)
例如,许多想法如何使用 Javascript 向上、向下或最接近数百
Math.floor(number / 100) * 100;
Run Code Online (Sandbox Code Playgroud)
但是可以在 Bash 中做同样的事情吗?
我想要floor()下一个特定数字(不是数字,而是实际值)。例如,对于年份列表:
valid_years = c(1990,2000,2006,2012,2018) # values to round to ("snap")
input = c(1990, 1991, 1992, 1993, 2000, 2001, 2002, 2006, 2007, 2016, 2020)
output = c(1990, 1990, 1990, 1990, 2000, 2000, 2000, 2006, 2006, 2012, 2018)
Run Code Online (Sandbox Code Playgroud)
低于(或在ceil()高于指定值的情况下)输入的行为对我来说并不重要。在我的情况下,一个好的行为是捕捉到 中的最低值valid_years,但这对我来说并不重要。
我知道我可以用 if-then-else (eg if(x < 2006) x = 2000 else if(x < 2012) x = 2006 ...)来实现这一点,但我相信有更优雅的方法来解决这个问题。
我浏览了许多“R 中的舍入”问题,但尽管有许多措辞相似的问题,但都没有找到答案,这些问题都有其独特的(不同)目标:例如,四舍五入到特定的小数步长, 四舍五入到某个范围内的任意数字或条件舍入。
rounding ×10
python ×3
python-3.x ×3
r ×2
bash ×1
c ×1
floor ×1
java ×1
javascript ×1
math ×1
numbers ×1
python-3.6 ×1
tofixed ×1