这个要看初始数据的精度。随机数生成与其他输入还有些区别。
help data types
Precision of numeric storage types
floats have about 7 digits of accuracy; the magnitude of the number does not matter. Thus,
1234567 can be stored perfectly as a float, as can 1234567e+20. The number 123456789, however,
would be rounded to 123456792. In general, this rounding does not matter.
If you are storing identification numbers, the rounding could matter. If the identification
numbers are integers and take 9 digits or less, store them as longs; otherwise, store them as
doubles. doubles have 16 digits of accuracy.
Stata stores numbers in binary, and this has a second effect on numbers less than 1. 1/10 has
no perfect binary representation just as 1/11 has no perfect decimal representation. In float,
.1 is stored as .10000000149011612. Note that there are 7 digits of accuracy, just as with
numbers larger than 1. Stata, however, performs all calculations in double precision. If you
were to store 0.1 in a float called x and then ask, say, "list if x==.1", there would be nothing
in the list. The .1 that you just typed was converted to double, with 16 digits of accuracy
(.100000000000000014...), and that number is never equal to 0.1 stored with float accuracy.
One solution is to type "list if x==float(.1)". The float() function rounds its argument to
float accuracy; see [D] functions. The other alternative would be store your data as double,
but this is probably a waste of memory. Few people have data that is accurate to 1 part in 10
to the 7th. Among the exceptions are banks, who keep records accurate to the penny on amounts
of billions of dollars. If you are dealing with such financial data, store your dollar amounts
as doubles. See float().
******
clear all
set ty float 或 double /*对应不同结果*/
inp x
1.0
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
end
levelsof x
n di "`r(levels)'"
***比较保险且省事的方法是,用group()将各值转换(对应)为整数,再利用整数操作(使用levelsof)。