arr = [] for i inrange(10000): arr.append(randFloat32()) minNum = min(arr) maxNum = max(arr) print(minNum, maxNum) div = 50# 把这一整段区间分成很多段,分别看每一段包含的数量 partLen = (maxNum - minNum) / div y = [] x = [] for i inrange(div): left = minNum + partLen * i count = 0 for n in arr: if left <= n < left + partLen: count += 1 y.append(count) x.append(f"{int(left)}") print(f"{count} /t/t{int(left)}~{int(left + partLen)}", )
defstrToFloat32(string): """把一个32位二进制字符流解析成float类型""" # 解析二进制字符流 flag = string[0] head = string[1:9] tile = string[9:] headNumber = strBinToInt(head) tile = "1" + tile left = strBinToInt(tile[:headNumber + 1]) right = 0.0 rightStr = tile[headNumber + 1:] for i, char inenumerate(rightStr): if char == "1": right += 0.5 ** i ... if flag == "0": return left + right else: return -(left + right)
defrandFloat32(): """随机生成一个float32的浮点数""" randStr = "" for i inrange(32): if random.random() < 0.5: randStr += "0" else: randStr += "1" return strToFloat32(randStr)