G***G 发帖数: 16778 | 1 【 以下文字转载自 Programming 讨论区 】
发信人: GoooG (pumpkin), 信区: Programming
标 题: round问题
发信站: BBS 未名空间站 (Sat Nov 29 14:46:09 2014, 美东)
round(2.5) =2
round(3.5) =4
this happened in R.
why? |
l******t 发帖数: 96 | 2 # Round to nearest, with .5 values rounded to even number.
【在 G***G 的大作中提到】 : 【 以下文字转载自 Programming 讨论区 】 : 发信人: GoooG (pumpkin), 信区: Programming : 标 题: round问题 : 发信站: BBS 未名空间站 (Sat Nov 29 14:46:09 2014, 美东) : round(2.5) =2 : round(3.5) =4 : this happened in R. : why?
|
G***G 发帖数: 16778 | 3 in R which function can round 'not to even'?
have you checked the following one?
(0.1+0.05)>0.15
The stupid R gives 'TRUE' as answer.
【在 l******t 的大作中提到】 : # Round to nearest, with .5 values rounded to even number.
|
D******n 发帖数: 2836 | 4 我好像以前碰到过这个问题,一气之下自己写了个round function。
R的base经常缺乏一些最基本的functions,很让人头痛。
【在 G***G 的大作中提到】 : 【 以下文字转载自 Programming 讨论区 】 : 发信人: GoooG (pumpkin), 信区: Programming : 标 题: round问题 : 发信站: BBS 未名空间站 (Sat Nov 29 14:46:09 2014, 美东) : round(2.5) =2 : round(3.5) =4 : this happened in R. : why?
|
l******t 发帖数: 96 | 5 it's not about R, it's how floating number is represented in computer.
Even you typed the same equation and get True in Python.
【在 G***G 的大作中提到】 : in R which function can round 'not to even'? : have you checked the following one? : (0.1+0.05)>0.15 : The stupid R gives 'TRUE' as answer.
|
G***G 发帖数: 16778 | 6 does this exist in other language such as C?
【在 l******t 的大作中提到】 : it's not about R, it's how floating number is represented in computer. : Even you typed the same equation and get True in Python.
|
l******t 发帖数: 96 | 7 The result is the same in my test case in C++. I think it's common sense not
to use "=" to compare floating number in programming languages.
#include
using namespace std;
int main() {
bool result = (.1 + .05) > .15;
cout << result;
return 0;
}
g++ test.cpp && ./a.out
1
【在 G***G 的大作中提到】 : does this exist in other language such as C?
|
G***G 发帖数: 16778 | 8 interesting.
thanks.
not
【在 l******t 的大作中提到】 : The result is the same in my test case in C++. I think it's common sense not : to use "=" to compare floating number in programming languages. : #include : using namespace std; : int main() { : bool result = (.1 + .05) > .15; : cout << result; : return 0; : } : g++ test.cpp && ./a.out
|
H*H 发帖数: 472 | 9 R的本质是数据分析,要是2.5 round 到3, 3.5 round 到4,都比原来大了0.5,得到
的结果就很容易引起bias
要是你想得到你要的结果, 用floor(0.5 + x) 应该就行了 |