全部版块 我的主页
论坛 计量经济学与统计论坛 五区 计量经济学与统计软件 Stata专版
2977 1
2009-05-17

gen uphitday=1 if high==round(preclose*1.1,.01)

gen p=round(preclose*1.1,0.01)

gen uphitday=1 if p==high

得到的结果居然不一样。用的是stata 10.1

谢谢。

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2009-5-21 11:29:00


和stata数据存储和计算时的精度有关,在stata中用 == 运算符必须非常小心。

Although you may store your numeric variables as byte, int, long, float, or double, Stata converts all numbers to double before performing any calculations. 
Consequently, difficulties can arise in comparing numbers that have no finite binary representations.

For example, if the variable x is stored as a float and contains the value 1.1 (a repeating "decimal" in binary), the expression x==1.1 will evaluate to false because the literal 1.1 is the double representation of 1.1, which is different from the float representation stored in x.  (They differ by 2.384 x 10^(-8).)
The expression x==float(1.1) will evaluate to true because the float() function converts the literal 1.1 to its float representation before it is compared with x. (See [U] 13.10 Precision and problems therein for more information.)

在你的数据中high的存储类型应该是float?
第二种方法可行。或用gen uphitday=1 if high==float(round(preclose*1.1,.01))。此时两种方法的结果应该一样。








[此贴子已经被作者于2009-5-21 11:31:54编辑过]

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群